"use strict";
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module Tiles
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.collectTransferables = exports.addPrimitiveTransferables = void 0;
function addTransferable(xfers, array) {
    if (array) {
        xfers.add(array.buffer);
    }
}
/** @internal */
function addPrimitiveTransferables(xfers, primitive) {
    if (primitive.type === "pattern") {
        addTransferable(xfers, primitive.params.xyOffsets);
        return;
    }
    addTransferable(xfers, primitive.params.vertices.data);
    if (primitive.modifier?.type === "instances") {
        addTransferable(xfers, primitive.modifier.transforms);
        addTransferable(xfers, primitive.modifier.featureIds);
        addTransferable(xfers, primitive.modifier.symbologyOverrides);
    }
    switch (primitive.type) {
        case "point":
            addTransferable(xfers, primitive.params.indices);
            break;
        case "polyline":
            addTransferable(xfers, primitive.params.polyline.indices);
            addTransferable(xfers, primitive.params.polyline.prevIndices);
            addTransferable(xfers, primitive.params.polyline.nextIndicesAndParams);
            break;
        case "mesh":
            addTransferable(xfers, primitive.params.surface.indices);
            const edges = primitive.params.edges;
            if (edges) {
                addTransferable(xfers, edges.segments?.indices);
                addTransferable(xfers, edges.segments?.endPointAndQuadIndices);
                addTransferable(xfers, edges.silhouettes?.indices);
                addTransferable(xfers, edges.silhouettes?.endPointAndQuadIndices);
                addTransferable(xfers, edges.silhouettes?.normalPairs);
                addTransferable(xfers, edges.polylines?.indices);
                addTransferable(xfers, edges.polylines?.prevIndices);
                addTransferable(xfers, edges.polylines?.nextIndicesAndParams);
                addTransferable(xfers, edges.indexed?.indices);
                addTransferable(xfers, edges.indexed?.edges.data);
            }
            break;
    }
}
exports.addPrimitiveTransferables = addPrimitiveTransferables;
/** Collect an array of all the [transferable objects](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Transferable_objects)
 * within the specified document.
 * @internal
 */
function collectTransferables(document) {
    const xfers = new Set();
    const add = (array) => {
        if (array)
            xfers.add(array.buffer);
    };
    add(document.binaryData);
    add(document.featureTable.data);
    add(document.featureTable.animationNodeIds);
    for (const node of document.nodes) {
        if (undefined !== node.groupId) {
            for (const primNode of node.nodes)
                for (const primitive of primNode.primitives)
                    addPrimitiveTransferables(xfers, primitive);
        }
        else {
            for (const primitive of node.primitives)
                addPrimitiveTransferables(xfers, primitive);
        }
    }
    for (const primitives of document.patterns.values())
        for (const primitive of primitives)
            addPrimitiveTransferables(xfers, primitive);
    return Array.from(xfers);
}
exports.collectTransferables = collectTransferables;
//# sourceMappingURL=ImdlModel.js.map