"use strict";
/**
 * Small helpers for consistent CLI option extraction.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.scanCommandArgs = exports.hasAnyFlag = exports.extractOption = void 0;
function findInlineOption(arg, flag) {
    const prefix = `${flag}=`;
    return arg.startsWith(prefix) ? arg.slice(prefix.length) : undefined;
}
function isKnownFlagToken(token, knownFlags) {
    if (!knownFlags || knownFlags.length === 0) {
        return false;
    }
    return knownFlags.some((flag) => token === flag || token.startsWith(`${flag}=`));
}
function findMatchingFlagToken(token, knownFlags) {
    if (!knownFlags || knownFlags.length === 0) {
        return undefined;
    }
    return knownFlags.find((flag) => token === flag || token.startsWith(`${flag}=`));
}
/**
 * Extract a single-value option and remove it from args.
 * Supports `--flag value` and `--flag=value` forms.
 */
function extractOption(args, flags, options = {}) {
    const remaining = [...args];
    const allowDashValue = options.allowDashValue ?? false;
    const allowLongDashValue = options.allowLongDashValue ?? false;
    for (let i = 0; i < remaining.length; i++) {
        const token = remaining[i];
        for (const flag of flags) {
            if (token === flag) {
                const next = remaining[i + 1];
                if (!next) {
                    remaining.splice(i, 1);
                    return { found: true, missingValue: true, remainingArgs: remaining };
                }
                const nextLooksLikeFlag = next.startsWith('-');
                const nextLooksLikeLongFlag = next.startsWith('--');
                const nextIsKnownFlag = isKnownFlagToken(next, options.knownFlags);
                const canTreatAsDashValue = allowDashValue && !nextIsKnownFlag && (!nextLooksLikeLongFlag || allowLongDashValue);
                if (nextLooksLikeFlag && !canTreatAsDashValue) {
                    remaining.splice(i, 1);
                    return { found: true, missingValue: true, remainingArgs: remaining };
                }
                remaining.splice(i, 2);
                return {
                    found: true,
                    value: next,
                    missingValue: false,
                    remainingArgs: remaining,
                };
            }
            const inlineValue = findInlineOption(token, flag);
            if (inlineValue !== undefined) {
                remaining.splice(i, 1);
                if (!inlineValue.trim()) {
                    return { found: true, missingValue: true, remainingArgs: remaining };
                }
                return {
                    found: true,
                    value: inlineValue,
                    missingValue: false,
                    remainingArgs: remaining,
                };
            }
        }
    }
    return { found: false, missingValue: false, remainingArgs: remaining };
}
exports.extractOption = extractOption;
/** Returns true if any of the provided boolean flags are present. */
function hasAnyFlag(args, flags) {
    const truthyValues = new Set(['1', 'true', 'yes', 'on']);
    return args.some((arg) => flags.some((flag) => {
        if (arg === flag) {
            return true;
        }
        const prefix = `${flag}=`;
        if (!arg.startsWith(prefix)) {
            return false;
        }
        const value = arg.slice(prefix.length).trim().toLowerCase();
        return truthyValues.has(value);
    }));
}
exports.hasAnyFlag = hasAnyFlag;
function scanCommandArgs(args, options) {
    const positionals = [];
    const unknownFlags = [];
    const allowDashValue = options.allowDashValue ?? false;
    const valueFlags = new Set(options.valueFlags ?? []);
    for (let i = 0; i < args.length; i++) {
        const token = args[i];
        if (token === '--') {
            positionals.push(...args.slice(i + 1));
            break;
        }
        if (token === '-' || !token.startsWith('-')) {
            positionals.push(token);
            continue;
        }
        const matchedFlag = findMatchingFlagToken(token, options.knownFlags);
        if (!matchedFlag) {
            unknownFlags.push(token);
            continue;
        }
        if (!valueFlags.has(matchedFlag) || token.includes('=')) {
            continue;
        }
        const next = args[i + 1];
        if (!next) {
            continue;
        }
        const nextLooksLikeFlag = next.startsWith('-');
        const nextLooksLikeLongFlag = next.startsWith('--');
        const nextIsKnownFlag = isKnownFlagToken(next, options.knownFlags);
        if (!nextLooksLikeFlag || (allowDashValue && !nextIsKnownFlag && !nextLooksLikeLongFlag)) {
            i++;
        }
    }
    return { positionals, unknownFlags };
}
exports.scanCommandArgs = scanCommandArgs;
//# sourceMappingURL=arg-extractor.js.map