"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sentenceCase = sentenceCase;
exports.list = list;
exports.listOrPending = listOrPending;
exports.assertPlanningAuthoringCompliance = assertPlanningAuthoringCompliance;
exports.injectRuntimeAttributionRequiredFrontMatter = injectRuntimeAttributionRequiredFrontMatter;
const errors_1 = require("../../errors");
const authoring_policy_1 = require("../authoring-policy");
function sentenceCase(value) {
    if (value === "") {
        return value;
    }
    return `${value[0].toUpperCase()}${value.slice(1)}`;
}
function list(items) {
    return listOrPending(items, ["Evidence is intentionally incomplete; refine this task before implementation."]);
}
function listOrPending(items, fallback = ["Pending. Add concrete evidence before implementation."]) {
    const normalized = items
        .map((item) => item?.trim() ?? "")
        .filter((item, index, allItems) => item !== "" && allItems.indexOf(item) === index);
    return (normalized.length === 0 ? fallback : normalized).map((item) => `- ${item}`).join("\n");
}
function assertPlanningAuthoringCompliance(artifactType, markdownText) {
    const issues = (0, authoring_policy_1.auditPlanningArtifactAuthoring)({
        artifactPath: `generated ${artifactType}`,
        artifactType,
        markdownText
    });
    if (issues.length > 0) {
        throw (0, errors_1.createUsageError)(`Planning authoring policy rejected generated ${artifactType} scaffold text.`, (0, authoring_policy_1.formatPlanningAuthoringIssues)(issues));
    }
}
function injectRuntimeAttributionRequiredFrontMatter(markdownText, required) {
    const lines = markdownText.split(/\r?\n/);
    const closingIndex = lines.findIndex((line, index) => index > 0 && line === "---");
    if (lines[0] !== "---" || closingIndex === -1) {
        throw new TypeError("Task artifact is missing valid YAML front matter.");
    }
    const existingIndex = lines.findIndex((line, index) => {
        return index > 0 && index < closingIndex && line.startsWith("runtime_attribution_required:");
    });
    if (existingIndex !== -1) {
        lines[existingIndex] = `runtime_attribution_required: ${required ? "true" : "false"}`;
        return lines.join("\n");
    }
    const evidenceProfileIndex = lines.findIndex((line, index) => {
        return index > 0 && index < closingIndex && line.startsWith("evidence_profile:");
    });
    const insertionIndex = evidenceProfileIndex === -1 ? closingIndex : evidenceProfileIndex + 1;
    lines.splice(insertionIndex, 0, `runtime_attribution_required: ${required ? "true" : "false"}`);
    return lines.join("\n");
}
