"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.LOCAL_SPEC_SOURCE_TYPE = void 0;
exports.classifySpecRegistrySource = classifySpecRegistrySource;
exports.parseSpecRegistrySourceReference = parseSpecRegistrySourceReference;
exports.serializeSpecRegistrySourceReference = serializeSpecRegistrySourceReference;
exports.LOCAL_SPEC_SOURCE_TYPE = "markdown";
function classifySpecRegistrySource(type) {
    return type === exports.LOCAL_SPEC_SOURCE_TYPE ? "local" : "remote";
}
function parseSpecRegistrySourceReference(input) {
    const source = readRecord(input, "Spec source");
    const type = readRequiredString(source, "type", "Spec source");
    const classification = classifySpecRegistrySource(type);
    const ref = readRequiredString(source, "ref", "Spec source");
    const additionalMetadata = {};
    for (const [key, value] of Object.entries(source)) {
        if (key !== "type" && key !== "ref") {
            additionalMetadata[key] = value;
        }
    }
    return {
        classification,
        type,
        ref,
        additionalMetadata
    };
}
function serializeSpecRegistrySourceReference(source) {
    const serialized = {
        type: source.type,
        ref: source.ref
    };
    for (const [key, value] of Object.entries(source.additionalMetadata)) {
        if (!(key in serialized)) {
            serialized[key] = value;
        }
    }
    return serialized;
}
function readRequiredString(record, field, label) {
    const value = record[field];
    if (typeof value !== "string" || value.trim() === "") {
        throw new TypeError(`${label} field '${field}' must be a non-empty string.`);
    }
    return value;
}
function readRecord(input, label) {
    if (typeof input !== "object" || input === null || Array.isArray(input)) {
        throw new TypeError(`${label} must be an object.`);
    }
    return input;
}
