"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildWorkerPullRequestReviewNecessityPrompt = buildWorkerPullRequestReviewNecessityPrompt;
const dedicated_prompt_support_1 = require("./dedicated-prompt-support");
const prompt_repair_1 = require("./prompt-repair");
const prompt_text_1 = require("./prompt-text");
const system_prompt_names_1 = require("./system-prompt-names");
const pull_request_review_necessity_contract_1 = require("./pull-request-review-necessity-contract");
function buildWorkerPullRequestReviewNecessityPrompt(workerName, input, repairContext) {
    const contract = (0, pull_request_review_necessity_contract_1.buildWorkerPullRequestReviewNecessityContract)(input);
    return (0, dedicated_prompt_support_1.renderDedicatedPromptEnvelope)({
        preambleLines: (0, dedicated_prompt_support_1.renderDedicatedPromptHeader)({
            workerName,
            runDescription: "This is a dedicated pull request review necessity decision run.",
            instructionLines: [
                "Read repository instructions in AGENTS.md and CLAUDE.md before acting.",
                (0, prompt_text_1.buildResponseLanguageInstruction)(input.outputLanguage),
                "Decide whether the pull request review workflow is worth running for this PR.",
                "Do not perform the PR review, propose code changes, inspect unrelated files, edit files, or run write commands.",
                "Use the PR metadata, changed files, commits, comments, linked source issue, and host automation state as evidence.",
                "Prefer skip_review only for low-risk changes where a separate AI review is unlikely to produce useful findings.",
                "Prefer run_review whenever the PR may change production behavior, automation, policies, security, data, dependencies, release process, or any area where review could materially reduce risk.",
            ],
            systemPrompts: input.systemPrompts,
            systemPromptNames: system_prompt_names_1.PULL_REQUEST_REVIEW_NECESSITY_SYSTEM_PROMPT_NAMES,
            allowSubagents: false,
        }),
        repairLines: repairContext
            ? (0, prompt_repair_1.renderPullRequestReviewNecessityRepairSectionLines)(repairContext)
            : undefined,
        inputLines: [
            "Pull request review necessity input:",
            ...(0, dedicated_prompt_support_1.renderDedicatedWorkspaceFields)(input),
            "- Effective policy:",
            "```json",
            JSON.stringify(input.effectivePolicy, null, 2),
            "```",
            "- Workflow state summary:",
            "```text",
            input.workflowStateSummary,
            "```",
            "- Pull request issue:",
            ...renderIssueSnapshot(input.issue),
            "- Linked source issue:",
            ...(input.sourceIssue === null
                ? [`  ${prompt_text_1.PROMPT_TEXT.none}`]
                : renderIssueSnapshot(input.sourceIssue)),
            "- Pull request metadata:",
            "```json",
            JSON.stringify(input.pullRequest, null, 2),
            "```",
            "- Host automation state:",
            "```json",
            JSON.stringify(input.automationState, null, 2),
            "```",
            "- Changed files:",
            "```json",
            JSON.stringify(input.changedFiles, null, 2),
            "```",
            "- Commits:",
            "```json",
            JSON.stringify(input.commits, null, 2),
            "```",
            "- Pull request comments and review comments:",
            ...renderComments(input.latestComments),
            "- Source issue comments:",
            ...renderComments(input.sourceComments),
        ],
        contract: {
            heading: "Pull request review necessity contract:",
            rules: contract.rules,
            promptSchema: contract.promptSchema,
            example: contract.example,
        },
    });
}
function renderIssueSnapshot(input) {
    return [
        `  - ID: ${input.displayId} (${input.id})`,
        `  - URL: ${input.url}`,
        `  - Title: ${input.title}`,
        "  - Body:",
        "```text",
        input.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ];
}
function renderComments(comments) {
    if (comments.length === 0) {
        return [`  ${prompt_text_1.PROMPT_TEXT.none}`];
    }
    return comments.flatMap((comment) => [
        `  - ${comment.createdAt} ${comment.author} (${comment.kind}):`,
        ...(comment.reviewState === undefined || comment.reviewState === null
            ? []
            : [`    reviewState: ${comment.reviewState}`]),
        ...(comment.path === undefined || comment.path === null
            ? []
            : [
                `    location: ${comment.path}${comment.line === undefined || comment.line === null
                    ? ""
                    : `:${String(comment.line)}`}`,
            ]),
        "```text",
        comment.body || prompt_text_1.PROMPT_TEXT.empty,
        "```",
    ]);
}
