"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ARCHIVED_SPEC_LIST_STATUS = exports.DEFAULT_SPEC_LIST_STATUS = void 0;
exports.normalizeSpecRegistryStatuses = normalizeSpecRegistryStatuses;
exports.selectVisibleSpecRegistryEntries = selectVisibleSpecRegistryEntries;
exports.selectDefaultSpecListEntries = selectDefaultSpecListEntries;
exports.mapSpecListEntrySummaries = mapSpecListEntrySummaries;
exports.renderDefaultSpecListText = renderDefaultSpecListText;
exports.renderSpecRegistryVisibilityEvidence = renderSpecRegistryVisibilityEvidence;
exports.countSpecRegistryStatuses = countSpecRegistryStatuses;
exports.DEFAULT_SPEC_LIST_STATUS = "active";
exports.ARCHIVED_SPEC_LIST_STATUS = "archived";
function normalizeSpecRegistryStatuses(statuses = []) {
    const normalizedStatuses = [];
    const seenStatuses = new Set();
    for (const status of statuses) {
        const normalizedStatus = normalizeSpecRegistryStatus(status);
        if (normalizedStatus.length === 0 || seenStatuses.has(normalizedStatus)) {
            continue;
        }
        seenStatuses.add(normalizedStatus);
        normalizedStatuses.push(normalizedStatus);
    }
    return normalizedStatuses;
}
function selectVisibleSpecRegistryEntries(document, options = {}) {
    const requestedStatuses = normalizeSpecRegistryStatuses(options.statuses);
    const includeArchived = options.includeArchived === true;
    const availableStatusCounts = countSpecRegistryStatuses(document.entries);
    const availableStatuses = Object.keys(availableStatusCounts);
    const appliedStatuses = requestedStatuses.length > 0
        ? [...requestedStatuses]
        : availableStatuses.filter((status) => status !== exports.ARCHIVED_SPEC_LIST_STATUS);
    if (includeArchived && !appliedStatuses.includes(exports.ARCHIVED_SPEC_LIST_STATUS)) {
        appliedStatuses.push(exports.ARCHIVED_SPEC_LIST_STATUS);
    }
    const appliedStatusSet = new Set(appliedStatuses);
    const entries = document.entries.filter((entry) => (appliedStatusSet.has(normalizeSpecRegistryStatus(entry.status))));
    const excludedEntries = document.entries.filter((entry) => (!appliedStatusSet.has(normalizeSpecRegistryStatus(entry.status))));
    return {
        entries,
        requestedStatuses,
        appliedStatuses,
        includeArchived,
        availableStatusCounts,
        excludedStatusCounts: countSpecRegistryStatuses(excludedEntries),
        totalEntries: document.entries.length,
        selectedEntries: entries.length,
        excludedEntries: excludedEntries.length
    };
}
function selectDefaultSpecListEntries(document, options = {}) {
    return selectVisibleSpecRegistryEntries(document, options).entries;
}
function mapSpecListEntrySummaries(entries) {
    return entries.map((entry) => {
        const summary = {
            id: entry.id,
            title: entry.title,
            status: entry.status,
            sourceType: entry.source.type,
            sourceReference: entry.source.ref
        };
        if (entry.depth !== undefined) {
            summary.depth = entry.depth;
        }
        if (entry.destination !== undefined) {
            summary.destination = entry.destination;
        }
        if (entry.optional.linked_slice !== undefined) {
            summary.linkedSlice = entry.optional.linked_slice;
        }
        return summary;
    });
}
function renderDefaultSpecListText(entries, options = {}, visibility) {
    const evidence = visibility === undefined
        ? []
        : [
            ...renderSpecRegistryVisibilityEvidence(visibility),
            `List result entries: ${entries.length}`
        ];
    const entryText = entries.length === 0
        ? renderEmptySpecListText(options, visibility)
        : entries
            .map((entry, index) => {
            const lines = [
                `[${index + 1}] ${entry.id}`,
                `  title: ${entry.title}`,
                `  status: ${entry.status}`,
                `  source type: ${entry.sourceType}`,
                `  source reference: ${entry.sourceReference}`
            ];
            if (entry.depth !== undefined) {
                lines.splice(2, 0, `  depth: ${entry.depth}`);
            }
            if (entry.destination !== undefined) {
                const statusIndex = lines.findIndex((line) => line.startsWith("  status:"));
                lines.splice(statusIndex + 1, 0, `  destination: ${entry.destination}`);
            }
            if (entry.linkedSlice !== undefined) {
                lines.push(`  linked slice: ${entry.linkedSlice.slice}`);
                lines.push(`  linked slice path: ${entry.linkedSlice.path}`);
            }
            return lines.join("\n");
        })
            .join("\n\n");
    return evidence.length === 0
        ? entryText
        : `${evidence.join("\n")}\n\n${entryText}`;
}
function renderSpecRegistryVisibilityEvidence(visibility) {
    return [
        `Requested statuses: ${formatStatusList(visibility.requestedStatuses, "all non-archived")}`,
        `Applied statuses: ${formatStatusList(visibility.appliedStatuses, "none")}`,
        `Include archived: ${visibility.includeArchived ? "yes" : "no"}`,
        `Available status counts: ${formatStatusCounts(visibility.availableStatusCounts)}`,
        `Excluded status counts: ${formatStatusCounts(visibility.excludedStatusCounts)}`,
        `Selected entries: ${visibility.selectedEntries} of ${visibility.totalEntries}`
    ];
}
function countSpecRegistryStatuses(entries) {
    const counts = {};
    for (const entry of entries) {
        const status = normalizeSpecRegistryStatus(entry.status);
        counts[status] = (counts[status] ?? 0) + 1;
    }
    return counts;
}
function normalizeSpecRegistryStatus(status) {
    return String(status).trim().toLocaleLowerCase();
}
function renderEmptySpecListText(options, visibility) {
    if (visibility?.requestedStatuses.length) {
        return "No spec registry entries matched the applied lifecycle statuses.";
    }
    if (options.includeArchived) {
        return "No spec registry entries found.";
    }
    return "No non-archived spec registry entries found.";
}
function formatStatusList(statuses, emptyLabel) {
    return statuses.length === 0 ? emptyLabel : statuses.join(", ");
}
function formatStatusCounts(counts) {
    const entries = Object.entries(counts);
    return entries.length === 0
        ? "none"
        : entries.map(([status, count]) => `${status}=${count}`).join(", ");
}
