"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseRegistryTimestampField = exports.parseLinkedSliceReference = exports.formatLinkedSliceReferenceText = exports.serializeSpecRegistrySourceHistory = exports.parseSpecRegistrySourceHistory = exports.applySpecSourceTransition = exports.serializeSpecRegistrySourceReference = exports.parseSpecRegistrySourceReference = exports.classifySpecRegistrySource = exports.LOCAL_SPEC_SOURCE_TYPE = exports.validateSpecRegistryRouting = exports.parseSpecRegistryRouting = exports.assertSpecRegistryRouting = exports.validateSpecRegistrySemanticMetadata = exports.validateSpecRegistryDestination = exports.parseSpecRegistryDestination = exports.assertSpecRegistryDestination = exports.validateSpecIndexDocumentStructure = exports.validateSpecRegistryStructure = exports.findDuplicateSpecIds = exports.validateSpecRegistryAddInput = exports.applySpecRegistryAddSafety = exports.selectDefaultSpecSearchEntries = exports.searchSpecRegistryEntries = exports.renderDefaultSpecSearchText = exports.mapSpecSearchEntries = exports.filterSpecSearchEntries = exports.buildSpecSearchRecoveryCommand = exports.DEFAULT_SPEC_SEARCH_STATUS = exports.selectVisibleSpecRegistryEntries = exports.selectDefaultSpecListEntries = exports.renderSpecRegistryVisibilityEvidence = exports.renderDefaultSpecListText = exports.normalizeSpecRegistryStatuses = exports.mapSpecListEntrySummaries = exports.countSpecRegistryStatuses = exports.DEFAULT_SPEC_LIST_STATUS = exports.ARCHIVED_SPEC_LIST_STATUS = exports.applySpecRegistryUnlink = exports.applySpecRegistryArchive = exports.applySpecRegistryAdd = exports.serializeSpecIndexDocument = exports.renderSpecIndexDocument = exports.buildSpecIndexDocumentWithEntries = exports.applyNonDestructiveSpecIndexEntryUpdate = exports.applySpecIndexEntryFieldUpdates = exports.parseSpecIndexDocumentText = exports.parseSpecIndexDocument = exports.serializeSpecIndexEntry = exports.parseSpecIndexEntry = void 0;
exports.SPEC_INDEX_ENTRY_ROUTED_REQUIRED_FIELDS = exports.SPEC_INDEX_ENTRY_REQUIRED_FIELDS = exports.SPEC_INDEX_ENTRY_OPTIONAL_FIELDS = exports.serializeLinkedSliceReference = exports.validateLinkedSliceReference = void 0;
exports.validateSpecRegistryDuplicateIdsForDoctor = validateSpecRegistryDuplicateIdsForDoctor;
exports.validateSpecRegistryStructureForDoctor = validateSpecRegistryStructureForDoctor;
exports.validateSpecRegistrySemanticMetadataForDoctor = validateSpecRegistrySemanticMetadataForDoctor;
const cli_constants_1 = require("../config/cli-constants");
const duplicate_detection_1 = require("./duplicate-detection");
const document_io_1 = require("./document-io");
const read_helpers_1 = require("./read-helpers");
const semantic_validation_1 = require("./semantic-validation");
const structure_validation_1 = require("./structure-validation");
var schema_1 = require("./schema");
Object.defineProperty(exports, "parseSpecIndexEntry", { enumerable: true, get: function () { return schema_1.parseSpecIndexEntry; } });
Object.defineProperty(exports, "serializeSpecIndexEntry", { enumerable: true, get: function () { return schema_1.serializeSpecIndexEntry; } });
var document_io_2 = require("./document-io");
Object.defineProperty(exports, "parseSpecIndexDocument", { enumerable: true, get: function () { return document_io_2.parseSpecIndexDocument; } });
Object.defineProperty(exports, "parseSpecIndexDocumentText", { enumerable: true, get: function () { return document_io_2.parseSpecIndexDocumentText; } });
Object.defineProperty(exports, "applySpecIndexEntryFieldUpdates", { enumerable: true, get: function () { return document_io_2.applySpecIndexEntryFieldUpdates; } });
Object.defineProperty(exports, "applyNonDestructiveSpecIndexEntryUpdate", { enumerable: true, get: function () { return document_io_2.applyNonDestructiveSpecIndexEntryUpdate; } });
Object.defineProperty(exports, "buildSpecIndexDocumentWithEntries", { enumerable: true, get: function () { return document_io_2.buildSpecIndexDocumentWithEntries; } });
Object.defineProperty(exports, "renderSpecIndexDocument", { enumerable: true, get: function () { return document_io_2.renderSpecIndexDocument; } });
Object.defineProperty(exports, "serializeSpecIndexDocument", { enumerable: true, get: function () { return document_io_2.serializeSpecIndexDocument; } });
var add_1 = require("./add");
Object.defineProperty(exports, "applySpecRegistryAdd", { enumerable: true, get: function () { return add_1.applySpecRegistryAdd; } });
var archive_1 = require("./archive");
Object.defineProperty(exports, "applySpecRegistryArchive", { enumerable: true, get: function () { return archive_1.applySpecRegistryArchive; } });
var unlink_1 = require("./unlink");
Object.defineProperty(exports, "applySpecRegistryUnlink", { enumerable: true, get: function () { return unlink_1.applySpecRegistryUnlink; } });
var list_1 = require("./list");
Object.defineProperty(exports, "ARCHIVED_SPEC_LIST_STATUS", { enumerable: true, get: function () { return list_1.ARCHIVED_SPEC_LIST_STATUS; } });
Object.defineProperty(exports, "DEFAULT_SPEC_LIST_STATUS", { enumerable: true, get: function () { return list_1.DEFAULT_SPEC_LIST_STATUS; } });
Object.defineProperty(exports, "countSpecRegistryStatuses", { enumerable: true, get: function () { return list_1.countSpecRegistryStatuses; } });
Object.defineProperty(exports, "mapSpecListEntrySummaries", { enumerable: true, get: function () { return list_1.mapSpecListEntrySummaries; } });
Object.defineProperty(exports, "normalizeSpecRegistryStatuses", { enumerable: true, get: function () { return list_1.normalizeSpecRegistryStatuses; } });
Object.defineProperty(exports, "renderDefaultSpecListText", { enumerable: true, get: function () { return list_1.renderDefaultSpecListText; } });
Object.defineProperty(exports, "renderSpecRegistryVisibilityEvidence", { enumerable: true, get: function () { return list_1.renderSpecRegistryVisibilityEvidence; } });
Object.defineProperty(exports, "selectDefaultSpecListEntries", { enumerable: true, get: function () { return list_1.selectDefaultSpecListEntries; } });
Object.defineProperty(exports, "selectVisibleSpecRegistryEntries", { enumerable: true, get: function () { return list_1.selectVisibleSpecRegistryEntries; } });
var search_1 = require("./search");
Object.defineProperty(exports, "DEFAULT_SPEC_SEARCH_STATUS", { enumerable: true, get: function () { return search_1.DEFAULT_SPEC_SEARCH_STATUS; } });
Object.defineProperty(exports, "buildSpecSearchRecoveryCommand", { enumerable: true, get: function () { return search_1.buildSpecSearchRecoveryCommand; } });
Object.defineProperty(exports, "filterSpecSearchEntries", { enumerable: true, get: function () { return search_1.filterSpecSearchEntries; } });
Object.defineProperty(exports, "mapSpecSearchEntries", { enumerable: true, get: function () { return search_1.mapSpecSearchEntries; } });
Object.defineProperty(exports, "renderDefaultSpecSearchText", { enumerable: true, get: function () { return search_1.renderDefaultSpecSearchText; } });
Object.defineProperty(exports, "searchSpecRegistryEntries", { enumerable: true, get: function () { return search_1.searchSpecRegistryEntries; } });
Object.defineProperty(exports, "selectDefaultSpecSearchEntries", { enumerable: true, get: function () { return search_1.selectDefaultSpecSearchEntries; } });
var add_safety_1 = require("./add-safety");
Object.defineProperty(exports, "applySpecRegistryAddSafety", { enumerable: true, get: function () { return add_safety_1.applySpecRegistryAddSafety; } });
var add_validation_1 = require("./add-validation");
Object.defineProperty(exports, "validateSpecRegistryAddInput", { enumerable: true, get: function () { return add_validation_1.validateSpecRegistryAddInput; } });
var duplicate_detection_2 = require("./duplicate-detection");
Object.defineProperty(exports, "findDuplicateSpecIds", { enumerable: true, get: function () { return duplicate_detection_2.findDuplicateSpecIds; } });
var structure_validation_2 = require("./structure-validation");
Object.defineProperty(exports, "validateSpecRegistryStructure", { enumerable: true, get: function () { return structure_validation_2.validateSpecRegistryStructure; } });
Object.defineProperty(exports, "validateSpecIndexDocumentStructure", { enumerable: true, get: function () { return structure_validation_2.validateSpecIndexDocumentStructure; } });
var destination_1 = require("./destination");
Object.defineProperty(exports, "assertSpecRegistryDestination", { enumerable: true, get: function () { return destination_1.assertSpecRegistryDestination; } });
Object.defineProperty(exports, "parseSpecRegistryDestination", { enumerable: true, get: function () { return destination_1.parseSpecRegistryDestination; } });
Object.defineProperty(exports, "validateSpecRegistryDestination", { enumerable: true, get: function () { return destination_1.validateSpecRegistryDestination; } });
var semantic_validation_2 = require("./semantic-validation");
Object.defineProperty(exports, "validateSpecRegistrySemanticMetadata", { enumerable: true, get: function () { return semantic_validation_2.validateSpecRegistrySemanticMetadata; } });
var routing_1 = require("./routing");
Object.defineProperty(exports, "assertSpecRegistryRouting", { enumerable: true, get: function () { return routing_1.assertSpecRegistryRouting; } });
Object.defineProperty(exports, "parseSpecRegistryRouting", { enumerable: true, get: function () { return routing_1.parseSpecRegistryRouting; } });
Object.defineProperty(exports, "validateSpecRegistryRouting", { enumerable: true, get: function () { return routing_1.validateSpecRegistryRouting; } });
var source_1 = require("./source");
Object.defineProperty(exports, "LOCAL_SPEC_SOURCE_TYPE", { enumerable: true, get: function () { return source_1.LOCAL_SPEC_SOURCE_TYPE; } });
Object.defineProperty(exports, "classifySpecRegistrySource", { enumerable: true, get: function () { return source_1.classifySpecRegistrySource; } });
Object.defineProperty(exports, "parseSpecRegistrySourceReference", { enumerable: true, get: function () { return source_1.parseSpecRegistrySourceReference; } });
Object.defineProperty(exports, "serializeSpecRegistrySourceReference", { enumerable: true, get: function () { return source_1.serializeSpecRegistrySourceReference; } });
var source_transition_1 = require("./source-transition");
Object.defineProperty(exports, "applySpecSourceTransition", { enumerable: true, get: function () { return source_transition_1.applySpecSourceTransition; } });
Object.defineProperty(exports, "parseSpecRegistrySourceHistory", { enumerable: true, get: function () { return source_transition_1.parseSpecRegistrySourceHistory; } });
Object.defineProperty(exports, "serializeSpecRegistrySourceHistory", { enumerable: true, get: function () { return source_transition_1.serializeSpecRegistrySourceHistory; } });
var traceability_1 = require("./traceability");
Object.defineProperty(exports, "formatLinkedSliceReferenceText", { enumerable: true, get: function () { return traceability_1.formatLinkedSliceReferenceText; } });
Object.defineProperty(exports, "parseLinkedSliceReference", { enumerable: true, get: function () { return traceability_1.parseLinkedSliceReference; } });
Object.defineProperty(exports, "parseRegistryTimestampField", { enumerable: true, get: function () { return traceability_1.parseRegistryTimestampField; } });
Object.defineProperty(exports, "validateLinkedSliceReference", { enumerable: true, get: function () { return traceability_1.validateLinkedSliceReference; } });
Object.defineProperty(exports, "serializeLinkedSliceReference", { enumerable: true, get: function () { return traceability_1.serializeLinkedSliceReference; } });
var types_1 = require("./types");
Object.defineProperty(exports, "SPEC_INDEX_ENTRY_OPTIONAL_FIELDS", { enumerable: true, get: function () { return types_1.SPEC_INDEX_ENTRY_OPTIONAL_FIELDS; } });
Object.defineProperty(exports, "SPEC_INDEX_ENTRY_REQUIRED_FIELDS", { enumerable: true, get: function () { return types_1.SPEC_INDEX_ENTRY_REQUIRED_FIELDS; } });
Object.defineProperty(exports, "SPEC_INDEX_ENTRY_ROUTED_REQUIRED_FIELDS", { enumerable: true, get: function () { return types_1.SPEC_INDEX_ENTRY_ROUTED_REQUIRED_FIELDS; } });
function validateSpecRegistryDuplicateIdsForDoctor(indexJson) {
    try {
        const indexDocument = (0, document_io_1.parseSpecIndexDocument)(indexJson);
        const duplicates = (0, duplicate_detection_1.findDuplicateSpecIds)(indexDocument.entries);
        if (duplicates.hasDuplicates) {
            return {
                status: "error",
                message: `Found ${duplicates.duplicateIdGroups.size} duplicate spec ID${duplicates.duplicateIdGroups.size === 1 ? "" : "s"} in the spec registry.`,
                findings: duplicates.findings
            };
        }
        return {
            status: "ok",
            message: "Spec registry contains no duplicate IDs.",
            findings: []
        };
    }
    catch (error) {
        return {
            status: "error",
            message: "Spec index could not be read or parsed.",
            findings: [
                {
                    code: "SPEC_REGISTRY_INDEX_UNREADABLE",
                    message: `Spec index could not be parsed: ${(0, read_helpers_1.toErrorMessage)(error, "Could not parse spec index.")}`,
                    severity: "error",
                    path: cli_constants_1.CLI_SPEC_INDEX_PATH
                }
            ]
        };
    }
}
function validateSpecRegistryStructureForDoctor(indexJson) {
    const documentStructureError = (0, structure_validation_1.validateSpecIndexDocumentStructure)(indexJson);
    if (documentStructureError !== null) {
        return {
            status: "error",
            message: "Spec index document structure is malformed.",
            findings: [documentStructureError]
        };
    }
    try {
        const indexDocument = indexJson;
        const rawEntries = indexDocument.specs;
        const validation = (0, structure_validation_1.validateSpecRegistryStructure)(rawEntries);
        if (validation.hasErrors) {
            return {
                status: "error",
                message: `Found ${validation.malformedEntries.length} spec index ${validation.malformedEntries.length === 1 ? "entry" : "entries"} with missing required metadata.`,
                findings: validation.findings
            };
        }
        // Preserve canonical parse as the final structure gate.
        (0, document_io_1.parseSpecIndexDocument)(indexJson);
        return {
            status: "ok",
            message: "All spec index entries have required metadata and valid structure.",
            findings: []
        };
    }
    catch (error) {
        return {
            status: "error",
            message: "Spec index structure and metadata validation failed.",
            findings: [
                {
                    code: "SPEC_REGISTRY_STRUCTURE_VALIDATION_FAILED",
                    message: `Spec index validation failed: ${(0, read_helpers_1.toErrorMessage)(error, "Could not validate spec index.")}`,
                    severity: "error",
                    path: cli_constants_1.CLI_SPEC_INDEX_PATH
                }
            ]
        };
    }
}
function validateSpecRegistrySemanticMetadataForDoctor(indexJson) {
    const documentStructureError = (0, structure_validation_1.validateSpecIndexDocumentStructure)(indexJson);
    if (documentStructureError !== null) {
        return {
            status: "informational",
            message: "Destination, source, and traceability validation is skipped until the spec index document structure is repaired.",
            findings: [
                {
                    code: "SPEC_REGISTRY_SEMANTIC_VALIDATION_SKIPPED",
                    message: "Destination, source, and traceability validation was skipped because the spec index document structure is malformed; repair structure findings first.",
                    severity: "informational",
                    path: cli_constants_1.CLI_SPEC_INDEX_PATH
                }
            ]
        };
    }
    try {
        const indexDocument = indexJson;
        const rawEntries = indexDocument.specs;
        const structureValidation = (0, structure_validation_1.validateSpecRegistryStructure)(rawEntries);
        if (structureValidation.hasErrors) {
            return {
                status: "informational",
                message: "Destination, source, and traceability validation is skipped for entries missing required metadata.",
                findings: [
                    {
                        code: "SPEC_REGISTRY_SEMANTIC_VALIDATION_SKIPPED",
                        message: "Destination, source, and traceability validation was skipped for malformed entries with missing required metadata; repair structure findings first.",
                        severity: "informational",
                        path: cli_constants_1.CLI_SPEC_INDEX_PATH
                    }
                ]
            };
        }
        const semanticValidation = (0, semantic_validation_1.validateSpecRegistrySemanticMetadata)(rawEntries);
        if (semanticValidation.hasErrors) {
            return {
                status: "error",
                message: `Found ${semanticValidation.findings.length} destination, source, or traceability issue${semanticValidation.findings.length === 1 ? "" : "s"} in the spec registry.`,
                findings: semanticValidation.findings
            };
        }
        return {
            status: "ok",
            message: "All spec index entries passed destination, source, and traceability validation.",
            findings: []
        };
    }
    catch (error) {
        return {
            status: "error",
            message: "Spec registry destination, source, and traceability validation failed.",
            findings: [
                {
                    code: "SPEC_REGISTRY_SEMANTIC_VALIDATION_FAILED",
                    message: `Spec registry semantic validation failed: ${(0, read_helpers_1.toErrorMessage)(error, "Could not validate destination, source, and traceability metadata.")}`,
                    severity: "error",
                    path: cli_constants_1.CLI_SPEC_INDEX_PATH
                }
            ]
        };
    }
}
