/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { fail } from "@fluidframework/core-utils/internal";
import { Counter, jsonMinimizingFilter, } from "./chunkCodecUtilities.js";
/**
 * An identifier which can be compressed using {@link Counter}.
 *
 * @remarks
 * Compression of Identifiers is done after the output is otherwise generated to enable counting all the usages.
 * To avoid having to decode the decode the data array to determine which data is an identifier and which is some other string,
 * some recognizable representation is required.
 * Using a class and checking its prototype works for this, and is why IdentifierToken is a class.
 */
export class IdentifierToken {
    constructor(identifier) {
        this.identifier = identifier;
    }
}
/**
 * Takes already encoded content in `buffer` and deeply searches, replacing each `IdentifierToken` and `Shape`
 * with references to their encodings in the returned `EncodedFieldBatchGeneric`.
 *
 * This looks inside nested arrays (including transitively) but not inside objects.
 *
 * Note that this modifies `buffer` to avoid having to copy it.
 */
export function updateShapesAndIdentifiersEncoding(version, buffer, identifierFilter = jsonMinimizingFilter) {
    const identifiers = new Counter();
    const shapes = new Counter();
    // Shapes can reference other shapes (and identifiers), so we need to traverse the shape graph.
    // These collections enable that.
    const shapesSeen = new Set();
    const shapeToCount = [];
    const shapeDiscovered = (shape) => {
        shapes.add(shape);
        if (!shapesSeen.has(shape)) {
            shapesSeen.add(shape);
            shapeToCount.push(shape);
        }
    };
    // A collection of all arrays transitively reachable (via arrays) from buffer.
    // These are all the arrays whose contents are searched for shapes and identifiers which are then replaced
    // with references. This uses a breadth first traversal, but the actual order is not important.
    const arrays = [buffer];
    for (const array of arrays) {
        for (const item of array) {
            if (item instanceof IdentifierToken) {
                identifiers.add(item.identifier);
            }
            else if (item instanceof Shape) {
                shapeDiscovered(item);
            }
            else if (Array.isArray(item)) {
                // In JS it is legal to push items to an array which is being iterated,
                // and they will be visited in order.
                arrays.push(item);
            }
            else if (item !== null &&
                typeof item === "object" &&
                item.shape instanceof Shape) {
                // because "serializable" is allowed in buffer and it has type `any`, its very easy to mess up including of shapes in the buffer.
                // This catches the easiest way to get it wrong.
                fail(0xb4b /* encoder interface instead of shape written to stream */);
            }
        }
    }
    // Traverse shape graph, discovering and counting all shape to shape and shape to identifier references.
    {
        let shape;
        while ((shape = shapeToCount.pop()) !== undefined) {
            shape.countReferencedShapesAndIdentifiers(identifiers, shapeDiscovered);
        }
    }
    // Determine substitutions for identifiers and shapes:
    const identifierTable = identifiers.buildTable(identifierFilter);
    const shapeTable = shapes.buildTable();
    for (const array of arrays) {
        for (let index = 0; index < array.length; index++) {
            const item = array[index];
            if (item instanceof IdentifierToken) {
                array[index] = identifierTable.valueToIndex.get(item.identifier) ?? item.identifier;
            }
            else if (item instanceof Shape) {
                array[index] = shapeTable.valueToIndex.get(item) ?? fail(0xb4c /* missing shape */);
            }
        }
    }
    const encodedShapes = shapeTable.indexToValue.map((shape) => shape.encodeShape(identifierTable, shapeTable));
    return {
        version,
        // TODO: fix readonly typing issues to remove this cast.
        identifiers: identifierTable.indexToValue,
        shapes: encodedShapes,
        data: buffer,
    };
}
/**
 * A tree shape.
 * This similar to a schema for a tree, though it may be more or less specific than the actual schema system used.
 * Can be encoded into a `TEncodedShape`: when doing so encodes references to shapes (if needed) using {@link Counter}:
 * this can include recursive references (direct or indirect).
 *
 * @remarks
 * Deduplication of shapes is done after the output is otherwise generated (including identifier dictionary encoding).
 * To avoid having to decode the data array to determine which data is a shape and which is some other object,
 * some recognizable representation is required.
 * Using a class and checking its prototype works for this, and is why Shape is a class.
 *
 * Note that deduplication compares shapes by object identity not by content, so encoders must ensure shapes are not duplicated to achieve efficient encoding.
 * Comparison by content would be difficult due to shape containing references to other shapes.
 *
 * @privateRemarks
 * Unlike with identifiers, conversion from the initial form (this class / IdentifierToken) is done by the `encodeShape` method, not by general purpose logic in `updateShapesAndIdentifiersEncoding`.
 * For `updateShapesAndIdentifiersEncoding` to do the conversion without help from `encodeShape`,
 * instances of this Shape class would have to either be or output an object that is identical to the `TEncodedShape` format except with all shape references as object references instead of indexes.
 * Those objects would have to be deeply traversed looking for shape objects to replace with reference indexes.
 * This is possible, but making it type safe would involve generating derived types from the `TEncodedShape` deeply replacing any shape references, as well as requiring deep traversal of all objects in the encoded output.
 * Such an approach seemed less maintainable and readable than the design taken here which avoids the need for those derived types.
 */
export class Shape {
}
//# sourceMappingURL=chunkEncodingGeneric.js.map