"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
 */
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.decodeMeshoptBuffer = void 0;
const core_bentley_1 = require("@itwin/core-bentley");
const FrontendLoggerCategory_1 = require("../common/FrontendLoggerCategory");
/** Loads and configures the MeshoptDecoder module on demand. */
class Loader {
    constructor() {
        this._status = "uninitialized";
    }
    async getDecoder() {
        const status = this._status;
        switch (status) {
            case "failed":
                (0, core_bentley_1.assert)(undefined === this._decoder);
                (0, core_bentley_1.assert)(undefined === this._promise);
                return undefined;
            case "ready":
                (0, core_bentley_1.assert)(undefined !== this._decoder);
                (0, core_bentley_1.assert)(undefined === this._promise);
                return this._decoder;
            case "loading":
                (0, core_bentley_1.assert)(undefined !== this._promise);
                await this._promise;
                (0, core_bentley_1.assert)("failed" === this._status || "ready" === this._status);
                (0, core_bentley_1.assert)(undefined === this._promise);
                return this._decoder;
        }
        (0, core_bentley_1.assert)("uninitialized" === status);
        this._status = "loading";
        this._promise = this.load();
        return this.getDecoder();
    }
    async load() {
        try {
            // Import the module on first use.
            const decoder = (await Promise.resolve().then(() => __importStar(require("meshoptimizer")))).MeshoptDecoder;
            await decoder.ready;
            // Configure it to do the decoding outside of the main thread. No compelling reason to use more than one worker.
            decoder.useWorkers(1);
            this._status = "ready";
            this._decoder = decoder;
        }
        catch (err) {
            core_bentley_1.Logger.logException(FrontendLoggerCategory_1.FrontendLoggerCategory.Render, err);
            this._status = "failed";
        }
        finally {
            this._promise = undefined;
        }
    }
}
const loader = new Loader();
/** @internal */
async function decodeMeshoptBuffer(source, args) {
    const decoder = await loader.getDecoder();
    if (!decoder) {
        return undefined;
    }
    return decoder.decodeGltfBufferAsync(args.count, args.byteStride, source, args.mode, args.filter);
}
exports.decodeMeshoptBuffer = decodeMeshoptBuffer;
//# sourceMappingURL=MeshoptCompression.js.map