function pushFlag(flags, name, value) {
    const stringify = (flagValue) => {
        if (flagValue === true)
            return "true";
        if (flagValue === false)
            return "false";
        return flagValue;
    };
    const current = flags[name];
    if (current === undefined) {
        flags[name] = value;
        return;
    }
    if (Array.isArray(current)) {
        flags[name] = [...current, stringify(value)];
        return;
    }
    if (typeof current === "string" || typeof current === "boolean") {
        flags[name] = [stringify(current), stringify(value)];
    }
}
export function tokenizeSlashArgs(raw) {
    const tokens = [];
    let current = "";
    let quote;
    let escaping = false;
    for (const char of raw) {
        if (escaping) {
            current += char;
            escaping = false;
            continue;
        }
        if (char === "\\") {
            escaping = true;
            continue;
        }
        if (quote) {
            if (char === quote) {
                quote = undefined;
            }
            else {
                current += char;
            }
            continue;
        }
        if (char === "'" || char === "\"") {
            quote = char;
            continue;
        }
        if (/\s/.test(char)) {
            if (current.length > 0) {
                tokens.push(current);
                current = "";
            }
            continue;
        }
        current += char;
    }
    if (escaping)
        current += "\\";
    if (current.length > 0)
        tokens.push(current);
    return tokens;
}
export function parseSlashArgs(raw) {
    const argv = tokenizeSlashArgs(raw);
    const flags = {};
    const positional = [];
    for (const token of argv) {
        if (!token.startsWith("--") || token === "--") {
            positional.push(token);
            continue;
        }
        const body = token.slice(2);
        const eq = body.indexOf("=");
        if (eq >= 0) {
            pushFlag(flags, body.slice(0, eq), body.slice(eq + 1));
        }
        else {
            pushFlag(flags, body, true);
        }
    }
    return { raw, argv, positional, flags };
}
export function parseSlashInput(input) {
    const raw = input.trim();
    if (!raw.startsWith("/") && !raw.startsWith(":"))
        return undefined;
    const match = /^(\S+)(?:\s+([\s\S]*))?$/.exec(raw);
    if (!match)
        return undefined;
    return {
        raw,
        command: match[1]?.toLowerCase() ?? raw.toLowerCase(),
        args: parseSlashArgs(match[2] ?? ""),
    };
}
