"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseWorkflowArgs = parseWorkflowArgs;
const catalog_1 = require("../../i18n/catalog");
const types_1 = require("../../workflows/types");
const shared_1 = require("./shared");
function parseWorkflowArgs(args, locale = catalog_1.DEFAULT_APP_LOCALE) {
    const [action, ...rest] = args;
    if (!action) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.usage"));
    }
    switch (action) {
        case "create":
            return parseWorkflowCreateArgs(rest, locale);
        case "update":
            return parseWorkflowUpdateArgs(rest, locale);
        case "delete":
            return parseWorkflowDeleteArgs(rest, locale);
        case "lint":
            return parseWorkflowLintArgs(rest, locale);
        case "list":
            return parseWorkflowListArgs(rest, locale);
        default:
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.unknownCommand", { action }));
    }
}
function parseWorkflowCreateArgs(args, locale) {
    const values = parseWorkflowCreateOptionValues(args, locale);
    const applyDraftArgs = buildWorkflowCreateApplyDraftArgs(values, locale);
    if (applyDraftArgs !== undefined) {
        return applyDraftArgs;
    }
    return buildWorkflowCreatePromptOrManualArgs(values, locale);
}
function parseWorkflowCreateOptionValues(args, locale) {
    const values = {
        requiredSkills: [],
        requiredMcp: [],
        targets: [],
        autoSelect: true,
        templateSet: false,
        saveDraft: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--prompt":
                values.prompt = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyFreeformTextArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--path":
                values.workflowTemplatePath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--description":
                values.description = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => optionValue);
                index += 1;
                break;
            case "--required-skill":
                values.requiredSkills.push((0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale)));
                index += 1;
                break;
            case "--required-mcp":
                values.requiredMcp.push((0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale)));
                index += 1;
                break;
            case "--target":
                values.targets.push((0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseWorkflowTargetArg(optionValue, locale)));
                index += 1;
                break;
            case "--no-auto-select":
                values.autoSelect = false;
                break;
            case "--body":
                assignWorkflowCreateBody(values, {
                    kind: "text",
                    text: (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyFreeformTextArg)(token, optionValue, locale)),
                }, locale);
                index += 1;
                break;
            case "--body-file":
                assignWorkflowCreateBody(values, {
                    kind: "file",
                    path: (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale)),
                }, locale);
                index += 1;
                break;
            case "--apply-draft":
                values.applyDraftPath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--template-set":
                values.templateSet = true;
                break;
            case "--save-draft":
                values.saveDraft = true;
                break;
            default: {
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.unknownOption", {
                    token: token ?? "",
                }));
            }
        }
    }
    return values;
}
function buildWorkflowCreateApplyDraftArgs(values, locale) {
    if (values.applyDraftPath !== undefined) {
        if (values.prompt !== undefined ||
            values.workflowTemplatePath !== undefined ||
            values.description !== undefined ||
            values.requiredSkills.length > 0 ||
            values.requiredMcp.length > 0 ||
            values.targets.length > 0 ||
            values.autoSelect === false ||
            values.body !== undefined ||
            values.templateSet) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.applyDraftInputError"));
        }
        if (values.saveDraft) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.applyDraftOutputError"));
        }
        return {
            action: "create",
            mode: "apply-draft",
            applyDraftPath: values.applyDraftPath,
        };
    }
    return undefined;
}
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: Workflow create parsing keeps prompt/manual mode conflicts explicit at the CLI boundary.
function buildWorkflowCreatePromptOrManualArgs(values, locale) {
    if (values.prompt !== undefined) {
        if (values.templateSet &&
            (values.workflowTemplatePath !== undefined ||
                values.description !== undefined ||
                values.requiredSkills.length > 0 ||
                values.requiredMcp.length > 0 ||
                values.targets.length > 0 ||
                values.autoSelect === false ||
                values.body !== undefined)) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.templateSetError"));
        }
        if (values.body !== undefined) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.modeError"));
        }
        return {
            action: "create",
            mode: "prompt",
            prompt: values.prompt,
            ...(values.templateSet ? { templateSet: true } : {}),
            ...(values.saveDraft ? { saveDraft: true } : {}),
            ...(values.workflowTemplatePath !== undefined
                ? { workflowTemplatePath: values.workflowTemplatePath }
                : {}),
            ...(values.description !== undefined
                ? { description: values.description }
                : {}),
            ...(values.requiredSkills.length > 0
                ? { requiredSkills: values.requiredSkills }
                : {}),
            ...(values.requiredMcp.length > 0
                ? { requiredMcp: values.requiredMcp }
                : {}),
            ...(values.targets.length > 0 ? { targets: values.targets } : {}),
            ...(values.autoSelect === false ? { autoSelect: false } : {}),
        };
    }
    if (values.templateSet) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.templateSetError"));
    }
    return {
        action: "create",
        mode: "manual",
        ...(values.saveDraft ? { saveDraft: true } : {}),
        workflowTemplatePath: (0, shared_1.requireParsedOption)(values.workflowTemplatePath, "--path", locale),
        ...(values.description !== undefined
            ? { description: values.description }
            : {}),
        ...(values.requiredSkills.length > 0
            ? { requiredSkills: values.requiredSkills }
            : {}),
        ...(values.requiredMcp.length > 0
            ? { requiredMcp: values.requiredMcp }
            : {}),
        ...(values.targets.length > 0 ? { targets: values.targets } : {}),
        ...(values.autoSelect === false ? { autoSelect: false } : {}),
        ...(values.body !== undefined ? { body: values.body } : {}),
    };
}
function parseWorkflowTargetArg(value, locale) {
    if (types_1.WORKFLOW_TEMPLATE_TARGETS.includes(value)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.list.invalidTarget", { value }));
}
function parseWorkflowUpdateArgs(args, locale) {
    const values = {
        templateSet: false,
        saveDraft: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--path":
                values.workflowTemplatePath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--prompt":
                values.prompt = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyFreeformTextArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--apply-draft":
                values.applyDraftPath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--template-set":
                values.templateSet = true;
                break;
            case "--save-draft":
                values.saveDraft = true;
                break;
            default: {
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.update.unknownOption", {
                    token: token ?? "",
                }));
            }
        }
    }
    if (values.applyDraftPath !== undefined) {
        if (values.workflowTemplatePath !== undefined ||
            values.prompt !== undefined ||
            values.templateSet) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.update.applyDraftInputError"));
        }
        if (values.saveDraft) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.update.applyDraftOutputError"));
        }
        return {
            action: "update",
            mode: "apply-draft",
            applyDraftPath: values.applyDraftPath,
        };
    }
    return {
        action: "update",
        mode: "prompt",
        prompt: (0, shared_1.requireParsedOption)(values.prompt, "--prompt", locale),
        ...(values.workflowTemplatePath !== undefined
            ? { workflowTemplatePath: values.workflowTemplatePath }
            : {}),
        ...(values.templateSet ? { templateSet: true } : {}),
        ...(values.saveDraft ? { saveDraft: true } : {}),
    };
}
function parseWorkflowDeleteArgs(args, locale) {
    const values = {
        cleanupReferences: false,
        dryRun: false,
        saveDraft: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--path":
                values.workflowTemplatePath = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--cleanup-references":
                values.cleanupReferences = true;
                break;
            case "--dry-run":
                values.dryRun = true;
                break;
            case "--save-draft":
                values.saveDraft = true;
                break;
            case "--apply-draft":
                values.applyDraftPath = (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.workflow.delete.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (values.applyDraftPath !== undefined) {
        if (values.workflowTemplatePath !== undefined ||
            values.cleanupReferences ||
            values.dryRun) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.delete.applyDraftInputError"));
        }
        if (values.saveDraft) {
            throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.delete.applyDraftOutputError"));
        }
        return {
            action: "delete",
            mode: "apply-draft",
            applyDraftPath: values.applyDraftPath,
        };
    }
    if (values.cleanupReferences && !values.saveDraft) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.delete.cleanupDraftError"));
    }
    if (values.cleanupReferences && values.dryRun) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.delete.cleanupDryRunError"));
    }
    if (values.dryRun && values.saveDraft) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.delete.dryRunSaveDraftError"));
    }
    return {
        action: "delete",
        mode: "delete",
        workflowTemplatePath: (0, shared_1.requireParsedOption)(values.workflowTemplatePath, "--path", locale),
        cleanupReferences: values.cleanupReferences,
        dryRun: values.dryRun,
        saveDraft: values.saveDraft,
    };
}
function parseWorkflowLintArgs(args, locale) {
    const values = { action: "lint" };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        switch (token) {
            case "--path": {
                const workflowTemplatePath = args[index + 1];
                if (!workflowTemplatePath) {
                    throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.lint.missingPathValue"));
                }
                values.workflowTemplatePath = workflowTemplatePath;
                index += 1;
                break;
            }
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.lint.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    return values;
}
function parseWorkflowListArgs(args, locale) {
    const values = {
        action: "list",
        format: "text",
        includeSystem: false,
    };
    for (let index = 0; index < args.length; index += 1) {
        const token = args[index];
        const value = args[index + 1];
        switch (token) {
            case "--include-system":
                values.includeSystem = true;
                break;
            case "--target":
                values.target = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseWorkflowListTarget(optionValue, locale));
                index += 1;
                break;
            case "--references-to":
                values.referencesTo = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => (0, shared_1.parseNonEmptyStringArg)(token, optionValue, locale));
                index += 1;
                break;
            case "--format":
                values.format = (0, shared_1.parseRequiredOptionValue)(token, value, locale, (optionValue) => parseWorkflowListFormat(optionValue, locale));
                index += 1;
                break;
            default:
                throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.list.unknownOption", {
                    token: token ?? "",
                }));
        }
    }
    if (values.referencesTo !== undefined && values.target !== undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.list.referencesTargetError"));
    }
    return values;
}
function parseWorkflowListTarget(value, locale) {
    if (types_1.WORKFLOW_TEMPLATE_TARGETS.includes(value)) {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.list.invalidTarget", { value }));
}
function parseWorkflowListFormat(value, locale) {
    if (value === "text" || value === "json") {
        return value;
    }
    throw new Error((0, catalog_1.translate)(locale, "cli.parser.invalidFormatValue", { value }));
}
function assignWorkflowCreateBody(values, body, locale) {
    if (values.body !== undefined) {
        throw new Error((0, catalog_1.translate)(locale, "cli.parser.workflow.create.bodyError"));
    }
    values.body = body;
}
