"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolvePersistedIssueWorkflowState = resolvePersistedIssueWorkflowState;
exports.loadPersistedWorkflowStatesForIssues = loadPersistedWorkflowStatesForIssues;
const canonical_workflow_state_1 = require("../comments/canonical-workflow-state");
const workflow_timeline_scan_1 = require("./workflow-timeline-scan");
async function resolvePersistedIssueWorkflowState(ticketTracker, issueId) {
    const comments = (0, workflow_timeline_scan_1.sortTimelineComments)(await ticketTracker.listComments(issueId, {
        timeline: "issue-only",
    }));
    const resolution = (0, canonical_workflow_state_1.resolveCanonicalWorkflowStateComment)(comments);
    return resolution.kind === "found" ? resolution.workflowState : null;
}
async function loadPersistedWorkflowStatesForIssues(ticketTracker, issues) {
    const workflowStates = await Promise.all(issues.map(async (issue) => ({
        issueId: issue.id,
        workflowState: await resolvePersistedIssueWorkflowState(ticketTracker, issue.id),
    })));
    return new Map(workflowStates.map(({ issueId, workflowState }) => [
        issueId,
        workflowState,
    ]));
}
