"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.EncodedFieldBatch = exports.EncodedChunkShape = exports.EncodedTreeShape = exports.EncodedValueShape = exports.EncodedCounter = exports.EncodedFieldShape = exports.EncodedAnyShape = exports.EncodedInlineArray = exports.EncodedNestedArray = exports.validVersions = exports.version = void 0;
const typebox_1 = require("@sinclair/typebox");
const index_js_1 = require("../../../codec/index.js");
const formatGeneric_js_1 = require("./formatGeneric.js");
// TODO: Versions from here and schemaIndexFormat.ts should eventually be deduplicated into one version.
exports.version = 1.0;
// Compatible versions used for format/version validation.
// TODO: A proper version update policy will need to be documented.
exports.validVersions = new Set([exports.version]);
/**
 * Top level length is implied from length of data array.
 * All content are of this shape.
 */
exports.EncodedNestedArray = formatGeneric_js_1.ShapeIndex;
/**
 * Inline array.
 */
exports.EncodedInlineArray = typebox_1.Type.Object({
    length: formatGeneric_js_1.Count,
    /**
     * All entries are this shape.
     */
    shape: formatGeneric_js_1.ShapeIndex,
}, { additionalProperties: false });
/**
 * Encoded as `shape, ...[data for shape]`.
 *
 * Used for polymorphism.
 */
exports.EncodedAnyShape = typebox_1.Type.Literal(0);
/**
 * Content of the encoded field is specified by the Shape referenced by the ShapeIndex.
 * This is a tuple for conciseness.
 */
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,
]);
var CounterRelativeTo;
(function (CounterRelativeTo) {
    // Relative to previous node of same type in depth first pre-order traversal.
    CounterRelativeTo[CounterRelativeTo["PreviousNodeOfType_DepthFirstPreOrder"] = 0] = "PreviousNodeOfType_DepthFirstPreOrder";
    // TODO: add alternative relative mode relative to previous note at a path to allow delta encoded sequences (like points where x and y are delta encoded relative to previous points).
})(CounterRelativeTo || (CounterRelativeTo = {}));
var CounterMode;
(function (CounterMode) {
    CounterMode[CounterMode["Number"] = 0] = "Number";
    // TODO: document wrap modes and bit skipping. Note UUID subVersion here (ex: UUIDv4)
    CounterMode[CounterMode["UUID"] = 1] = "UUID";
})(CounterMode || (CounterMode = {}));
/**
 * Delta encoded value relative to a previous node's value.
 */
exports.EncodedCounter = typebox_1.Type.Object({
    relativeTo: typebox_1.Type.Enum(CounterRelativeTo),
    // If not provided, delta inline in data.
    delta: typebox_1.Type.Optional(typebox_1.Type.Number()),
    mode: typebox_1.Type.Enum(CounterMode),
}, { additionalProperties: false });
/**
 * 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 three 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.
 * 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 EncodedTreeShape} for usage.
 */
exports.EncodedValueShape = typebox_1.Type.Union([
    typebox_1.Type.Boolean(),
    typebox_1.Type.Array(typebox_1.Type.Any(), { minItems: 1, maxItems: 1 }),
    // TODO: support delta encoding and/or special node identifier handling
    // EncodedCounter,
]);
exports.EncodedTreeShape = 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 });
/**
 * Discriminated union of chunk shapes.
 *
 * See {@link DiscriminatedUnionDispatcher} for more information on this pattern.
 */
exports.EncodedChunkShape = typebox_1.Type.Object({
    /**
     * {@link EncodedNestedArray} union member.
     */
    a: typebox_1.Type.Optional(exports.EncodedNestedArray),
    /**
     * {@link EncodedInlineArray} union member.
     */
    b: typebox_1.Type.Optional(exports.EncodedInlineArray),
    /**
     * {@link EncodedTreeShape} union member.
     */
    c: typebox_1.Type.Optional(exports.EncodedTreeShape),
    /**
     * {@link EncodedAnyShape} union member.
     */
    d: typebox_1.Type.Optional(exports.EncodedAnyShape),
}, index_js_1.unionOptions);
exports.EncodedFieldBatch = (0, formatGeneric_js_1.EncodedFieldBatchGeneric)(exports.version, exports.EncodedChunkShape);
//# sourceMappingURL=format.js.map