"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectUnexpectedDockerArgs = exports.parseDockerLogsOptions = exports.parseDockerUpOptions = exports.parseDockerTarget = void 0;
const arg_extractor_1 = require("../arg-extractor");
function parseNumberOption(args, flag, knownFlags) {
    const extracted = (0, arg_extractor_1.extractOption)(args, [flag], { knownFlags });
    if (!extracted.found) {
        return { remainingArgs: args };
    }
    if (extracted.missingValue || !extracted.value) {
        return { remainingArgs: extracted.remainingArgs, error: `Missing value for ${flag}` };
    }
    const value = Number.parseInt(extracted.value, 10);
    if (Number.isNaN(value) || value <= 0 || value >= 65536) {
        return { remainingArgs: extracted.remainingArgs, error: `Invalid value for ${flag}` };
    }
    return { value, remainingArgs: extracted.remainingArgs };
}
function parseDockerTarget(args, knownFlags) {
    const extracted = (0, arg_extractor_1.extractOption)(args, ['--host'], { knownFlags });
    if (!extracted.found) {
        return { errors: [], remainingArgs: args };
    }
    if (extracted.missingValue || !extracted.value?.trim()) {
        return { errors: ['Missing value for --host'], remainingArgs: extracted.remainingArgs };
    }
    const host = extracted.value.trim();
    if (host.startsWith('-') || /\s/.test(host)) {
        return {
            errors: [
                'Invalid value for --host. Use a single SSH target or SSH config alias such as my-box or user@host.',
            ],
            remainingArgs: extracted.remainingArgs,
        };
    }
    return {
        errors: [],
        remainingArgs: extracted.remainingArgs,
        host,
    };
}
exports.parseDockerTarget = parseDockerTarget;
function parseDockerUpOptions(args, knownFlags) {
    const target = parseDockerTarget(args, knownFlags);
    const port = parseNumberOption(target.remainingArgs, '--port', knownFlags);
    const proxyPort = parseNumberOption(port.remainingArgs, '--proxy-port', knownFlags);
    const errors = [...target.errors];
    if (port.error) {
        errors.push(port.error);
    }
    if (proxyPort.error) {
        errors.push(proxyPort.error);
    }
    return {
        ...target,
        remainingArgs: proxyPort.remainingArgs,
        port: port.value,
        proxyPort: proxyPort.value,
        errors,
    };
}
exports.parseDockerUpOptions = parseDockerUpOptions;
function parseDockerLogsOptions(args, knownFlags) {
    const target = parseDockerTarget(args, knownFlags);
    const service = (0, arg_extractor_1.extractOption)(target.remainingArgs, ['--service'], { knownFlags });
    let parsedService;
    const errors = [...target.errors];
    if (service.found) {
        if (service.missingValue || !service.value) {
            errors.push('Missing value for --service');
        }
        else if (service.value !== 'ccs' && service.value !== 'cliproxy') {
            errors.push('Invalid value for --service. Use: ccs or cliproxy');
        }
        else {
            parsedService = service.value;
        }
    }
    return {
        ...target,
        remainingArgs: service.remainingArgs,
        follow: (0, arg_extractor_1.hasAnyFlag)(service.remainingArgs, ['--follow']),
        service: parsedService,
        errors,
    };
}
exports.parseDockerLogsOptions = parseDockerLogsOptions;
function collectUnexpectedDockerArgs(args, options) {
    const scanned = (0, arg_extractor_1.scanCommandArgs)(args, {
        knownFlags: options.knownFlags,
        valueFlags: options.valueFlags,
    });
    const errors = scanned.unknownFlags.map((flag) => `Unknown option: ${flag}`);
    const maxPositionals = options.maxPositionals ?? 0;
    if (scanned.positionals.length > maxPositionals) {
        errors.push(`Unexpected arguments: ${scanned.positionals.slice(maxPositionals).join(' ')}`);
    }
    return errors;
}
exports.collectUnexpectedDockerArgs = collectUnexpectedDockerArgs;
//# sourceMappingURL=options.js.map