"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveOwnedIssueWorkflowStatePublication = resolveOwnedIssueWorkflowStatePublication;
exports.publishOwnedIssueWorkflowState = publishOwnedIssueWorkflowState;
const canonical_workflow_state_1 = require("../../comments/canonical-workflow-state");
const publisher_shared_1 = require("../../workflows/publisher-shared");
const execution_ownership_1 = require("../execution-ownership");
const publisher_factory_1 = require("./publisher-factory");
const shared_1 = require("./shared");
async function resolveOwnedIssueWorkflowStatePublication(input) {
    const comments = input.comments ??
        (await (0, shared_1.loadSortedIssueComments)(input.context.ticketTracker, input.claimedIssue.originalIssue.id, {
            timeline: "issue-only",
        }));
    const currentWorkflowStateResolution = (0, canonical_workflow_state_1.resolveCanonicalWorkflowStateComment)(comments, input.responseLanguage);
    if (currentWorkflowStateResolution.kind !== "found" ||
        !(0, execution_ownership_1.isExecutionOwnershipOwnedBy)(currentWorkflowStateResolution.workflowState.executionOwnership, input.claimedIssue.executionOwnership)) {
        return null;
    }
    const publisher = (0, publisher_factory_1.createRunWorkflowPublisher)(input.context);
    return {
        currentWorkflowState: currentWorkflowStateResolution.workflowState,
        sourceCommentId: currentWorkflowStateResolution.comment.id,
        sourceLastScannedCommentCursor: currentWorkflowStateResolution.workflowState.lastScannedCommentCursor ??
            null,
        async publishWorkflowState(workflowState, options) {
            const request = {
                target: {
                    kind: "existing_issue",
                    issueId: input.claimedIssue.originalIssue.id,
                    preloadedIssue: input.issue,
                },
                workflowState,
                publishPrecondition: {
                    expectedCanonicalCommentId: currentWorkflowStateResolution.comment.id,
                    expectedLastScannedCommentCursor: currentWorkflowStateResolution.workflowState
                        .lastScannedCommentCursor ?? null,
                    expectedExecutionOwner: input.claimedIssue.executionOwnership,
                },
                commentCursorPolicy: {
                    mode: publisher_shared_1.WORKFLOW_COMMENT_CURSOR_POLICY_MODES.preserve,
                },
                responseLanguage: input.responseLanguage,
                ...(options?.workflowProjection !== undefined
                    ? { workflowProjection: options.workflowProjection }
                    : {}),
            };
            const result = await publisher.publishWorkflow(request);
            await options?.afterPublish?.({ request, result });
            return result;
        },
    };
}
async function publishOwnedIssueWorkflowState(input) {
    const publication = await resolveOwnedIssueWorkflowStatePublication(input);
    if (publication === null) {
        return null;
    }
    const workflowState = input.buildWorkflowState === undefined
        ? (0, execution_ownership_1.stripExecutionOwnership)(publication.currentWorkflowState)
        : input.buildWorkflowState(publication.currentWorkflowState);
    if (workflowState === null) {
        return null;
    }
    return publication.publishWorkflowState(workflowState, {
        workflowProjection: input.buildWorkflowProjection?.({
            currentWorkflowState: publication.currentWorkflowState,
            nextWorkflowState: workflowState,
        }),
        ...(input.afterPublish !== undefined
            ? { afterPublish: input.afterPublish }
            : {}),
    });
}
