"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WORKFLOW_VISIBLE_UPDATE_COMMENT_EVENT_ENABLED = exports.WORKFLOW_VISIBLE_EVENTS = exports.WORKFLOW_NEEDS_HUMAN_REASONS = void 0;
exports.isWorkflowNeedsHumanReason = isWorkflowNeedsHumanReason;
exports.isWorkflowVisibleEvent = isWorkflowVisibleEvent;
exports.renderWorkflowVisibleEventValue = renderWorkflowVisibleEventValue;
exports.normalizeWorkflowVisibleEventValue = normalizeWorkflowVisibleEventValue;
exports.renderWorkflowNeedsHumanReasonValue = renderWorkflowNeedsHumanReasonValue;
exports.normalizeWorkflowNeedsHumanReasonValue = normalizeWorkflowNeedsHumanReasonValue;
exports.renderWorkerOutcomeValue = renderWorkerOutcomeValue;
exports.normalizeWorkerOutcomeValue = normalizeWorkerOutcomeValue;
const catalog_1 = require("../i18n/catalog");
exports.WORKFLOW_NEEDS_HUMAN_REASONS = {
    reviewReady: "review_ready",
    question: "question",
    timeout: "timeout",
    runLimit: "run_limit",
    assigneeResolution: "assignee_resolution",
    foreignApproval: "foreign_approval",
    startApproval: "start_approval",
    closeFailed: "close_failed",
    systemError: "system_error",
};
exports.WORKFLOW_VISIBLE_EVENTS = {
    workflowStarted: "workflow_started",
    issueCreated: "issue_created",
    childIssueCreated: "child_issue_created",
    childIssueReused: "child_issue_reused",
    childIssueSkipped: "child_issue_skipped",
    replanQueued: "replan_queued",
    prHandoff: "pr_handoff",
    issueReviewReady: "issue_review_ready",
    prReviewReady: "pr_review_ready",
    prReviewSkipped: "pr_review_skipped",
    autoMergePending: "auto_merge_pending",
    dependencyIntegrated: "dependency_integrated",
    humanAttentionRequested: "human_attention_requested",
    workflowCompleted: "workflow_completed",
    workflowFailed: "workflow_failed",
};
exports.WORKFLOW_VISIBLE_UPDATE_COMMENT_EVENT_ENABLED = {
    [exports.WORKFLOW_VISIBLE_EVENTS.workflowStarted]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.issueCreated]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.childIssueCreated]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.childIssueReused]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.childIssueSkipped]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.replanQueued]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.prHandoff]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.issueReviewReady]: true,
    [exports.WORKFLOW_VISIBLE_EVENTS.prReviewReady]: true,
    [exports.WORKFLOW_VISIBLE_EVENTS.prReviewSkipped]: true,
    [exports.WORKFLOW_VISIBLE_EVENTS.autoMergePending]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.dependencyIntegrated]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.humanAttentionRequested]: true,
    [exports.WORKFLOW_VISIBLE_EVENTS.workflowCompleted]: false,
    [exports.WORKFLOW_VISIBLE_EVENTS.workflowFailed]: true,
};
const WORKFLOW_VISIBLE_EVENT_VALUES = Object.values(exports.WORKFLOW_VISIBLE_EVENTS);
const WORKFLOW_NEEDS_HUMAN_REASON_VALUES = Object.values(exports.WORKFLOW_NEEDS_HUMAN_REASONS);
function isWorkflowNeedsHumanReason(value) {
    return (typeof value === "string" &&
        WORKFLOW_NEEDS_HUMAN_REASON_VALUES.includes(value));
}
function isWorkflowVisibleEvent(value) {
    return (typeof value === "string" &&
        WORKFLOW_VISIBLE_EVENT_VALUES.includes(value));
}
function renderWorkflowVisibleEventValue(event, language) {
    return (0, catalog_1.translate)(language, `workflowUpdate.metadata.event.${event}`);
}
function normalizeWorkflowVisibleEventValue(value) {
    return normalizeLocalizedValue(value, WORKFLOW_VISIBLE_EVENT_VALUES, (event) => `workflowUpdate.metadata.event.${event}`);
}
function renderWorkflowNeedsHumanReasonValue(reason, language) {
    return (0, catalog_1.translate)(language, `workflowUpdate.metadata.needsHumanReason.${reason}`);
}
function normalizeWorkflowNeedsHumanReasonValue(value) {
    return normalizeLocalizedValue(value, WORKFLOW_NEEDS_HUMAN_REASON_VALUES, (reason) => `workflowUpdate.metadata.needsHumanReason.${reason}`);
}
function renderWorkerOutcomeValue(outcome, language, terminalSuccess = false) {
    switch (outcome) {
        case "proceed":
            if (terminalSuccess) {
                return (0, catalog_1.translate)(language, "workflowUpdate.metadata.outcomeComplete");
            }
            return (0, catalog_1.translate)(language, "workflowUpdate.metadata.outcomeProceed");
        case "wait_human":
            return (0, catalog_1.translate)(language, "workflowUpdate.metadata.outcomeWaitHuman");
        case "fail":
            return (0, catalog_1.translate)(language, "workflowUpdate.metadata.outcomeFail");
    }
}
function normalizeWorkerOutcomeValue(value) {
    if (value === undefined) {
        return null;
    }
    const normalized = value.trim();
    if (normalized.length === 0) {
        return null;
    }
    if ((0, catalog_1.translateAllLocales)("workflowUpdate.metadata.outcomeProceed").includes(normalized)) {
        return { workerOutcome: "proceed", terminalSuccess: false };
    }
    if ((0, catalog_1.translateAllLocales)("workflowUpdate.metadata.outcomeComplete").includes(normalized)) {
        return { workerOutcome: "proceed", terminalSuccess: true };
    }
    if ((0, catalog_1.translateAllLocales)("workflowUpdate.metadata.outcomeWaitHuman").includes(normalized)) {
        return { workerOutcome: "wait_human", terminalSuccess: false };
    }
    if ((0, catalog_1.translateAllLocales)("workflowUpdate.metadata.outcomeFail").includes(normalized)) {
        return { workerOutcome: "fail", terminalSuccess: false };
    }
    return null;
}
function normalizeLocalizedValue(value, candidates, keyForValue) {
    if (value === undefined) {
        return null;
    }
    const normalized = value.trim();
    if (normalized.length === 0) {
        return null;
    }
    for (const candidate of candidates) {
        if ((0, catalog_1.translateAllLocales)(keyForValue(candidate)).includes(normalized)) {
            return candidate;
        }
    }
    return null;
}
