"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildPullRequestHandoffPublicationPlan = buildPullRequestHandoffPublicationPlan;
exports.buildPullRequestAutoMergePublicationPlan = buildPullRequestAutoMergePublicationPlan;
exports.buildPullRequestHumanAttentionPublicationPlan = buildPullRequestHumanAttentionPublicationPlan;
exports.buildPullRequestMergeFinalizationPublicationPlan = buildPullRequestMergeFinalizationPublicationPlan;
exports.buildWorkingInPrRestorePublicationPlan = buildWorkingInPrRestorePublicationPlan;
exports.buildWorkingInPrMergeFinalizationPublicationPlan = buildWorkingInPrMergeFinalizationPublicationPlan;
const types_1 = require("../../platform/ticket-tracker/types");
const publisher_1 = require("../../workflows/publisher");
const publish_request_builders_1 = require("../../workflows/publish-request-builders");
const publisher_state_support_1 = require("../../workflows/publisher-state-support");
const workflow_projection_support_1 = require("../../workflows/workflow-projection-support");
function buildPullRequestHandoffPublicationPlan(input) {
    const pullRequestThreadRequest = buildExistingIssuePublicationRequest({
        target: buildPullRequestThreadTarget(input.pullRequestIssue, input.pullRequestSourceIssueId ?? input.sourceIssue.id),
        workflowState: input.pullRequestWorkflowState,
        visibleWorkflowState: input.pullRequestVisibleWorkflowState,
        projection: input.pullRequestProjection,
        workflowLabels: input.workflowLabels,
        visibleUpdate: input.pullRequestVisibleUpdate,
        trackerTitle: input.pullRequestTrackerTitle,
        responseLanguage: input.responseLanguage,
    });
    return {
        kind: "pr_handoff",
        sourceIssueRequest: buildExistingIssuePublicationRequest({
            target: buildExistingIssueTarget(input.sourceIssue),
            workflowState: input.sourceIssueWorkflowState,
            projection: input.sourceProjection,
            workflowLabels: input.workflowLabels,
            visibleUpdate: input.sourceVisibleUpdate,
            commentCursorPolicy: resolvePullRequestHandoffSourceCursorPolicy({
                scannedInputCursor: input.sourceScannedInputCursor,
            }),
            trackerTitle: input.sourceTrackerTitle,
            responseLanguage: input.responseLanguage,
        }),
        pullRequestThreadRequest,
    };
}
function buildPullRequestAutoMergePublicationPlan(input) {
    return {
        kind: "pr_auto_merge",
        pullRequestThreadRequest: buildExistingIssuePublicationRequest({
            target: buildPullRequestTarget(input.pullRequestIssue, input.sourceIssueId),
            workflowState: input.workflowState,
            projection: (0, workflow_projection_support_1.buildSameWorkflowStateProjection)({
                previous: input.pullRequestIssue.workflowState,
                next: (0, workflow_projection_support_1.withWorkflowStatus)(input.pullRequestIssue.workflowState, "ai-ready"),
                previousNeedsHumanReason: input.previousNeedsHumanReason,
                nextNeedsHumanReason: null,
                workflowState: input.workflowState,
            }),
            workflowLabels: input.workflowLabels,
            visibleUpdate: input.visibleUpdate ?? null,
            trackerTitle: input.trackerTitle,
            responseLanguage: input.responseLanguage,
        }),
    };
}
function buildPullRequestHumanAttentionPublicationPlan(input) {
    return {
        kind: "pr_human_attention",
        pullRequestThreadRequest: buildExistingIssuePublicationRequest({
            target: buildPullRequestTarget(input.pullRequestIssue, input.sourceIssueId),
            workflowState: input.workflowState,
            projection: (0, workflow_projection_support_1.buildSameWorkflowStateProjection)({
                previous: input.pullRequestIssue.workflowState,
                next: input.nextSnapshot ?? {
                    ...input.pullRequestIssue.workflowState,
                },
                previousNeedsHumanReason: input.previousNeedsHumanReason,
                nextNeedsHumanReason: input.nextNeedsHumanReason,
                workflowState: input.workflowState,
            }),
            workflowLabels: input.workflowLabels,
            visibleUpdate: input.visibleUpdate,
            trackerTitle: input.trackerTitle,
            responseLanguage: input.responseLanguage,
        }),
    };
}
function buildPullRequestMergeFinalizationPublicationPlan(input) {
    return {
        kind: "pr_merge_finalization",
        pullRequestThreadRequest: buildMergeFinalizationRequest({
            target: buildPullRequestTarget(input.pullRequestIssue, input.pullRequestSourceIssueId),
            issue: input.pullRequestIssue,
            snapshot: input.pullRequestIssue.workflowState,
            workflowState: input.pullRequestWorkflowState ?? null,
            needsHumanReason: input.pullRequestNeedsHumanReason ?? null,
            trackerTitle: input.pullRequestTrackerTitle,
            workflowLabels: input.workflowLabels,
            responseLanguage: input.responseLanguage,
        }),
    };
}
function buildWorkingInPrRestorePublicationPlan(input) {
    return {
        kind: "working_in_pr_restore",
        sourceIssueRequest: buildExistingIssuePublicationRequest({
            target: buildExistingIssueTarget(input.sourceIssue),
            workflowState: input.sourceWorkflowState,
            projection: (0, workflow_projection_support_1.buildSameWorkflowStateProjection)({
                previous: input.sourceIssue.workflowState,
                next: (0, workflow_projection_support_1.withWorkflowStatus)(input.sourceIssue.workflowState, "ai-needs-human"),
                previousNeedsHumanReason: input.sourceNeedsHumanReason,
                nextNeedsHumanReason: null,
                workflowState: input.sourceWorkflowState,
            }),
            workflowLabels: input.workflowLabels,
            visibleUpdate: input.visibleUpdate,
            trackerTitle: input.trackerTitle,
            responseLanguage: input.responseLanguage,
        }),
    };
}
function buildWorkingInPrMergeFinalizationPublicationPlan(input) {
    return {
        kind: "working_in_pr_merge_finalization",
        sourceIssueRequest: buildMergeFinalizationRequest({
            target: buildExistingIssueTarget(input.sourceIssue),
            issue: input.sourceIssue,
            snapshot: input.sourceSnapshot,
            workflowState: input.sourceWorkflowState,
            needsHumanReason: input.sourceNeedsHumanReason,
            trackerTitle: input.trackerTitle,
            workflowLabels: input.workflowLabels,
            responseLanguage: input.responseLanguage,
        }),
    };
}
function buildMergeFinalizationRequest(input) {
    const projection = (0, workflow_projection_support_1.buildSameWorkflowStateProjection)({
        previous: input.snapshot,
        next: input.snapshot,
        previousNeedsHumanReason: input.needsHumanReason,
        nextNeedsHumanReason: null,
        workflowState: input.workflowState,
        labelProjection: { omitWorkflowLabels: true },
    });
    const currentLabelNames = input.issue.labelNames ?? [];
    if (currentLabelNames.length > 0) {
        projection.currentLabelNames = currentLabelNames;
    }
    return buildExistingIssuePublicationRequest({
        target: input.target,
        workflowState: input.workflowState,
        workflowLabels: input.workflowLabels,
        visibleUpdate: null,
        trackerTitle: input.trackerTitle,
        terminalAction: {
            kind: "close",
            postCloseWorkflowProjection: (0, workflow_projection_support_1.buildWorkflowPublicationProjectionChange)({
                workflowLabels: input.workflowLabels,
                projection,
            }),
        },
        responseLanguage: input.responseLanguage,
    });
}
function buildExistingIssuePublicationRequest(input) {
    const workflowProjection = input.projection === undefined
        ? undefined
        : (0, workflow_projection_support_1.buildWorkflowPublicationProjectionChange)({
            workflowLabels: input.workflowLabels,
            projection: input.projection,
        });
    const trackerFields = input.trackerTitle === undefined
        ? undefined
        : (0, publisher_state_support_1.buildWorkflowTrackerFields)({
            title: input.trackerTitle,
        });
    if (input.target.kind === "existing_issue") {
        return (0, publish_request_builders_1.buildExistingIssueWorkflowPublishRequest)({
            issueId: input.target.issueId,
            preloadedIssue: input.target.preloadedIssue,
            workflowState: input.workflowState,
            visibleWorkflowState: input.visibleWorkflowState,
            workflowProjection,
            trackerFields,
            visibleUpdate: input.visibleUpdate === undefined ? undefined : input.visibleUpdate,
            commentCursorPolicy: input.commentCursorPolicy,
            terminalAction: input.terminalAction,
            responseLanguage: input.responseLanguage,
        });
    }
    return (0, publish_request_builders_1.buildPullRequestThreadWorkflowPublishRequest)({
        pullRequestThreadId: input.target.pullRequestThreadId,
        sourceIssueId: input.target.sourceIssueId,
        preloadedIssue: input.target.preloadedIssue,
        workflowState: input.workflowState,
        visibleWorkflowState: input.visibleWorkflowState,
        workflowProjection,
        trackerFields,
        visibleUpdate: input.visibleUpdate === undefined ? undefined : input.visibleUpdate,
        commentCursorPolicy: input.commentCursorPolicy,
        terminalAction: input.terminalAction,
        responseLanguage: input.responseLanguage,
    });
}
function buildExistingIssueTarget(issue) {
    return {
        kind: "existing_issue",
        issueId: issue.id,
        preloadedIssue: issue,
    };
}
function resolvePullRequestHandoffSourceCursorPolicy(input) {
    if (input.scannedInputCursor === null) {
        return { mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve };
    }
    return {
        mode: publisher_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.advanceToPublishedCommentIfSafe,
        scannedInputCursor: input.scannedInputCursor,
    };
}
function buildPullRequestTarget(issue, sourceIssueId) {
    if (issue.kind !== types_1.TRACKER_ITEM_KINDS.pullRequest) {
        throw new Error("Pull request publication requires a pull request issue.");
    }
    return buildPullRequestThreadTarget(issue, sourceIssueId);
}
function buildPullRequestThreadTarget(issue, sourceIssueId) {
    return {
        kind: "pull_request_thread",
        pullRequestThreadId: issue.id,
        sourceIssueId,
        preloadedIssue: issue,
    };
}
