"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveWorkflowAuxiliaryManagedLabels = resolveWorkflowAuxiliaryManagedLabels;
exports.withWorkflowStatus = withWorkflowStatus;
exports.buildSameWorkflowStateProjection = buildSameWorkflowStateProjection;
exports.buildWorkflowProjectionWithAuxiliaryLabels = buildWorkflowProjectionWithAuxiliaryLabels;
exports.buildWorkflowPublicationProjectionChange = buildWorkflowPublicationProjectionChange;
const workflow_labels_1 = require("../platform/ticket-tracker/workflow-labels");
const publisher_state_support_1 = require("./publisher-state-support");
const recurring_1 = require("./recurring");
const scheduled_run_1 = require("./scheduled-run");
function resolveWorkflowAuxiliaryManagedLabels(input) {
    return (0, workflow_labels_1.resolveAuxiliaryManagedLabelNames)({
        workflowLabels: input.workflowLabels,
        snapshot: input.snapshot,
        needsHumanReason: input.needsHumanReason,
        coolingDown: (0, scheduled_run_1.isCooldownScheduledRun)(input.workflowState),
        recurringLabelNames: (0, recurring_1.resolveRecurringLabelNames)(input.workflowLabels.aiRecurring, input.workflowState),
    });
}
function withWorkflowStatus(snapshot, status) {
    return {
        status,
        rules: [...snapshot.rules],
    };
}
function buildSameWorkflowStateProjection(input) {
    const projection = {
        previous: input.previous,
        next: input.next,
        previousNeedsHumanReason: input.previousNeedsHumanReason,
        nextNeedsHumanReason: input.nextNeedsHumanReason,
        previousWorkflowState: input.workflowState,
        nextWorkflowState: input.workflowState,
    };
    if (input.labelProjection !== undefined) {
        projection.labelProjection = input.labelProjection;
    }
    return projection;
}
function buildWorkflowProjectionWithAuxiliaryLabels(input) {
    const projectionInput = {
        previous: input.previous,
        next: input.next,
        auxiliaryManagedLabels: {
            previous: resolveWorkflowAuxiliaryManagedLabels({
                workflowLabels: input.workflowLabels,
                snapshot: input.previous,
                workflowState: input.previousWorkflowState,
                needsHumanReason: input.previousNeedsHumanReason,
            }),
            next: resolveWorkflowAuxiliaryManagedLabels({
                workflowLabels: input.workflowLabels,
                snapshot: input.next,
                workflowState: input.nextWorkflowState,
                needsHumanReason: input.nextNeedsHumanReason,
            }),
        },
    };
    if (input.issueId !== undefined) {
        projectionInput.issueId = input.issueId;
    }
    if (input.currentLabelNames !== undefined) {
        projectionInput.currentLabelNames = input.currentLabelNames;
    }
    if (input.labelProjection !== undefined) {
        projectionInput.labelProjection = input.labelProjection;
    }
    return (0, publisher_state_support_1.buildWorkflowProjectionChange)(projectionInput);
}
function buildWorkflowPublicationProjectionChange(input) {
    const projectionInput = {
        workflowLabels: input.workflowLabels,
        previous: input.projection.previous,
        next: input.projection.next,
        previousWorkflowState: input.projection.previousWorkflowState,
        nextWorkflowState: input.projection.nextWorkflowState,
        previousNeedsHumanReason: input.projection.previousNeedsHumanReason,
        nextNeedsHumanReason: input.projection.nextNeedsHumanReason,
    };
    if (input.projection.currentLabelNames !== undefined) {
        projectionInput.currentLabelNames = input.projection.currentLabelNames;
    }
    if (input.projection.labelProjection !== undefined) {
        projectionInput.labelProjection = input.projection.labelProjection;
    }
    return buildWorkflowProjectionWithAuxiliaryLabels(projectionInput);
}
