"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ALLOWED_MANUAL_WORKFLOW_EXCEPTIONS = void 0;
exports.buildCliWorkflowProvenance = buildCliWorkflowProvenance;
exports.expectedWorkflowCommandPrefixes = expectedWorkflowCommandPrefixes;
exports.isAllowedManualWorkflowException = isAllowedManualWorkflowException;
exports.inspectPlanningWorkflowProvenance = inspectPlanningWorkflowProvenance;
exports.projectWorkflowManualExceptionField = projectWorkflowManualExceptionField;
exports.ALLOWED_MANUAL_WORKFLOW_EXCEPTIONS = [
    "unsupported by CLI",
    "CLI failed",
    "manual recovery after scaffold verification"
];
const BASIC_WORK_CREATION_COMMAND_PREFIXES = [
    "day-shift work create",
    "node dist/cli/index.js work create"
];
const STRUCTURED_TASK_CREATION_COMMAND_PREFIXES = [
    "day-shift work task create",
    "node dist/cli/index.js work task create"
];
function buildCliWorkflowProvenance(command) {
    return {
        workflowProvenance: "cli",
        workflowCliCommand: command
    };
}
function expectedWorkflowCommandPrefixes(artifactType) {
    switch (artifactType) {
        case "slice_overview":
            return [
                "day-shift slice new",
                "day-shift slice update",
                "node dist/cli/index.js slice new",
                "node dist/cli/index.js slice update"
            ];
        case "phase_overview":
            return [
                "day-shift phase new",
                "day-shift phase update",
                "node dist/cli/index.js phase new",
                "node dist/cli/index.js phase update"
            ];
        case "milestone_overview":
            return [
                "day-shift milestone new",
                "day-shift milestone update",
                "node dist/cli/index.js milestone new",
                "node dist/cli/index.js milestone update"
            ];
        case "task_definition":
            return [
                "day-shift task new",
                "day-shift task promote",
                "day-shift task update",
                ...BASIC_WORK_CREATION_COMMAND_PREFIXES,
                ...STRUCTURED_TASK_CREATION_COMMAND_PREFIXES,
                "node dist/cli/index.js task new",
                "node dist/cli/index.js task promote",
                "node dist/cli/index.js task update"
            ];
        case "implementation_summary":
            return [
                "day-shift task new",
                "day-shift task promote",
                "day-shift implementation-summary build",
                "day-shift implementation-summary update",
                ...BASIC_WORK_CREATION_COMMAND_PREFIXES,
                ...STRUCTURED_TASK_CREATION_COMMAND_PREFIXES,
                "node dist/cli/index.js task new",
                "node dist/cli/index.js task promote",
                "node dist/cli/index.js implementation-summary build",
                "node dist/cli/index.js implementation-summary update"
            ];
        case "reconciliation":
            return [
                "day-shift milestone new",
                "day-shift reconciliation build",
                "day-shift reconciliation update",
                "node dist/cli/index.js milestone new",
                "node dist/cli/index.js reconciliation build",
                "node dist/cli/index.js reconciliation update"
            ];
        default:
            return [];
    }
}
function isAllowedManualWorkflowException(value) {
    return exports.ALLOWED_MANUAL_WORKFLOW_EXCEPTIONS.includes(value);
}
function inspectPlanningWorkflowProvenance(options) {
    const provenance = options.workflowProvenance.trim();
    const command = options.workflowCliCommand.trim();
    const manualException = options.workflowManualException.trim();
    const expectedPrefixes = expectedWorkflowCommandPrefixes(options.artifactType);
    if (provenance === "") {
        return [{
                code: "PLANNING_WORKFLOW_PROVENANCE_MISSING",
                message: "Planning artifact is missing workflow provenance. Record CLI-first evidence or an allowed manual exception."
            }];
    }
    if (provenance === "cli") {
        if (command === "") {
            return [{
                    code: "PLANNING_WORKFLOW_CLI_COMMAND_MISSING",
                    message: "CLI-authored planning artifact is missing workflow_cli_command."
                }];
        }
        if (!expectedPrefixes.some((prefix) => command.startsWith(prefix))) {
            return [{
                    code: "PLANNING_WORKFLOW_CLI_COMMAND_INVALID",
                    message: `workflow_cli_command '${command}' does not match the expected command family for artifact_type '${options.artifactType}'.`
                }];
        }
        if (manualException !== "") {
            return [{
                    code: "PLANNING_WORKFLOW_EXCEPTION_UNEXPECTED",
                    message: "CLI-authored planning artifact must not record workflow_manual_exception."
                }];
        }
        return [];
    }
    if (provenance === "manual-exception") {
        if (command === "") {
            return [{
                    code: "PLANNING_WORKFLOW_CLI_COMMAND_MISSING",
                    message: "Manual-exception planning artifact must record the skipped CLI command in workflow_cli_command."
                }];
        }
        if (!expectedPrefixes.some((prefix) => command.startsWith(prefix))) {
            return [{
                    code: "PLANNING_WORKFLOW_CLI_COMMAND_INVALID",
                    message: `Skipped workflow_cli_command '${command}' does not match the expected command family for artifact_type '${options.artifactType}'.`
                }];
        }
        if (!isAllowedManualWorkflowException(manualException)) {
            return [{
                    code: "PLANNING_WORKFLOW_EXCEPTION_INVALID",
                    message: `workflow_manual_exception must be exactly one of: ${exports.ALLOWED_MANUAL_WORKFLOW_EXCEPTIONS.join(", ")}.`
                }];
        }
        return [];
    }
    if (options.allowLegacyManualAlias === true && provenance === "manual") {
        return [];
    }
    return [{
            code: "PLANNING_WORKFLOW_PROVENANCE_INVALID",
            message: "workflow_provenance must be 'cli' or 'manual-exception'."
        }];
}
function projectWorkflowManualExceptionField(options) {
    const issues = inspectPlanningWorkflowProvenance(options);
    if (issues.length > 0) {
        return {
            fieldLine: null,
            issues
        };
    }
    const workflowProvenance = options.workflowProvenance.trim();
    const workflowManualException = options.workflowManualException.trim();
    return {
        fieldLine: workflowProvenance === "manual-exception"
            ? `workflow_manual_exception: ${JSON.stringify(workflowManualException)}`
            : null,
        issues: []
    };
}
