"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseSkillArgs = parseSkillArgs;
const control_plane_config_1 = require("../../config/control-plane-config");
const catalog_1 = require("../../i18n/catalog");
const shared_1 = require("./shared");
function parseSkillArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.usage"));
    }
    switch (action) {
        case "list":
            return parseSkillListArgs(rest, locale);
        case "search":
            return parseSkillSearchArgs(rest, locale);
        case "install":
            return parseSkillInstallArgs(rest, locale);
        case "create":
            return parseSkillCreateArgs(rest, locale);
        case "catalog":
            return parseSkillCatalogArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.unknownCommand", { action }));
    }
}
function parseSkillListArgs(args, locale) {
    const values = { action: "list" };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--for":
                values.target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillListTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--include-excluded":
                values.includeExcluded = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.list.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseSkillSearchArgs(args, locale) {
    let query;
    let target;
    let mode = "all";
    let withWorker = false;
    let includeExcluded = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale, "cli.parser.skill.search.invalidTarget"));
                index += 1;
                break;
            case "--installed-only":
                if (mode !== "all") {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.search.modeConflict"));
                }
                mode = "installed";
                break;
            case "--installable-only":
                if (mode !== "all") {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.search.modeConflict"));
                }
                mode = "installable";
                break;
            case "--with-worker":
                withWorker = true;
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                if (token?.startsWith("-")) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.search.unknownOption", {
                        token: token ?? "",
                    }));
                }
                if (query !== undefined) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.search.unexpectedArgument", {
                        value: token ?? "",
                    }));
                }
                query = (0, shared_1.parseNonEmptyStringArg)("query", token ?? "", locale);
                break;
        }
    }
    if (query === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.search.missingQuery"));
    }
    return {
        action: "search",
        query,
        mode,
        ...(includeExcluded ? { includeExcluded } : {}),
        ...(withWorker ? { withWorker } : {}),
        ...(target === undefined ? {} : { target }),
    };
}
function parseSkillInstallArgs(args, locale) {
    let source;
    let query;
    let target;
    let overwrite = false;
    let pluginScope = "project";
    let includeExcluded = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--source":
                source = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--search":
                query = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillInstallTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--overwrite":
                overwrite = true;
                break;
            case "--plugin-scope":
                pluginScope = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillInstallPluginScope((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale));
                index += 1;
                break;
            case "--include-excluded":
                includeExcluded = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.install.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if ((source === undefined) === (query === undefined)) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.install.sourceMode"));
    }
    if (target === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.install.missingTarget"));
    }
    return {
        action: "install",
        ...(source === undefined ? {} : { source }),
        ...(query === undefined ? {} : { query }),
        target,
        overwrite,
        pluginScope,
        ...(includeExcluded ? { includeExcluded } : {}),
    };
}
function parseSkillCreateArgs(args, locale) {
    let prompt;
    let applyDraftPath;
    let target = "all";
    let targetSpecified = false;
    let saveDraft = false;
    for (let index = 0; index < args.length;) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--prompt":
                prompt = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyFreeformTextArg)(token, optionValue, locale));
                index += 2;
                break;
            case "--apply-draft":
                applyDraftPath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 2;
                break;
            case "--for":
                if (targetSpecified) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.create.duplicateTarget"));
                }
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale, "cli.parser.skill.create.invalidTarget"));
                targetSpecified = true;
                index += 2;
                break;
            case "--save-draft":
                saveDraft = true;
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.create.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (applyDraftPath !== undefined) {
        if (prompt !== undefined || targetSpecified || saveDraft) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.create.applyDraftMode"));
        }
        return {
            action: "create",
            mode: "apply-draft",
            applyDraftPath,
        };
    }
    if (prompt === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.create.missingPrompt"));
    }
    return {
        action: "create",
        mode: "prompt",
        prompt,
        target,
        ...(saveDraft ? { saveDraft: true } : {}),
    };
}
function parseSkillCatalogArgs(args, locale) {
    const [catalogAction, ...rest] = args;
    if (!catalogAction) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.usage"));
    }
    switch (catalogAction) {
        case "list":
            return parseSkillCatalogListArgs(rest, locale);
        case "add":
            return parseSkillCatalogAddArgs(rest, locale);
        case "import-github":
            return parseSkillCatalogImportGithubArgs(rest, locale);
        case "remove":
            return parseSkillCatalogRemoveArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.unknownCommand", {
                action: catalogAction,
            }));
    }
}
function parseSkillCatalogListArgs(args, locale) {
    let includeSystem = false;
    for (const token of args) {
        switch (token) {
            case "--include-system":
                includeSystem = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.list.unknownOption", {
                    token,
                }));
        }
    }
    return { action: "catalog", catalogAction: "list", includeSystem };
}
function parseSkillCatalogAddArgs(args, locale) {
    let id;
    let description;
    let defaultSource;
    let codexSource;
    let claudeSource;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--id":
                id = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--description":
                description = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--source":
                defaultSource = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--codex-source":
                codexSource = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--claude-source":
                claudeSource = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.add.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (id === undefined || description === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.add.usage"));
    }
    if (defaultSource === undefined &&
        codexSource === undefined &&
        claudeSource === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.add.sourceMode"));
    }
    return {
        action: "catalog",
        catalogAction: "add",
        id,
        description,
        ...(defaultSource === undefined ? {} : { defaultSource }),
        ...(codexSource === undefined ? {} : { codexSource }),
        ...(claudeSource === undefined ? {} : { claudeSource }),
    };
}
function parseSkillCatalogImportGithubArgs(args, locale) {
    let repo;
    let path;
    let ref = "main";
    let target = "all";
    let overwrite = false;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--repo":
                repo = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--path":
                path = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--ref":
                ref = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--for":
                target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseSkillWorkerOrAllTarget((0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale), locale, "cli.parser.skill.catalog.importGithub.invalidTarget"));
                index += 1;
                break;
            case "--overwrite":
                overwrite = true;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.importGithub.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (repo === undefined || path === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.importGithub.usage"));
    }
    return {
        action: "catalog",
        catalogAction: "import-github",
        repo,
        path,
        ref,
        target,
        overwrite,
    };
}
function parseSkillCatalogRemoveArgs(args, locale) {
    let id;
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--id":
                id = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.remove.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (id === undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.catalog.remove.usage"));
    }
    return { action: "catalog", catalogAction: "remove", id };
}
function parseSkillListTarget(value, locale) {
    return parseSkillWorkerTarget(value, locale, "cli.parser.skill.list.invalidTarget");
}
function parseSkillWorkerOrAllTarget(value, locale, invalidTargetKey) {
    if (value === "all") {
        return value;
    }
    return parseSkillWorkerTarget(value, locale, invalidTargetKey);
}
function parseSkillInstallTarget(value, locale) {
    return parseSkillWorkerTarget(value, locale, "cli.parser.skill.install.invalidTarget");
}
function parseSkillWorkerTarget(value, locale, invalidTargetKey) {
    if (value !== "all" &&
        value !== "default" &&
        ((0, control_plane_config_1.isBuiltInWorkerName)(value) ||
            control_plane_config_1.customWorkerIdSchema.safeParse(value).success)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, invalidTargetKey, { value }));
}
function parseSkillInstallPluginScope(value, locale) {
    if (value === "project" || value === "user" || value === "local") {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.skill.install.invalidPluginScope", {
        value,
    }));
}
