"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildForeignApprovalPlanSpec = buildForeignApprovalPlanSpec;
exports.buildAssigneeResolutionPlanSpec = buildAssigneeResolutionPlanSpec;
exports.buildStartApprovalWaitPlanSpec = buildStartApprovalWaitPlanSpec;
const catalog_1 = require("../../i18n/catalog");
const types_1 = require("../../platform/workflow-state/types");
const assignee_resolution_1 = require("../../workflows/assignee-resolution");
const publisher_1 = require("../../workflows/publisher");
const publisher_events_1 = require("../../workflows/publisher-events");
const approval_instructions_1 = require("../../workflows/approval-instructions");
const visible_update_builders_1 = require("../../workflows/visible-update-builders");
const workflow_projection_support_1 = require("../../workflows/workflow-projection-support");
const publish_precondition_support_1 = require("../../workflows/publish-precondition-support");
const workflow_timeline_scan_1 = require("../../workflows/workflow-timeline-scan");
const workflow_surface_1 = require("../../run/run-once-cycle/workflow-surface");
const config_decision_approval_support_1 = require("./config-decision-approval-support");
const config_publish_spec_support_1 = require("./config-publish-spec-support");
function buildForeignApprovalPlanSpec(input) {
    let visibleUpdateDedupeCursor;
    let publishPrecondition;
    if (input.triggerComment !== null) {
        visibleUpdateDedupeCursor = (0, workflow_timeline_scan_1.toWorkflowCommentCursor)(input.triggerComment);
    }
    else {
        visibleUpdateDedupeCursor = null;
        publishPrecondition = (0, publish_precondition_support_1.buildWorkflowPublishPrecondition)({
            currentWorkflowStateCommentId: input.currentWorkflowStateCommentId,
            workflowState: input.stickyState,
        });
    }
    return buildNeedsHumanPlanSpec({
        config: input.config,
        issue: input.issue,
        stickyState: input.stickyState,
        currentNeedsHumanReason: input.currentNeedsHumanReason,
        workflowLabels: input.workflowLabels,
        responseLanguage: input.responseLanguage,
        scannedInputCursor: input.scannedInputCursor,
        needsHumanReason: publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.foreignApproval,
        workflowStateOverrides: {
            workflowSurface: (0, workflow_surface_1.resolveWorkflowSurface)(input.issue),
            resetAiRunBudget: true,
        },
        commentCursorPolicyMode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToPublishedCommentIfSafe,
        visibleUpdate: {
            summary: (0, approval_instructions_1.buildForeignApprovalSummary)(input.responseLanguage, input.issue.id),
            next: (0, approval_instructions_1.buildForeignApprovalNext)(input.responseLanguage, input.issue.id),
        },
        visibleUpdateDedupeCursor,
        publishPrecondition,
    });
}
function buildAssigneeResolutionPlanSpec(input) {
    return buildNeedsHumanPlanSpec({
        config: input.config,
        issue: input.issue,
        stickyState: input.stickyState,
        currentNeedsHumanReason: input.currentNeedsHumanReason,
        workflowLabels: input.workflowLabels,
        responseLanguage: input.responseLanguage,
        scannedInputCursor: input.scannedInputCursor,
        needsHumanReason: publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.assigneeResolution,
        workflowStateOverrides: {
            workflowSurface: (0, workflow_surface_1.resolveWorkflowSurface)(input.issue),
            resetAiRunBudget: true,
        },
        commentCursorPolicyMode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToPublishedCommentIfSafe,
        visibleUpdateDedupeCursor: null,
        publishPrecondition: (0, publish_precondition_support_1.buildWorkflowPublishPrecondition)({
            currentWorkflowStateCommentId: input.currentWorkflowStateCommentId,
            workflowState: input.stickyState,
        }),
        visibleUpdate: {
            summary: (0, assignee_resolution_1.buildAssigneeResolutionSummary)(input.responseLanguage, input.resolution),
            next: (0, assignee_resolution_1.buildAssigneeResolutionNext)(input.responseLanguage, input.resolution),
        },
    });
}
function buildStartApprovalWaitPlanSpec(input) {
    return buildNeedsHumanPlanSpec({
        config: input.config,
        issue: input.issue,
        stickyState: input.stickyState,
        currentNeedsHumanReason: input.currentNeedsHumanReason,
        workflowLabels: input.workflowLabels,
        responseLanguage: input.responseLanguage,
        scannedInputCursor: input.scannedInputCursor,
        needsHumanReason: publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval,
        workflowStateOverrides: {
            pendingWorkflowStartApprovalIntent: input.stickyState.pendingWorkflowStartApprovalIntent,
        },
        commentCursorPolicyMode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve,
        visibleUpdate: {
            summary: (0, catalog_1.translate)(input.responseLanguage, "serviceRuntimeConfigApproval.visibleUpdate.assigneeResolution.releasedToStartApproval.summary"),
            next: (0, approval_instructions_1.buildStartApprovalNext)(input.responseLanguage, input.issue.id),
        },
    });
}
function buildNeedsHumanPlanSpec(input) {
    const nextWorkflowState = (0, config_decision_approval_support_1.materializeServiceRuntimeWorkflowState)({
        config: input.config,
        previousState: input.stickyState,
        needsHumanReason: input.needsHumanReason,
        ...(input.workflowStateOverrides ?? {}),
    });
    const nextSnapshot = (0, workflow_projection_support_1.withWorkflowStatus)(input.issue.workflowState, types_1.WORKFLOW_STATUSES.aiNeedsHuman);
    return (0, config_publish_spec_support_1.buildServiceRuntimeProjectedPublishSpec)({
        decisionKind: "commandless",
        issue: input.issue,
        responseLanguage: input.responseLanguage,
        workflowState: nextWorkflowState,
        scannedInputCursor: input.scannedInputCursor,
        commentCursorPolicyMode: input.commentCursorPolicyMode,
        workflowLabels: input.workflowLabels,
        nextWorkflowSnapshot: nextSnapshot,
        previousWorkflowState: input.stickyState,
        nextWorkflowState,
        previousNeedsHumanReason: input.currentNeedsHumanReason,
        nextNeedsHumanReason: input.needsHumanReason,
        visibleUpdate: (0, visible_update_builders_1.buildWorkflowVisibleUpdate)({
            event: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.humanAttentionRequested,
            summary: input.visibleUpdate.summary,
            next: input.visibleUpdate.next,
            needsHumanReason: input.needsHumanReason,
        }),
        visibleUpdateDedupeCursor: input.visibleUpdateDedupeCursor,
        publishPrecondition: input.publishPrecondition,
    });
}
