"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleUp = void 0;
const docker_1 = require("../../docker");
const ui_1 = require("../../utils/ui");
const options_1 = require("./options");
const KNOWN_FLAGS = ['--host', '--port', '--proxy-port'];
async function handleUp(args) {
    await (0, ui_1.initUI)();
    const parsed = (0, options_1.parseDockerUpOptions)(args, KNOWN_FLAGS);
    const errors = [
        ...parsed.errors,
        ...(0, options_1.collectUnexpectedDockerArgs)(parsed.remainingArgs, {
            knownFlags: [],
            maxPositionals: 0,
        }),
    ];
    if (errors.length > 0) {
        console.error((0, ui_1.box)((0, ui_1.fail)(errors.join('\n')), { title: 'Docker', padding: 1 }));
        process.exitCode = 1;
        return;
    }
    const executor = new docker_1.DockerExecutor();
    const port = parsed.port ?? docker_1.DOCKER_DEFAULT_DASHBOARD_PORT;
    const proxyPort = parsed.proxyPort ?? docker_1.DOCKER_DEFAULT_PROXY_PORT;
    console.log((0, ui_1.info)(`Starting integrated Docker stack${parsed.host ? ` on ${parsed.host}` : ''}...`));
    try {
        await executor.up({ host: parsed.host, port, proxyPort });
        const rotationBanner = executor.getKeyRotationBanner({ host: parsed.host });
        if (rotationBanner) {
            console.error(rotationBanner);
        }
        console.log((0, ui_1.ok)(`Docker stack is running${parsed.host ? ` on ${parsed.host}` : ' locally'}.`));
        console.log((0, ui_1.info)(`Dashboard port: ${port}`));
        console.log((0, ui_1.info)(`CLIProxy port: ${proxyPort}`));
        if (parsed.host) {
            console.log((0, ui_1.info)('Full remote management requires dashboard auth. Without it, remote access stays read-only.\nRun inside the container:\n  docker exec -it ccs-cliproxy ccs config auth setup'));
        }
    }
    catch (error) {
        console.error((0, ui_1.box)((0, ui_1.fail)(error instanceof Error ? error.message : String(error)), {
            title: 'Docker',
            padding: 1,
        }));
        process.exitCode = 1;
    }
}
exports.handleUp = handleUp;
//# sourceMappingURL=up-subcommand.js.map