"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.resolveServiceRuntimeStartApprovalNormalization = resolveServiceRuntimeStartApprovalNormalization;
exports.buildApproveAcceptedVisibleUpdate = buildApproveAcceptedVisibleUpdate;
exports.materializeServiceRuntimeWorkflowState = materializeServiceRuntimeWorkflowState;
const control_plane_config_1 = require("../../config/control-plane-config");
const catalog_1 = require("../../i18n/catalog");
const approval_instructions_1 = require("../../workflows/approval-instructions");
const pre_plan_state_1 = require("../../workflows/pre-plan-state");
const followup_policy_1 = require("../../workflows/followup-policy");
const publisher_events_1 = require("../../workflows/publisher-events");
const visible_update_builders_1 = require("../../workflows/visible-update-builders");
const start_approval_support_1 = require("../../workflows/start-approval-support");
const template_bindings_1 = require("../../workflows/template-bindings");
function resolveServiceRuntimeStartApprovalNormalization(input) {
    const effectiveConfig = (0, control_plane_config_1.resolveIssueEffectiveConfigForWorkflowState)(input.config, input.stickyState);
    return {
        effectiveConfig,
        normalizedStickyState: input.stickyState === null
            ? null
            : (0, start_approval_support_1.normalizeWorkflowStartApprovalState)({
                config: effectiveConfig,
                issue: input.issue,
                workflowState: input.stickyState,
            }),
    };
}
function buildApproveAcceptedVisibleUpdate(input) {
    const summaryKey = input.reason === "accepted-noop"
        ? "serviceRuntimeConfigApproval.visibleUpdate.approve.acceptedNoop.summary"
        : "serviceRuntimeConfigApproval.visibleUpdate.approve.acceptedReady.summary";
    const nextKey = input.reason === "accepted-ready"
        ? "serviceRuntimeConfigApproval.visibleUpdate.approve.acceptedReady.next"
        : "serviceRuntimeConfigApproval.visibleUpdate.approve.acceptedNoop.next";
    const visibleUpdate = (0, visible_update_builders_1.buildWorkflowVisibleUpdate)({
        event: publisher_events_1.WORKFLOW_VISIBLE_EVENTS.humanAttentionRequested,
        summary: (0, catalog_1.translate)(input.language, summaryKey),
        next: input.reason === "accepted-start-approval"
            ? (0, approval_instructions_1.buildStartApprovalNext)(input.language, input.issueId)
            : (0, catalog_1.translate)(input.language, nextKey),
    });
    if (input.reason === "accepted-start-approval") {
        visibleUpdate.needsHumanReason = publisher_events_1.WORKFLOW_NEEDS_HUMAN_REASONS.startApproval;
    }
    return visibleUpdate;
}
function materializeServiceRuntimeWorkflowState(input) {
    if (input.previousState === null) {
        const workflowStateInput = {
            previousState: null,
            workflowTemplates: (0, template_bindings_1.createUnresolvedWorkflowTemplateBindings)(input.config.workflowTemplateDefaults),
            baseBranch: input.config.repositoryHost.baseBranch,
            followup: (0, followup_policy_1.resolveWorkflowFollowupState)({
                config: input.config.followup,
            }),
        };
        if (input.workflowSurface !== undefined) {
            workflowStateInput.workflowSurface = input.workflowSurface;
        }
        applyServiceRuntimeWorkflowStateOverridesToPrePlanInput(workflowStateInput, input);
        return (0, pre_plan_state_1.buildPrePlanWorkflowState)(workflowStateInput);
    }
    const nextState = {
        ...input.previousState,
    };
    applyServiceRuntimeWorkflowStateOverrides(nextState, input);
    return nextState;
}
function applyServiceRuntimeWorkflowStateOverridesToPrePlanInput(target, input) {
    if (input.foreignInputApproval !== undefined) {
        target.foreignInputApproval = input.foreignInputApproval;
    }
    if (input.needsHumanReason !== undefined) {
        target.needsHumanReason = input.needsHumanReason;
    }
    if (input.pendingWorkflowStartApprovalIntent !== undefined) {
        if (input.pendingWorkflowStartApprovalIntent !== null) {
            target.pendingWorkflowStartApprovalIntent =
                input.pendingWorkflowStartApprovalIntent;
        }
    }
    if (input.pendingPlanningIntent !== undefined) {
        if (input.pendingPlanningIntent !== null) {
            target.pendingPlanningIntent = input.pendingPlanningIntent;
        }
    }
    if (input.pendingPlanningReason !== undefined) {
        target.pendingPlanningReason = input.pendingPlanningReason;
    }
    if (input.resetAiRunBudget === true) {
        target.consecutiveAiRuns = 0;
        target.totalAiRunHours = 0;
    }
}
function applyServiceRuntimeWorkflowStateOverrides(target, input) {
    if (input.foreignInputApproval !== undefined) {
        target.foreignInputApproval = input.foreignInputApproval;
    }
    if (input.needsHumanReason !== undefined) {
        target.needsHumanReason = input.needsHumanReason;
    }
    if (input.pendingWorkflowStartApprovalIntent !== undefined) {
        if (input.pendingWorkflowStartApprovalIntent === null) {
            delete target.pendingWorkflowStartApprovalIntent;
        }
        else {
            target.pendingWorkflowStartApprovalIntent =
                input.pendingWorkflowStartApprovalIntent;
        }
    }
    if (input.pendingPlanningIntent !== undefined) {
        if (input.pendingPlanningIntent === null) {
            delete target.pendingPlanningIntent;
        }
        else {
            target.pendingPlanningIntent = input.pendingPlanningIntent;
        }
    }
    if (input.pendingPlanningReason !== undefined) {
        if (input.pendingPlanningReason === null) {
            delete target.pendingPlanningReason;
        }
        else {
            target.pendingPlanningReason = input.pendingPlanningReason;
        }
    }
    if (input.resetAiRunBudget === true) {
        target.consecutiveAiRuns = 0;
        target.totalAiRunHours = 0;
    }
}
