"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.handleDockerCommand = void 0;
const arg_extractor_1 = require("../arg-extractor");
const config_subcommand_1 = require("./config-subcommand");
const down_subcommand_1 = require("./down-subcommand");
const finalize_key_rotation_subcommand_1 = require("./finalize-key-rotation-subcommand");
const help_subcommand_1 = require("./help-subcommand");
const logs_subcommand_1 = require("./logs-subcommand");
const show_key_subcommand_1 = require("./show-key-subcommand");
const status_subcommand_1 = require("./status-subcommand");
const up_subcommand_1 = require("./up-subcommand");
const update_subcommand_1 = require("./update-subcommand");
function normalizeLeadingHostArg(args) {
    const firstToken = args[0];
    if (!firstToken || (firstToken !== '--host' && !firstToken.startsWith('--host='))) {
        return args;
    }
    const extracted = (0, arg_extractor_1.extractOption)(args, ['--host'], {
        knownFlags: ['--host', '--help', '-h'],
    });
    if (extracted.missingValue || !extracted.value?.trim()) {
        return args;
    }
    const command = extracted.remainingArgs[0];
    if (!command || command.startsWith('-')) {
        return args;
    }
    return [command, '--host', extracted.value.trim(), ...extracted.remainingArgs.slice(1)];
}
async function handleDockerCommand(args) {
    const normalizedArgs = normalizeLeadingHostArg(args);
    if ((0, arg_extractor_1.hasAnyFlag)(normalizedArgs, ['--help', '-h'])) {
        await (0, help_subcommand_1.showHelp)();
        return;
    }
    const command = normalizedArgs[0];
    const commandHandlers = {
        up: up_subcommand_1.handleUp,
        down: down_subcommand_1.handleDown,
        status: status_subcommand_1.handleStatus,
        update: update_subcommand_1.handleUpdate,
        logs: logs_subcommand_1.handleLogs,
        config: config_subcommand_1.handleConfig,
        'show-key': show_key_subcommand_1.handleShowKey,
        'finalize-key-rotation': finalize_key_rotation_subcommand_1.handleFinalizeKeyRotation,
        help: async () => (0, help_subcommand_1.showHelp)(),
    };
    if (!command) {
        await (0, help_subcommand_1.showHelp)();
        return;
    }
    const handler = commandHandlers[command];
    if (!handler) {
        await (0, help_subcommand_1.showHelp)();
        process.exitCode = 1;
        return;
    }
    await handler(normalizedArgs.slice(1));
}
exports.handleDockerCommand = handleDockerCommand;
//# sourceMappingURL=index.js.map