"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasActiveWorkflowStartApproval = hasActiveWorkflowStartApproval;
exports.isWorkflowAwaitingActiveStartApproval = isWorkflowAwaitingActiveStartApproval;
exports.normalizeWorkflowStartApprovalState = normalizeWorkflowStartApprovalState;
const control_plane_config_1 = require("../config/control-plane-config");
const publisher_events_1 = require("./publisher-events");
function hasActiveWorkflowStartApproval(input) {
    return isWorkflowStartApprovalIntentActive({
        config: input.config,
        issue: input.issue,
        intent: input.workflowState?.pendingWorkflowStartApprovalIntent,
    });
}
function isWorkflowAwaitingActiveStartApproval(input) {
    return (hasActiveWorkflowStartApproval(input) &&
        input.workflowState?.needsHumanReason ===
            publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval);
}
function normalizeWorkflowStartApprovalState(input) {
    if (hasActiveWorkflowStartApproval(input)) {
        return input.workflowState;
    }
    if (input.workflowState.pendingWorkflowStartApprovalIntent === undefined &&
        input.workflowState.pendingPlanningIntent !== "revise_plan" &&
        input.workflowState.needsHumanReason !==
            publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval) {
        return input.workflowState;
    }
    const nextState = {
        ...input.workflowState,
    };
    delete nextState.pendingWorkflowStartApprovalIntent;
    if (nextState.needsHumanReason === publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval) {
        nextState.needsHumanReason = null;
    }
    if (nextState.pendingPlanningIntent === "revise_plan") {
        delete nextState.pendingPlanningIntent;
        delete nextState.pendingPlanningReason;
        delete nextState.pendingPlanningSource;
    }
    return nextState;
}
function isWorkflowStartApprovalIntentActive(input) {
    if (input.intent === undefined || input.intent === null) {
        return false;
    }
    return ((0, control_plane_config_1.resolveWorkflowStartApprovalMode)({
        target: input.issue.kind === "pull_request" ? "pull_request" : "issue",
        planningIntent: input.intent,
        config: input.config,
    }) !== "auto");
}
