"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.addSchemaDependency = addSchemaDependency;
exports.addSchema = addSchema;
exports.initializeSerialization = initializeSerialization;
exports.getVerifyFunction = getVerifyFunction;
const ajv_1 = __importDefault(require("ajv"));
const ajv = new ajv_1.default();
const dependencyMap = new Map();
const schemaMap = new Map();
const verifyFunctions = new Map();
function addSchemaDependency(name, requiredBy) {
    dependencyMap.set(requiredBy, name);
}
function addSchema(name, schema) {
    if (schemaMap.has(name)) {
        throw new Error(`error already registered ${name}`);
    }
    schemaMap.set(name, schema);
}
function initializeSerialization() {
    for (const [key, value] of dependencyMap.entries()) {
        const schema = schemaMap.get(value);
        if (schema != null) {
            ajv.addSchema(schema, value);
        }
        else {
            throw new Error(`error missing schema ${value} required by ${key}`);
        }
    }
}
function getVerifyFunction(name) {
    const existingFn = verifyFunctions.get(name);
    if (existingFn) {
        return existingFn;
    }
    const schema = schemaMap.get(name);
    if (!schema) {
        throw new Error(`error missing schema ${name}`);
    }
    const verifyFn = ajv.compile(schema);
    verifyFunctions.set(name, verifyFn);
    return verifyFn;
}
//# sourceMappingURL=SchemaHelpers.js.map