"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderStartApprovalNextAction = renderStartApprovalNextAction;
const catalog_1 = require("../../i18n/catalog");
const approval_instructions_1 = require("../../workflows/approval-instructions");
const state_1 = require("../../workflows/state");
function renderStartApprovalNextAction(locale, workflowState, issueId) {
    const baseMessage = firstLine((0, approval_instructions_1.buildStartApprovalNext)(locale, requireIssueIdForStartApproval(issueId)));
    if (!hasPendingControllerChildIssueMaterialization(workflowState)) {
        return baseMessage;
    }
    return `${baseMessage} ${(0, catalog_1.translate)(locale, "canonicalWorkflowComment.startApprovalControllerChildIssueNext")}`.trim();
}
function hasPendingControllerChildIssueMaterialization(workflowState) {
    if (!(0, state_1.isWorkflowAwaitingStartApproval)(workflowState) ||
        workflowState.plan === null) {
        return false;
    }
    return workflowState.plan.steps.some((step) => step.kind === "child_issue" && !step.childIssueRef);
}
function firstLine(value) {
    return value.split(/\r?\n/u)[0]?.trim() ?? "";
}
function requireIssueIdForStartApproval(issueId) {
    if (issueId === undefined || issueId === null) {
        throw new Error("issueId is required for start approval instructions.");
    }
    return issueId;
}
