/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { unreachableCase } from "@fluidframework/core-utils";
import { ValueSchema, ObjectNodeStoredSchema, LeafNodeStoredSchema, MapNodeStoredSchema, } from "../../../core/index.js";
import { fail } from "../../../util/index.js";
import { Multiplicity } from "../../multiplicity.js";
import { EncoderCache, anyNodeEncoder, asFieldEncoder, compressedEncode, } from "./compressedEncode.js";
import { NodeShape } from "./nodeShape.js";
/**
 * Encode data from `fieldBatch` in into an `EncodedChunk`.
 *
 * Optimized for encoded size and encoding performance.
 * TODO: This function should eventually also take in the root FieldSchema to more efficiently compress the nodes.
 */
export function schemaCompressedEncode(schema, policy, fieldBatch) {
    return compressedEncode(fieldBatch, buildCache(schema, policy));
}
export function buildCache(schema, policy) {
    const cache = new EncoderCache((fieldHandler, schemaName) => treeShaper(schema, policy, fieldHandler, schemaName), (treeHandler, field) => fieldShaper(treeHandler, field, cache), policy.fieldKinds);
    return cache;
}
/**
 * Selects shapes to use to encode fields.
 */
export function fieldShaper(treeHandler, field, cache) {
    const kind = cache.fieldShapes.get(field.kind.identifier) ?? fail("missing FieldKind");
    const type = oneFromSet(field.types);
    const nodeEncoder = type !== undefined ? treeHandler.shapeFromTree(type) : anyNodeEncoder;
    // eslint-disable-next-line unicorn/prefer-ternary
    if (kind.multiplicity === Multiplicity.Single) {
        return asFieldEncoder(nodeEncoder);
    }
    else {
        return cache.nestedArray(nodeEncoder);
    }
}
/**
 * Selects shapes to use to encode trees.
 */
export function treeShaper(fullSchema, policy, fieldHandler, schemaName) {
    const schema = fullSchema.nodeSchema.get(schemaName) ?? fail("missing node schema");
    if (schema instanceof ObjectNodeStoredSchema) {
        // TODO:Performance:
        // consider moving some optional and sequence fields to extra fields if they are commonly empty
        // to reduce encoded size.
        const objectNodeFields = [];
        for (const [key, field] of schema.objectNodeFields ?? []) {
            objectNodeFields.push({ key, shape: fieldHandler.shapeFromField(field) });
        }
        const shape = new NodeShape(schemaName, false, objectNodeFields, undefined);
        return shape;
    }
    if (schema instanceof LeafNodeStoredSchema) {
        const shape = new NodeShape(schemaName, valueShapeFromSchema(schema.leafValue), [], undefined);
        return shape;
    }
    if (schema instanceof MapNodeStoredSchema) {
        const shape = new NodeShape(schemaName, false, [], fieldHandler.shapeFromField(schema.mapFields));
        return shape;
    }
    fail("unsupported node kind");
}
export function oneFromSet(set) {
    if (set === undefined) {
        return undefined;
    }
    if (set.size !== 1) {
        return undefined;
    }
    for (const item of set) {
        return item;
    }
}
function valueShapeFromSchema(schema) {
    switch (schema) {
        case undefined:
            return false;
        case ValueSchema.Number:
        case ValueSchema.String:
        case ValueSchema.Boolean:
        case ValueSchema.FluidHandle:
            return true;
        case ValueSchema.Null:
            return [null];
        default:
            unreachableCase(schema);
    }
}
//# sourceMappingURL=schemaBasedEncoding.js.map