"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderWorkflowStepTitleWithIssueRef = renderWorkflowStepTitleWithIssueRef;
exports.splitMarkdownSections = splitMarkdownSections;
exports.renderValue = renderValue;
exports.flattenMarkdownSummary = flattenMarkdownSummary;
function renderWorkflowStepTitleWithIssueRef(step) {
    if (step.kind !== "child_issue" || !step.childIssueRef) {
        return step.title;
    }
    return `${step.title} (${step.childIssueRef})`;
}
function splitMarkdownSections(markdown) {
    const sections = new Map();
    const parts = markdown.split(/^### /m).slice(1);
    for (const part of parts) {
        const separatorIndex = part.indexOf("\n\n");
        if (separatorIndex === -1) {
            continue;
        }
        const title = part.slice(0, separatorIndex).trim();
        const body = part.slice(separatorIndex + 2).trim();
        if (title.length > 0) {
            sections.set(title, body);
        }
    }
    return sections;
}
function normalizeNullableText(value) {
    if (value === undefined || value === null) {
        return null;
    }
    const text = value.trim();
    return text.length === 0 || text === "-" ? null : text;
}
function renderValue(value) {
    if (value === undefined || value === null) {
        return "-";
    }
    const text = String(value).trim();
    return text.length > 0 ? text : "-";
}
function flattenMarkdownSummary(text) {
    if (text === null) {
        return null;
    }
    const segments = text
        .split("\n")
        .map((line) => normalizeNullableText(line))
        .filter((line) => line !== null)
        .map((line) => {
        const trimmed = line.trim();
        if (trimmed.length === 0) {
            return null;
        }
        if (/^#{1,6}\s+/.test(trimmed)) {
            return {
                separator: "",
                text: trimmed.replace(/^#{1,6}\s+/, ""),
            };
        }
        if (/^>\s?/.test(trimmed)) {
            return {
                separator: "",
                text: trimmed.replace(/^>\s?/, ""),
            };
        }
        if (/^[-*+]\s+/.test(trimmed)) {
            return {
                separator: " - ",
                text: trimmed.replace(/^[-*+]\s+/, ""),
            };
        }
        if (/^\d+\.\s+/.test(trimmed)) {
            return {
                separator: " - ",
                text: trimmed.replace(/^\d+\.\s+/, ""),
            };
        }
        return {
            separator: "",
            text: trimmed,
        };
    })
        .filter((segment) => segment !== null && segment.text.length > 0);
    if (segments.length === 0) {
        return null;
    }
    const flattened = segments.reduce((combined, segment, index) => {
        if (index === 0) {
            return segment.text;
        }
        return `${combined}${segment.separator || " "}${segment.text}`;
    }, "");
    const plainText = flattened
        .replace(/\[([^\]]+)\]\(([^)]+)\)/g, "$1")
        .replace(/`([^`]+)`/g, "$1")
        .replace(/\*\*([^*]+)\*\*/g, "$1")
        .replace(/__([^_]+)__/g, "$1")
        .replace(/\*([^*\n]+)\*/g, "$1")
        .replace(/_([^_\n]+)_/g, "$1")
        .replace(/\s+/g, " ")
        .trim();
    return plainText.length === 0 ? null : plainText;
}
