"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSameIssueReplanPublishRequest = buildSameIssueReplanPublishRequest;
const catalog_1 = require("../../i18n/catalog");
const publisher_1 = require("../../workflows/publisher");
const publish_target_support_1 = require("../../workflows/publish-target-support");
const publisher_events_1 = require("../../workflows/publisher-events");
const replan_comment_1 = require("../../workflows/replan-comment");
const visible_update_builders_1 = require("../../workflows/visible-update-builders");
const workflow_projection_support_1 = require("../../workflows/workflow-projection-support");
const same_issue_replan_state_support_1 = require("./same-issue-replan-state-support");
function buildSameIssueReplanPublishRequest(input) {
    const { intent } = input;
    const target = (0, publish_target_support_1.resolveLinkedIssuePublishTarget)({
        issue: intent.issue,
        comments: input.issueComments,
        unlinkedPullRequestPolicy: "null",
    });
    if (target === null) {
        return null;
    }
    const workflowState = buildSameIssueReplanWorkflowState(intent);
    const visibleUpdate = resolveSameIssueReplanVisibleUpdate({
        intent,
        responseLanguage: input.responseLanguage,
    });
    return {
        target,
        workflowState,
        ...(shouldAppendSameIssueReplanCanonicalState(intent)
            ? { appendCanonicalWorkflowStateComment: true }
            : {}),
        publishPrecondition: intent.publishPrecondition,
        workflowProjection: (0, workflow_projection_support_1.buildWorkflowProjectionWithAuxiliaryLabels)({
            issueId: intent.issue.id,
            workflowLabels: input.workflowLabels,
            previous: intent.previousProjection,
            next: intent.nextProjection,
            previousWorkflowState: intent.previousWorkflowState,
            nextWorkflowState: workflowState,
            previousNeedsHumanReason: intent.previousNeedsHumanReason,
            nextNeedsHumanReason: intent.nextNeedsHumanReason,
            ...(intent.labelProjection !== undefined
                ? { labelProjection: intent.labelProjection }
                : {}),
        }),
        visibleUpdate,
        commentCursorPolicy: resolveSameIssueReplanCommentCursorPolicy(intent, visibleUpdate),
        responseLanguage: input.responseLanguage,
    };
}
function shouldAppendSameIssueReplanCanonicalState(intent) {
    switch (intent.kind) {
        case "service_implicit_replan":
        case "worker_replan_same_issue":
        case "run_limit_downgrade":
        case "preflight_template_drift":
        case "dependency_completion_requeue":
        case "revise_plan":
        case "replan_completion":
            return false;
    }
}
function buildSameIssueReplanWorkflowState(intent) {
    switch (intent.kind) {
        case "worker_replan_same_issue":
        case "run_limit_downgrade":
        case "dependency_completion_requeue":
        case "preflight_template_drift":
        case "replan_completion":
            return (0, same_issue_replan_state_support_1.deriveSameIssueReplanWorkflowState)({
                kind: intent.kind,
                previousWorkflowState: intent.previousWorkflowState,
                nextWorkflowState: intent.nextWorkflowState,
                reason: intent.kind === "replan_completion" ? null : intent.reason,
            });
        case "service_implicit_replan":
        case "revise_plan":
            return (0, same_issue_replan_state_support_1.deriveSameIssueReplanWorkflowState)({
                kind: intent.kind,
                previousWorkflowState: intent.previousWorkflowState,
                triggerCursor: intent.triggerCursor,
                reason: intent.reason,
            });
    }
}
function resolveSameIssueReplanCommentCursorPolicy(intent, visibleUpdate) {
    switch (intent.kind) {
        case "worker_replan_same_issue":
        case "run_limit_downgrade":
            if (intent.scannedInputCursor === null) {
                return { mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve };
            }
            return visibleUpdate === null
                ? {
                    mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToScannedInput,
                    scannedInputCursor: intent.scannedInputCursor,
                }
                : {
                    mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToPublishedCommentIfSafe,
                    scannedInputCursor: intent.scannedInputCursor,
                };
        case "service_implicit_replan":
            return {
                mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToPublishedCommentIfSafe,
                scannedInputCursor: intent.triggerCursor,
            };
        case "dependency_completion_requeue":
        case "preflight_template_drift":
        case "replan_completion":
            return { mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve };
        case "revise_plan":
            return {
                mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToScannedInput,
                scannedInputCursor: intent.triggerCursor,
            };
    }
}
function resolveSameIssueReplanVisibleUpdate(input) {
    const dedupeKey = resolveSameIssueReplanDedupeKey(input.intent);
    switch (input.intent.kind) {
        case "service_implicit_replan":
            return applyWorkflowUpdateDedupeKey((0, visible_update_builders_1.buildWorkflowVisibleUpdate)({
                event: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.replanQueued,
                summary: (0, replan_comment_1.formatWorkflowReplanSummary)({
                    responseLanguage: input.responseLanguage,
                    reason: input.intent.reason,
                }),
                next: (0, catalog_1.translate)(input.responseLanguage, "run.presentation.proceedNext"),
            }), dedupeKey);
        case "revise_plan":
            return null;
        default:
            return applyWorkflowUpdateDedupeKey(input.intent.visibleUpdate, dedupeKey);
    }
}
function applyWorkflowUpdateDedupeKey(visibleUpdate, dedupeKey) {
    if (visibleUpdate === null ||
        visibleUpdate.kind !== "workflow_update" ||
        dedupeKey === null ||
        visibleUpdate.dedupeKey !== undefined) {
        return visibleUpdate;
    }
    return {
        ...visibleUpdate,
        dedupeKey,
    };
}
function resolveSameIssueReplanDedupeKey(intent) {
    switch (intent.kind) {
        case "service_implicit_replan":
            return [
                "same-issue-replan",
                intent.kind,
                intent.issue.id,
                formatCursorDedupePart(intent.triggerCursor),
            ].join(":");
        case "worker_replan_same_issue":
        case "run_limit_downgrade":
            return [
                "same-issue-replan",
                intent.kind,
                intent.issue.id,
                formatCursorDedupePart(intent.scannedInputCursor),
                formatPreconditionDedupePart(intent.publishPrecondition),
            ].join(":");
        case "preflight_template_drift":
            return [
                "same-issue-replan",
                intent.kind,
                intent.issue.id,
                formatPreconditionDedupePart(intent.publishPrecondition),
            ].join(":");
        case "dependency_completion_requeue":
            return null;
        case "revise_plan":
        case "replan_completion":
            return null;
    }
}
function formatCursorDedupePart(cursor) {
    if (cursor === null) {
        return "cursor:none";
    }
    return `cursor:${cursor.commentId}@${cursor.createdAt}`;
}
function formatPreconditionDedupePart(precondition) {
    return [
        "canonical",
        precondition.expectedCanonicalCommentId ?? "none",
        "scan",
        precondition.expectedLastScannedCommentCursor == null
            ? "none"
            : formatCursorDedupePart(precondition.expectedLastScannedCommentCursor),
        "revision",
        precondition.expectedPlanRevision ?? "none",
    ].join(":");
}
