"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UNPLANNED_CHILD_KEY = void 0;
exports.resolveChildCanonicalWorkflowState = resolveChildCanonicalWorkflowState;
exports.loadChildCanonicalStateMap = loadChildCanonicalStateMap;
exports.resolveChildIssueIdentity = resolveChildIssueIdentity;
const canonical_workflow_state_1 = require("../../comments/canonical-workflow-state");
exports.UNPLANNED_CHILD_KEY = "unplanned";
async function resolveChildCanonicalWorkflowState(input) {
    const comments = await input.ticketTracker.listComments(input.issueId, {
        timeline: "issue-only",
    });
    const resolution = (0, canonical_workflow_state_1.resolveCanonicalWorkflowStateComment)(comments);
    return resolution.kind === "found" ? resolution.workflowState : null;
}
async function loadChildCanonicalStateMap(input) {
    const issueIds = [...new Set(input.issues.map((issue) => issue.id))];
    const workflowStates = await Promise.all(issueIds.map((issueId) => resolveChildCanonicalWorkflowState({
        ticketTracker: input.ticketTracker,
        issueId,
    })));
    return new Map(issueIds.map((issueId, index) => [issueId, workflowStates[index] ?? null]));
}
function resolveChildIssueIdentity(workflowState) {
    return {
        childKey: workflowState?.parentLink?.childKey ?? null,
        parentRef: workflowState?.parentLink?.parentRef ?? null,
    };
}
