"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applySpecRegistryArchive = applySpecRegistryArchive;
const archive_status_1 = require("./archive-status");
const archive_write_1 = require("./archive-write");
const traceability_1 = require("./traceability");
function applySpecRegistryArchive(input) {
    const entryId = readEntryId(input.entryId);
    const sourceValidation = (0, archive_status_1.validateArchiveSourceEntry)(entryId, input.currentDocument);
    if (!sourceValidation.ok) {
        return {
            ok: false,
            findings: sourceValidation.findings
        };
    }
    const archiveTimestamp = (0, traceability_1.parseRegistryTimestampField)(input.archivedAt ?? new Date().toISOString(), "archived_at");
    const archivedEntry = (0, archive_write_1.archiveSpecRegistryEntry)(sourceValidation.targetEntry, archiveTimestamp);
    return {
        ok: true,
        entry: archivedEntry,
        document: (0, archive_write_1.buildArchivedRegistryDocument)(input.currentDocument, sourceValidation.targetIndex, archivedEntry)
    };
}
function readEntryId(entryId) {
    if (typeof entryId !== "string" || entryId.trim() === "") {
        throw new TypeError("Spec archive entry id must be a non-empty string.");
    }
    return entryId;
}
