"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.runSpecRegistryDuplicateIdCheck = runSpecRegistryDuplicateIdCheck;
exports.runSpecRegistryStructureAndMetadataCheck = runSpecRegistryStructureAndMetadataCheck;
exports.runSpecRegistryDestinationSourceAndTraceabilityCheck = runSpecRegistryDestinationSourceAndTraceabilityCheck;
const node_fs_1 = __importDefault(require("node:fs"));
const path_resolution_1 = require("../spec-registry/path-resolution");
const spec_registry_1 = require("../spec-registry");
const local_paths_1 = require("./local-paths");
function runSpecRegistryDuplicateIdCheck(repositoryRoot, findings) {
    const specIndexPath = (0, path_resolution_1.resolveSpecIndexAbsolutePath)(repositoryRoot);
    if (!(0, local_paths_1.isExistingFile)(specIndexPath)) {
        return {
            id: "spec-registry-duplicate-ids",
            status: "ok",
            message: "Spec index does not exist; duplicate-id check is skipped."
        };
    }
    try {
        const indexContent = node_fs_1.default.readFileSync(specIndexPath, "utf8");
        const duplicateValidation = (0, spec_registry_1.validateSpecRegistryDuplicateIdsForDoctor)(JSON.parse(indexContent));
        for (const finding of duplicateValidation.findings) {
            findings.push(finding);
        }
        return {
            id: "spec-registry-duplicate-ids",
            status: duplicateValidation.status,
            message: duplicateValidation.message
        };
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : "Could not parse spec index.";
        findings.push({
            code: "SPEC_REGISTRY_INDEX_UNREADABLE",
            message: `Spec index could not be parsed: ${errorMessage}`,
            severity: "error",
            path: (0, local_paths_1.toRepositoryRelativePath)(repositoryRoot, specIndexPath)
        });
        return {
            id: "spec-registry-duplicate-ids",
            status: "error",
            message: "Spec index could not be read or parsed."
        };
    }
}
function runSpecRegistryStructureAndMetadataCheck(repositoryRoot, findings) {
    const specIndexPath = (0, path_resolution_1.resolveSpecIndexAbsolutePath)(repositoryRoot);
    if (!(0, local_paths_1.isExistingFile)(specIndexPath)) {
        return {
            id: "spec-registry-structure-and-metadata",
            status: "ok",
            message: "Spec index does not exist; structure and metadata check is skipped."
        };
    }
    try {
        const indexContent = node_fs_1.default.readFileSync(specIndexPath, "utf8");
        const structureValidation = (0, spec_registry_1.validateSpecRegistryStructureForDoctor)(JSON.parse(indexContent));
        for (const finding of structureValidation.findings) {
            findings.push(finding);
        }
        return {
            id: "spec-registry-structure-and-metadata",
            status: structureValidation.status,
            message: structureValidation.message
        };
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : "Could not validate spec index.";
        findings.push({
            code: "SPEC_REGISTRY_STRUCTURE_VALIDATION_FAILED",
            message: `Spec index validation failed: ${errorMessage}`,
            severity: "error",
            path: (0, local_paths_1.toRepositoryRelativePath)(repositoryRoot, specIndexPath)
        });
        return {
            id: "spec-registry-structure-and-metadata",
            status: "error",
            message: "Spec index structure and metadata validation failed."
        };
    }
}
function runSpecRegistryDestinationSourceAndTraceabilityCheck(repositoryRoot, findings) {
    const specIndexPath = (0, path_resolution_1.resolveSpecIndexAbsolutePath)(repositoryRoot);
    const relativeSpecIndexPath = (0, local_paths_1.toRepositoryRelativePath)(repositoryRoot, specIndexPath);
    if (!(0, local_paths_1.isExistingFile)(specIndexPath)) {
        return {
            id: "spec-registry-destination-source-and-traceability",
            status: "ok",
            message: "Spec index does not exist; destination, source, and traceability check is skipped."
        };
    }
    try {
        const indexContent = node_fs_1.default.readFileSync(specIndexPath, "utf8");
        const semanticValidation = (0, spec_registry_1.validateSpecRegistrySemanticMetadataForDoctor)(JSON.parse(indexContent));
        for (const finding of semanticValidation.findings) {
            findings.push({
                ...finding,
                path: finding.path ?? relativeSpecIndexPath
            });
        }
        return {
            id: "spec-registry-destination-source-and-traceability",
            status: semanticValidation.status,
            message: semanticValidation.message
        };
    }
    catch (error) {
        const errorMessage = error instanceof Error ? error.message : "Could not validate destination, source, and traceability metadata.";
        findings.push({
            code: "SPEC_REGISTRY_SEMANTIC_VALIDATION_FAILED",
            message: `Spec registry semantic validation failed: ${errorMessage}`,
            severity: "error",
            path: relativeSpecIndexPath
        });
        return {
            id: "spec-registry-destination-source-and-traceability",
            status: "error",
            message: "Spec registry destination, source, and traceability validation failed."
        };
    }
}
