import { buildProviderContext, createExecutionResources, resolveCommandFlags, resolveServiceAdapter, formatServiceList, listServiceNames } from "./shared.js";
import { resolveServiceArgument } from "./configure.js";
import { withSpinner } from "toolcraft-design";
export function registerInstallCommand(program, container) {
    const serviceNames = container.registry
        .list()
        .filter((service) => typeof service.install === "function");
    const serviceDescription = `Agent to install${formatServiceList(listServiceNames(serviceNames))}`;
    return program
        .command("install")
        .alias("i")
        .description("Install agent binary for a configured agent.")
        .argument("[agent]", serviceDescription)
        .action(async (service) => {
        const resolved = await resolveServiceArgument(program, container, service, { action: "install" });
        await executeInstall(program, container, resolved);
    });
}
export async function executeInstall(program, container, service) {
    const adapter = resolveServiceAdapter(container, service);
    const canonicalService = adapter.name;
    const flags = resolveCommandFlags(program);
    const resources = createExecutionResources(container, flags, `install:${canonicalService}`);
    resources.logger.intro(`install ${canonicalService}`);
    const providerContext = buildProviderContext(container, adapter, resources);
    const installProvider = async () => {
        await container.registry.invoke(canonicalService, "install", async (entry) => {
            if (!entry.install) {
                throw new Error(`Agent "${canonicalService}" does not support install.`);
            }
            await entry.install(providerContext);
        });
    };
    if (flags.dryRun) {
        await installProvider();
    }
    else {
        await withSpinner({
            message: `Installing ${adapter.label}...`,
            fn: installProvider
        });
    }
    const dryMessage = canonicalService === "claude-code"
        ? `${adapter.label} install (dry run)`
        : `Dry run: would install ${adapter.label}.`;
    resources.context.complete({
        success: `Installed ${adapter.label}.`,
        dry: dryMessage
    });
    resources.context.finalize();
}
//# sourceMappingURL=install.js.map