const KNOWN_COMMANDS = ['start', 'stop', 'status', 'logs', 'init', 'discover', 'validate', 'followup-importants', 'setup'];
function hasFlag(args, long, short) {
    return args.includes(long) || (short !== undefined && args.includes(short));
}
function getFlagValue(args, long, short) {
    for (let index = 0; index < args.length; index++) {
        if (args[index] === long || (short !== undefined && args[index] === short)) {
            return args[index + 1];
        }
    }
    return undefined;
}
function extractCommand(args) {
    const positional = args.find((arg) => !arg.startsWith('-'));
    if (positional && KNOWN_COMMANDS.includes(positional)) {
        return positional;
    }
    return 'start';
}
function parseStartArgs(args) {
    const portValue = getFlagValue(args, '--port', '-p');
    return {
        command: 'start',
        skipDependencyCheck: hasFlag(args, '--skip-dependency-check'),
        daemon: hasFlag(args, '--daemon', '-d'),
        port: portValue !== undefined ? Number(portValue) : undefined,
        open: hasFlag(args, '--open', '-o'),
    };
}
function parseStopArgs(args) {
    return {
        command: 'stop',
        force: hasFlag(args, '--force', '-f'),
    };
}
function parseStatusArgs(args) {
    return {
        command: 'status',
        json: hasFlag(args, '--json'),
    };
}
function parseFollowupImportantsArgs(args) {
    return {
        command: 'followup-importants',
        project: getFlagValue(args, '--project', '-p'),
        yes: hasFlag(args, '--yes', '-y'),
    };
}
function parseLogsArgs(args) {
    const linesValue = getFlagValue(args, '--lines', '-n');
    return {
        command: 'logs',
        follow: hasFlag(args, '--follow', '-f'),
        lines: linesValue !== undefined ? Number(linesValue) : 20,
    };
}
function getAllFlagValues(args, long) {
    const values = [];
    for (let index = 0; index < args.length; index++) {
        if (args[index] === long && args[index + 1] !== undefined) {
            values.push(args[index + 1]);
        }
    }
    return values;
}
function parseInitArgs(args) {
    return {
        command: 'init',
        yes: hasFlag(args, '--yes', '-y'),
        skipMcp: hasFlag(args, '--skip-mcp'),
        showSecrets: hasFlag(args, '--show-secrets'),
        scanPaths: getAllFlagValues(args, '--scan-path'),
    };
}
function parseDiscoverArgs(args) {
    const maxDepthValue = getFlagValue(args, '--max-depth');
    return {
        command: 'discover',
        scanPaths: getAllFlagValues(args, '--scan-path'),
        maxDepth: maxDepthValue !== undefined ? Number(maxDepthValue) : 3,
    };
}
function parseValidateArgs(args) {
    return {
        command: 'validate',
        fix: hasFlag(args, '--fix'),
    };
}
function isPositionalPath(arg, index, args) {
    if (arg.startsWith('-'))
        return false;
    if (arg === 'setup')
        return false;
    return index === 0 || args[index - 1] !== '--path';
}
function parseSetupArgs(args) {
    const positional = args.find((arg, index) => isPositionalPath(arg, index, args));
    const flagPath = getFlagValue(args, '--path');
    return {
        command: 'setup',
        path: flagPath ?? positional,
        json: hasFlag(args, '--json'),
        force: hasFlag(args, '--force'),
        ai: hasFlag(args, '--ai'),
        yes: hasFlag(args, '--yes', '-y'),
        showSecrets: hasFlag(args, '--show-secrets'),
    };
}
export function parseCliArgs(args) {
    if (hasFlag(args, '--version', '-v')) {
        return { command: 'version' };
    }
    if (hasFlag(args, '--help', '-h')) {
        return { command: 'help' };
    }
    const command = extractCommand(args);
    switch (command) {
        case 'start':
            return parseStartArgs(args);
        case 'stop':
            return parseStopArgs(args);
        case 'status':
            return parseStatusArgs(args);
        case 'logs':
            return parseLogsArgs(args);
        case 'init':
            return parseInitArgs(args);
        case 'discover':
            return parseDiscoverArgs(args);
        case 'validate':
            return parseValidateArgs(args);
        case 'followup-importants':
            return parseFollowupImportantsArgs(args);
        case 'setup':
            return parseSetupArgs(args);
    }
}
//# sourceMappingURL=parseCliArgs.js.map