"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPendingWorkflowCommandUpdate = getPendingWorkflowCommandUpdate;
exports.resolvePendingWorkflowCommand = resolvePendingWorkflowCommand;
exports.resolvePendingWorkflowCommandFromEvents = resolvePendingWorkflowCommandFromEvents;
const command_apply_1 = require("./command-apply");
const workflow_option_normalizer_1 = require("./workflow-option-normalizer");
const workflow_timeline_scan_1 = require("./workflow-timeline-scan");
function getPendingWorkflowCommandUpdate(pendingCommand) {
    return pendingCommand?.command.kind === "update"
        ? pendingCommand.command
        : null;
}
function resolvePendingWorkflowCommand(comments, options = {}) {
    const scanInput = {
        comments,
        workflowState: options.workflowState ?? null,
    };
    if (options.issueBody !== undefined) {
        scanInput.issueBody = options.issueBody;
    }
    const scan = (0, workflow_timeline_scan_1.scanWorkflowTimeline)(scanInput);
    return resolvePendingWorkflowCommandFromEvents(scan.workflowCommandEvents);
}
function resolvePendingWorkflowCommandFromEvents(events) {
    const folded = foldWorkflowCommandEvents(events);
    if (folded === null) {
        return null;
    }
    if (folded.source === "body") {
        return {
            source: "body",
            command: folded.command,
        };
    }
    return {
        source: "comment",
        comment: folded.comment,
        command: folded.command,
    };
}
function foldWorkflowCommandEvents(events) {
    let foldedUpdate = null;
    let latestAppliedEvent = null;
    for (const event of events) {
        if (event.command.kind === "invalid") {
            latestAppliedEvent = event;
            continue;
        }
        foldedUpdate = mergeWorkflowCommandUpdates(foldedUpdate, event.command);
        latestAppliedEvent =
            event.source === "body"
                ? { ...event, command: foldedUpdate }
                : { ...event, command: foldedUpdate };
    }
    return latestAppliedEvent;
}
function mergeWorkflowCommandUpdates(previous, next) {
    const configOverridePatch = mergeConfigOverridePatchForCommand(previous?.configOverridePatch, next.configOverridePatch);
    const merged = {
        kind: "update",
        bodyAfterCommand: next.bodyAfterCommand,
    };
    if (previous !== null) {
        (0, workflow_option_normalizer_1.assignWorkflowCommandCoreUpdateFields)(merged, previous);
    }
    (0, workflow_option_normalizer_1.assignWorkflowCommandCoreUpdateFields)(merged, next);
    if (configOverridePatch !== undefined) {
        merged.configOverridePatch = configOverridePatch;
    }
    return merged;
}
function mergeConfigOverridePatchForCommand(previous, next) {
    if (next === undefined) {
        return previous;
    }
    const mergedEntries = (0, command_apply_1.mergeConfigOverridePatchEntries)(previous?.entries, next.entries, { keepInheritEntries: true });
    return mergedEntries.length === 0 ? undefined : { entries: mergedEntries };
}
