"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.formatConfigOverrideDisplayEntries = formatConfigOverrideDisplayEntries;
const issue_completion_1 = require("../config/issue-completion");
const CONFIG_OVERRIDE_DISPLAY_LABELS = new Map([
    ["issueCompletion.target", "Issue completion target"],
    ["issueCompletion.pullRequest.reviewPolicy", "PR review policy"],
    ["issueCompletion.pullRequest.mergePolicy", "PR merge policy"],
    ["aiRunLimits.maxRuns", "AI run count limit"],
    ["aiRunLimits.maxHours", "AI run hours limit"],
    ["recurring.maxActiveChildren", "Recurring active child limit"],
    ["followup.maxIssuesPerSource.mainWork", "Follow-up mainWork issue cap"],
    [
        "followup.maxIssuesPerSource.maintenance",
        "Follow-up maintenance issue cap",
    ],
    ["repositoryHost.pullRequest.defaultReviewers", "PR default reviewers"],
    ["workers.default", "Default worker"],
    ["workers.thinking", "Thinking worker"],
    ["approvals.requirementsSetup.skills", "Required skill setup"],
    ["approvals.requirementsSetup.mcp", "Required MCP setup"],
]);
function formatConfigOverrideDisplayEntries(patch) {
    if (patch === null || patch.entries.length === 0) {
        return [];
    }
    return patch.entries.map((entry) => formatConfigOverrideEntry(entry));
}
function formatConfigOverrideEntry(entry) {
    if (entry.path === "aiRunLimits.maxHours") {
        return formatAiMaxHoursEntry(entry);
    }
    if (entry.path === "workers.default" || entry.path === "workers.thinking") {
        return formatWorkerEntry(entry);
    }
    if (entry.op === "set") {
        return `${formatConfigOverridePath(entry.path)}: set ${formatConfigOverrideValue(entry.value)}`;
    }
    if (entry.op === "inherit") {
        return `${formatConfigOverridePath(entry.path)}: default`;
    }
    return `${formatConfigOverridePath(entry.path)}: ${entry.op}`;
}
function formatWorkerEntry(entry) {
    if (entry.op === "inherit") {
        return `${formatConfigOverridePath(entry.path)}: default`;
    }
    if (entry.op === "set") {
        return `${formatConfigOverridePath(entry.path)}: ${formatWorkerValue(entry.value)}`;
    }
    throw new Error(`Unsupported worker override for display: ${JSON.stringify(entry)}`);
}
function formatAiMaxHoursEntry(entry) {
    switch (entry.op) {
        case "disable":
            return "AI run hours limit: off";
        case "inherit":
            return "AI run hours limit: default";
        case "set":
            return `AI run hours limit: ${formatConfigOverrideValue(entry.value)}`;
    }
}
function formatConfigOverrideValue(value) {
    if ((0, issue_completion_1.isIssueCompletionTarget)(value)) {
        return (0, issue_completion_1.formatIssueCompletionTargetForCli)(value);
    }
    if ((0, issue_completion_1.isPullRequestReviewPolicy)(value)) {
        return (0, issue_completion_1.formatPullRequestReviewPolicyForCli)(value);
    }
    if ((0, issue_completion_1.isPullRequestMergePolicy)(value)) {
        return (0, issue_completion_1.formatPullRequestMergePolicyForCli)(value);
    }
    if (typeof value === "string") {
        return value;
    }
    return JSON.stringify(value) ?? String(value);
}
function formatWorkerValue(value) {
    return Array.isArray(value) && value.length === 1
        ? formatConfigOverrideValue(value[0])
        : formatConfigOverrideValue(value);
}
function formatConfigOverridePath(path) {
    return CONFIG_OVERRIDE_DISPLAY_LABELS.get(path) ?? path;
}
