"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.verifyLatestCanonicalCommentUnchanged = verifyLatestCanonicalCommentUnchanged;
exports.resolveLatestCanonicalComment = resolveLatestCanonicalComment;
const canonical_workflow_state_1 = require("../comments/canonical-workflow-state");
const state_backup_1 = require("./state-backup");
async function verifyLatestCanonicalCommentUnchanged(input) {
    input.ticketTracker.resetVolatileIssueReads(input.issueId);
    const freshComments = await input.ticketTracker.listComments(input.issueId, {
        timeline: "issue-only",
    });
    const fresh = resolveLatestCanonicalComment(freshComments);
    if (fresh.kind !== "found") {
        return { kind: "skipped", reason: "canonical workflow state changed" };
    }
    if (fresh.comment.id !== input.expectedCommentId) {
        return {
            kind: "skipped",
            reason: "newer canonical workflow state comment exists",
        };
    }
    if ((0, state_backup_1.sha256Hex)(fresh.comment.body) !== input.expectedBodySha256) {
        return {
            kind: "skipped",
            reason: input.changedReason,
        };
    }
    return { kind: "matched", comment: fresh.comment };
}
function resolveLatestCanonicalComment(comments) {
    for (const comment of [...comments].reverse()) {
        if ((0, canonical_workflow_state_1.extractRawCanonicalWorkflowMachineState)(comment.body).kind !== "missing") {
            return { kind: "found", comment };
        }
    }
    return { kind: "missing" };
}
