#!/usr/bin/env node
import path from "node:path";
import { buildRepoControlState } from "../core/ui-state.js";
import { loadMachineAdvisory, loadMachineAdvisorySection } from "../integrations/machine-advisory.js";
import { resolveShreyJuniorProductRoot } from "../runtime.js";
async function main() {
    const parsed = parseArgs(process.argv.slice(2));
    const repoRoot = typeof parsed.flags["repo-root"] === "string"
        ? path.resolve(String(parsed.flags["repo-root"]))
        : resolveShreyJuniorProductRoot(import.meta.url);
    const fastMode = parsed.flags["machine-fast"] === true || parsed.flags.fast === true;
    const forceRefresh = parsed.flags.refresh === true;
    switch (parsed.command) {
        case "repo-state": {
            const targetRoot = typeof parsed.flags["target-root"] === "string"
                ? path.resolve(String(parsed.flags["target-root"]))
                : process.cwd();
            const profileName = typeof parsed.flags.profile === "string" ? String(parsed.flags.profile) : undefined;
            const state = await buildRepoControlState({
                repoRoot,
                targetRoot,
                ...(profileName ? { profileName } : {}),
                machineAdvisoryOptions: {
                    fastMode
                },
                readOnly: true
            });
            process.stdout.write(`${JSON.stringify(state, null, 2)}\n`);
            return;
        }
        case "machine-advisory": {
            const advisory = await loadMachineAdvisory({
                forceRefresh,
                fastMode
            });
            process.stdout.write(`${JSON.stringify(advisory, null, 2)}\n`);
            return;
        }
        case "machine-advisory-section": {
            if (typeof parsed.flags.section !== "string") {
                throw new Error("machine-advisory-section requires --section");
            }
            const section = String(parsed.flags.section);
            const advisorySection = await loadMachineAdvisorySection(section, {
                forceRefresh,
                fastMode
            });
            process.stdout.write(`${JSON.stringify(advisorySection, null, 2)}\n`);
            return;
        }
        default:
            throw new Error("usage: ui-bridge <repo-state|machine-advisory|machine-advisory-section> [flags]");
    }
}
function parseArgs(argv) {
    const [command, ...rest] = argv;
    const flags = {};
    for (let index = 0; index < rest.length; index += 1) {
        const value = rest[index];
        if (!value?.startsWith("--")) {
            continue;
        }
        const key = value.slice(2);
        const next = rest[index + 1];
        if (!next || next.startsWith("--")) {
            flags[key] = true;
            continue;
        }
        flags[key] = next;
        index += 1;
    }
    return { command, flags };
}
main().catch((error) => {
    const detail = error instanceof Error ? error.message : String(error);
    process.stderr.write(`${detail}\n`);
    process.exitCode = 1;
});
