"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_WORKFLOW_FOLLOWUP_MAX_ISSUES_PER_SOURCE = exports.WORKFLOW_FOLLOWUP_RECOMMENDATION_CATEGORIES = exports.WORKFLOW_COOLDOWN_REASON_VALUES = exports.WORKFLOW_PENDING_PLANNING_SOURCE_VALUES = exports.WORKFLOW_PENDING_PLANNING_INTENT_VALUES = exports.WORKFLOW_START_APPROVAL_INTENT_VALUES = exports.FOLLOWUP_ITEM_PROPOSAL_APPROVALS = exports.FOLLOWUP_PROPOSAL_APPROVALS = exports.FOLLOWUP_PROPOSAL_POLICIES = exports.WORKFLOW_TEMPLATE_UPDATE_MODES = exports.WORKFLOW_SCHEDULE_MODES = exports.WORKFLOW_FINAL_RESULT_OUTCOMES = exports.WORKFLOW_RESULT_OUTCOMES = exports.WORKFLOW_TEMPLATE_TARGETS = exports.WORKFLOW_SURFACES = exports.WORKFLOW_STATE_VERSION = exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL = exports.WORKFLOW_TEMPLATE_AUTO_SELECT_SENTINEL = exports.WORKFLOW_TEMPLATE_AUTO_SENTINEL = exports.CONFIG_OVERRIDE_INHERIT_SENTINEL = exports.WORKFLOW_TEMPLATE_ROOT_DIRECTORY_NAME = exports.WORKFLOW_TEMPLATE_DIRECTORY_NAME = exports.WORKFLOW_TEMPLATE_FILE_SUFFIX = void 0;
exports.createAutoSelectWorkflowTemplateSelector = createAutoSelectWorkflowTemplateSelector;
exports.createNoTemplateWorkflowTemplateSelector = createNoTemplateWorkflowTemplateSelector;
exports.isReservedWorkflowTemplateSelectorValue = isReservedWorkflowTemplateSelectorValue;
exports.normalizeWorkflowTemplatePath = normalizeWorkflowTemplatePath;
exports.normalizeFollowupWorkflowTemplateCandidate = normalizeFollowupWorkflowTemplateCandidate;
exports.parseFollowupWorkflowTemplateCandidateSelector = parseFollowupWorkflowTemplateCandidateSelector;
exports.createOffWorkflowScheduleState = createOffWorkflowScheduleState;
exports.createOffWorkflowScheduledRunCommand = createOffWorkflowScheduledRunCommand;
exports.isWorkflowChildIssueStep = isWorkflowChildIssueStep;
exports.normalizeWorkflowOptionalResultFields = normalizeWorkflowOptionalResultFields;
exports.buildWorkflowResultFieldsWithDefaults = buildWorkflowResultFieldsWithDefaults;
exports.buildWorkflowResultWithDefaults = buildWorkflowResultWithDefaults;
exports.buildWorkflowFinalResultWithDefaults = buildWorkflowFinalResultWithDefaults;
exports.createOffWorkflowFollowupState = createOffWorkflowFollowupState;
const node_path_1 = __importDefault(require("node:path"));
const branding_1 = require("../core/branding");
exports.WORKFLOW_TEMPLATE_FILE_SUFFIX = ".md";
exports.WORKFLOW_TEMPLATE_DIRECTORY_NAME = "workflows";
exports.WORKFLOW_TEMPLATE_ROOT_DIRECTORY_NAME = branding_1.REPO_HOME_DIRECTORY_NAME;
exports.CONFIG_OVERRIDE_INHERIT_SENTINEL = "default";
exports.WORKFLOW_TEMPLATE_AUTO_SENTINEL = "auto";
exports.WORKFLOW_TEMPLATE_AUTO_SELECT_SENTINEL = "auto_select";
exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL = "no_template";
exports.WORKFLOW_STATE_VERSION = 20;
exports.WORKFLOW_SURFACES = [
    "issue",
    "pull_request",
    "unplanned",
    "merge",
    "followup",
];
exports.WORKFLOW_TEMPLATE_TARGETS = [
    "issue",
    "pull_request",
    "unplanned",
    "followup",
];
exports.WORKFLOW_RESULT_OUTCOMES = [
    "proceed",
    "wait_human",
    "fail",
];
exports.WORKFLOW_FINAL_RESULT_OUTCOMES = [
    "proceed",
    "wait_human",
    "fail",
];
exports.WORKFLOW_SCHEDULE_MODES = ["off", "every", "cron"];
exports.WORKFLOW_TEMPLATE_UPDATE_MODES = [
    "flexible",
    "strict",
    "disabled",
];
exports.FOLLOWUP_PROPOSAL_POLICIES = [
    "off",
    "if_needed",
    "always",
];
exports.FOLLOWUP_PROPOSAL_APPROVALS = [
    "auto",
    "confirm",
    "confirm_if_needed",
];
exports.FOLLOWUP_ITEM_PROPOSAL_APPROVALS = ["auto", "confirm"];
exports.WORKFLOW_START_APPROVAL_INTENT_VALUES = [
    "new_plan",
    "replan",
];
exports.WORKFLOW_PENDING_PLANNING_INTENT_VALUES = [
    "revise_plan",
    "replan",
];
exports.WORKFLOW_PENDING_PLANNING_SOURCE_VALUES = [
    "dependency_completion",
];
function createAutoSelectWorkflowTemplateSelector() {
    return { mode: "auto_select" };
}
function createNoTemplateWorkflowTemplateSelector() {
    return { mode: "no_template" };
}
function isReservedWorkflowTemplateSelectorValue(value) {
    return (value === exports.CONFIG_OVERRIDE_INHERIT_SENTINEL ||
        value === exports.WORKFLOW_TEMPLATE_AUTO_SENTINEL ||
        value === exports.WORKFLOW_TEMPLATE_AUTO_SELECT_SENTINEL ||
        value === exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL);
}
function normalizeWorkflowTemplatePath(selector) {
    const trimmed = selector.trim();
    if (trimmed.length === 0 ||
        isReservedWorkflowTemplateSelectorValue(trimmed)) {
        return null;
    }
    if (trimmed.includes("\\") || node_path_1.default.isAbsolute(trimmed)) {
        return null;
    }
    if (/\s/u.test(trimmed)) {
        return null;
    }
    const normalized = node_path_1.default.posix.normalize(trimmed);
    if (normalized.length === 0 ||
        normalized === "." ||
        normalized !== trimmed ||
        normalized.startsWith("../") ||
        normalized.includes("/../") ||
        normalized.split("/").some((segment) => {
            return segment === "." || segment === ".." || segment.length === 0;
        }) ||
        !normalized.endsWith(exports.WORKFLOW_TEMPLATE_FILE_SUFFIX)) {
        return null;
    }
    return normalized;
}
function normalizeFollowupWorkflowTemplateCandidate(value) {
    const trimmed = value.trim();
    if (trimmed === exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL) {
        return exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL;
    }
    return normalizeWorkflowTemplatePath(trimmed);
}
function parseFollowupWorkflowTemplateCandidateSelector(value) {
    const normalized = normalizeFollowupWorkflowTemplateCandidate(value);
    if (normalized === null) {
        return null;
    }
    if (normalized === exports.WORKFLOW_TEMPLATE_NO_TEMPLATE_SENTINEL) {
        return { mode: "no_template" };
    }
    return { mode: "template", templatePath: normalized };
}
function createOffWorkflowScheduleState() {
    return { mode: "off", value: null };
}
exports.WORKFLOW_COOLDOWN_REASON_VALUES = [
    "quota_exhausted",
    "capacity",
];
function createOffWorkflowScheduledRunCommand() {
    return { mode: "off", value: null };
}
function isWorkflowChildIssueStep(step) {
    return step?.kind === "child_issue";
}
function normalizeWorkflowOptionalResultFields(fields) {
    return {
        evidence: fields.evidence ?? [],
        humanComment: fields.humanComment ?? null,
    };
}
function buildWorkflowResultFieldsWithDefaults(fields) {
    return {
        resultSummary: fields.resultSummary,
        ...normalizeWorkflowOptionalResultFields(fields),
    };
}
function buildWorkflowResultWithDefaults(result) {
    return {
        outcome: result.outcome,
        ...buildWorkflowResultFieldsWithDefaults(result),
    };
}
function buildWorkflowFinalResultWithDefaults(result) {
    return {
        outcome: result.outcome,
        ...buildWorkflowResultFieldsWithDefaults(result),
    };
}
exports.WORKFLOW_FOLLOWUP_RECOMMENDATION_CATEGORIES = [
    "mainWork",
    "maintenance",
];
exports.DEFAULT_WORKFLOW_FOLLOWUP_MAX_ISSUES_PER_SOURCE = {
    mainWork: 2,
    maintenance: 2,
};
function createOffWorkflowFollowupState() {
    return { proposalPolicy: "off", proposalApproval: "auto" };
}
