"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 core_1 = require("../../core");
const util_1 = require("../../util");
const codec_1 = require("../../codec");
const format_1 = require("./format");
function encodeRepo(repo) {
    const nodeSchema = Object.create(null);
    const rootFieldSchema = (0, core_1.encodeFieldSchema)(repo.rootFieldSchema);
    for (const name of [...repo.nodeSchema.keys()].sort()) {
        const schema = repo.nodeSchema.get(name) ?? (0, util_1.fail)("missing schema");
        Object.defineProperty(nodeSchema, name, {
            enumerable: true,
            configurable: true,
            writable: true,
            value: (0, util_1.fromErased)(schema.encode()),
        });
    }
    return {
        version: core_1.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, util_1.brand)(key), core_1.storedSchemaDecodeDispatcher.dispatch(schema));
    }
    return {
        rootFieldSchema: (0, core_1.decodeFieldSchema)(f.root),
        nodeSchema,
    };
}
/**
 * Creates a codec which performs synchronous monolithic encoding of schema content.
 */
function makeSchemaCodec(options) {
    return (0, codec_1.makeVersionedValidatedCodec)(options, new Set([core_1.schemaFormat.version]), format_1.Format, {
        encode: (data) => encodeRepo(data),
        decode: (data) => decode(data),
    });
}
exports.makeSchemaCodec = makeSchemaCodec;
//# sourceMappingURL=codec.js.map