"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateArchiveSourceEntry = validateArchiveSourceEntry;
const cli_constants_1 = require("../config/cli-constants");
const ACTIVE_SPEC_REGISTRY_STATUS = "active";
const ARCHIVED_SPEC_REGISTRY_STATUS = "archived";
function validateArchiveSourceEntry(entryId, currentDocument) {
    const targetIndex = currentDocument.entries.findIndex((entry) => entry.id === entryId);
    if (targetIndex === -1) {
        return {
            ok: false,
            findings: [
                createFinding("SPEC_ARCHIVE_ENTRY_NOT_FOUND", `Spec id '${entryId}' does not exist in the registry index.`, "error", cli_constants_1.CLI_SPEC_INDEX_PATH)
            ]
        };
    }
    const targetEntry = currentDocument.entries[targetIndex];
    if (targetEntry.status === ARCHIVED_SPEC_REGISTRY_STATUS) {
        return {
            ok: false,
            findings: [
                createFinding("SPEC_ARCHIVE_ALREADY_ARCHIVED", `Spec id '${entryId}' is already archived.`, "error", cli_constants_1.CLI_SPEC_INDEX_PATH)
            ]
        };
    }
    if (targetEntry.status !== ACTIVE_SPEC_REGISTRY_STATUS) {
        return {
            ok: false,
            findings: [
                createFinding("SPEC_ARCHIVE_INVALID_SOURCE_STATUS", `Spec id '${entryId}' must be in status '${ACTIVE_SPEC_REGISTRY_STATUS}' before it can be archived; received '${targetEntry.status}'.`, "error", cli_constants_1.CLI_SPEC_INDEX_PATH)
            ]
        };
    }
    return {
        ok: true,
        targetIndex,
        targetEntry
    };
}
function createFinding(code, message, severity, path) {
    const finding = {
        code,
        message,
        severity
    };
    if (path !== undefined) {
        finding.path = path;
    }
    return finding;
}
