/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
 * @module WebGL
 */
import { BeEvent } from "@itwin/core-bentley";
import { initializeDefaultPatterns, initializeLineCodeCapacity, lineCodeFromLinePixels, lineCodeTextureCapacity, lineCodeTextureSize, onLineCodeAssigned, resetLineCodeState, } from "../../../common/internal/render/LineCode";
/** Describes one of the pre-defined line patterns. See Render.LinePixels.
 * @internal
 */
export var LineCode;
(function (LineCode) {
    function valueFromLinePixels(pixels) {
        return lineCodeFromLinePixels(pixels);
    }
    LineCode.valueFromLinePixels = valueFromLinePixels;
    function initializeCapacity(maxTexSize) {
        initializeLineCodeCapacity(maxTexSize);
        // Recreate texture data array with the new capacity
        textureData = new Uint8Array(LineCode.size * capacity());
        // Reset and reassign default patterns with new capacity
        isInitializing = true;
        resetLineCodeState();
        initializeDefaultPatterns();
        isInitializing = false;
    }
    LineCode.initializeCapacity = initializeCapacity;
    LineCode.solid = 0;
    LineCode.size = lineCodeTextureSize;
    function capacity() {
        return lineCodeTextureCapacity();
    }
    LineCode.capacity = capacity;
    let textureData;
    const textureUpdated = new BeEvent();
    // Initialize with default capacity for tests that don't create a full System
    function ensureTextureData() {
        if (undefined === textureData) {
            textureData = new Uint8Array(LineCode.size * capacity());
            initializeDefaultPatterns();
        }
    }
    function writeRow(code, pattern) {
        ensureTextureData();
        if (code < 0 || code >= capacity())
            return;
        const offset = code * LineCode.size;
        for (let i = 0; i < LineCode.size; i++) {
            const bit = (pattern >>> i) & 0x1;
            const data = textureData;
            if (data)
                data[offset + i] = bit ? 0xff : 0x00;
        }
    }
    let isInitializing = false;
    onLineCodeAssigned((args) => {
        writeRow(args.code, args.pattern);
        if (!isInitializing)
            textureUpdated.raiseEvent();
    });
    function getTextureData() {
        ensureTextureData();
        // textureData is guaranteed to be defined after ensureTextureData()
        return textureData ?? new Uint8Array(0);
    }
    LineCode.getTextureData = getTextureData;
    function onTextureUpdated(listener) {
        textureUpdated.addListener(listener);
        return () => textureUpdated.removeListener(listener);
    }
    LineCode.onTextureUpdated = onTextureUpdated;
})(LineCode || (LineCode = {}));
//# sourceMappingURL=LineCode.js.map