"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeSchemaCodec = exports.encodeRepo = void 0;
const index_js_1 = require("../../codec/index.js");
const index_js_2 = require("../../core/index.js");
const index_js_3 = require("../../util/index.js");
const format_js_1 = require("./format.js");
function encodeRepo(repo) {
    const nodeSchema = Object.create(null);
    const rootFieldSchema = (0, index_js_2.encodeFieldSchema)(repo.rootFieldSchema);
    for (const name of [...repo.nodeSchema.keys()].sort()) {
        const schema = repo.nodeSchema.get(name) ?? (0, index_js_3.fail)("missing schema");
        Object.defineProperty(nodeSchema, name, {
            enumerable: true,
            configurable: true,
            writable: true,
            value: (0, index_js_2.toTreeNodeSchemaDataFormat)(schema.encode()),
        });
    }
    return {
        version: index_js_2.schemaFormat.version,
        nodes: nodeSchema,
        root: rootFieldSchema,
    };
}
exports.encodeRepo = encodeRepo;
function decode(f) {
    const nodeSchema = new Map();
    for (const [key, schema] of Object.entries(f.nodes)) {
        nodeSchema.set((0, index_js_3.brand)(key), index_js_2.storedSchemaDecodeDispatcher.dispatch(schema));
    }
    return {
        rootFieldSchema: (0, index_js_2.decodeFieldSchema)(f.root),
        nodeSchema,
    };
}
/**
 * Creates a codec which performs synchronous monolithic encoding of schema content.
 */
function makeSchemaCodec(options) {
    return (0, index_js_1.makeVersionedValidatedCodec)(options, new Set([index_js_2.schemaFormat.version]), format_js_1.Format, {
        encode: (data) => encodeRepo(data),
        decode: (data) => decode(data),
    });
}
exports.makeSchemaCodec = makeSchemaCodec;
//# sourceMappingURL=codec.js.map