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