"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedChunkShapeV1 = exports.shapesV1 = exports.EncodedNodeShape = exports.EncodedValueShape = exports.SpecialField = exports.EncodedFieldShape = exports.EncodedAnyShape = exports.EncodedInlineArrayShape = exports.EncodedNestedArrayShape = void 0;
const typebox_1 = require("@sinclair/typebox");
const index_js_1 = require("../../../../codec/index.js");
const formatGeneric_js_1 = require("./formatGeneric.js");
exports.EncodedNestedArrayShape = formatGeneric_js_1.ShapeIndex;
exports.EncodedInlineArrayShape = typebox_1.Type.Object({
    length: formatGeneric_js_1.Count,
    /**
     * All entries are this shape.
     */
    shape: formatGeneric_js_1.ShapeIndex,
}, { additionalProperties: false });
exports.EncodedAnyShape = typebox_1.Type.Literal(0);
exports.EncodedFieldShape = typebox_1.Type.Tuple([
    /**
     * Field key for this field.
     */
    formatGeneric_js_1.IdentifierOrIndex,
    /**
     * Shape of data in this field.
     */
    formatGeneric_js_1.ShapeIndex,
]);
/**
 * Used in {@link EncodedValueShape} for special field kind handling.
 */
var SpecialField;
(function (SpecialField) {
    /**
     * Special case for Identifier field kind.
     */
    SpecialField[SpecialField["Identifier"] = 0] = "Identifier";
})(SpecialField || (exports.SpecialField = SpecialField = {}));
/**
 * Shape of a value on a node.
 *
 * Due to limitations of TypeBox and differences between JavaScript objects, TypeScript types and JSON,
 * the case where no information about the value is captured in the shape is a bit confusing.
 * In TypeBox this is allowed by the user of this type putting it in an optional property.
 * In TypeScript it is modeled using `undefined`.
 * In JavaScript the property may be missing or explicitly `undefined`.
 * In JSON this will serialize as the property being omitted.
 * In this case, the value will be encoded as either:
 * - `false` (when there is no value) OR
 * - `true, value` when there is a value.
 *
 * For a more compact encoding, there are 4 options for the shape:
 * - `true`: there is a value, and it will simply be encoded by putting it in the output buffer (so `value`).
 * - `false`: there is never a value, and it takes up no space in the output buffer.
 * - `[value]`: there is a value, and its always the same.
 * - `SpecialField.Identifier`: special case for node identifier handling.
 * Takes up no space in the output buffer: the value comes from the shape arrays's content.
 * It is wrapped in an array to differentiate value shape types.
 *
 * In the future other value shape formats may be added, likely as objects.
 *
 * @remarks
 * See {@link EncodedNodeShape} for usage.
 */
exports.EncodedValueShape = typebox_1.Type.Union([
    typebox_1.Type.Boolean(),
    typebox_1.Type.Array(typebox_1.Type.Any(), { minItems: 1, maxItems: 1 }),
    typebox_1.Type.Enum(SpecialField),
    // TODO: support delta encoding and/or special node identifier handling
    // EncodedCounter,
]);
exports.EncodedNodeShape = typebox_1.Type.Object({
    /**
     * If not provided, inlined in data.
     */
    type: typebox_1.Type.Optional(formatGeneric_js_1.IdentifierOrIndex),
    value: typebox_1.Type.Optional(exports.EncodedValueShape),
    /**
     * Fields with fixed (per key) shapes.
     * They are encoded in the order they are specified here.
     * To ensure the order is preserved, this is an array instead of an object with keys.
     */
    fields: typebox_1.Type.Optional(typebox_1.Type.Array(exports.EncodedFieldShape)),
    /**
     * If undefined, no data. Otherwise, nested array of `[key, ...data]*`
     * Covers any fields beyond those in `fields`.
     */
    extraFields: typebox_1.Type.Optional(formatGeneric_js_1.ShapeIndex),
}, { additionalProperties: false });
/**
 * The chunk shapes supported by the V1 format.
 * @remarks
 * See {@link EncodedChunkShapeV1}.
 */
exports.shapesV1 = {
    /**
     * {@link EncodedNestedArrayShape} union member.
     */
    a: typebox_1.Type.Optional(exports.EncodedNestedArrayShape),
    /**
     * {@link EncodedInlineArrayShape} union member.
     */
    b: typebox_1.Type.Optional(exports.EncodedInlineArrayShape),
    /**
     * {@link EncodedNodeShape} union member.
     */
    c: typebox_1.Type.Optional(exports.EncodedNodeShape),
    /**
     * {@link EncodedAnyShape} union member.
     */
    d: typebox_1.Type.Optional(exports.EncodedAnyShape),
};
exports.EncodedChunkShapeV1 = typebox_1.Type.Object({ ...exports.shapesV1, e: typebox_1.Type.Optional(typebox_1.Type.Never()) }, index_js_1.unionOptions);
//# sourceMappingURL=formatV1.js.map