"use strict";
/**
 * WASM-based LZMA2/XZ compression using liblzma compiled via Emscripten.
 * The WASM module is lazy-loaded on first use.
 */
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.lzmaWasmCompress = lzmaWasmCompress;
exports.lzmaWasmDecompress = lzmaWasmDecompress;
let modulePromise = null;
function getModule() {
    if (!modulePromise) {
        modulePromise = (() => __awaiter(this, void 0, void 0, function* () {
            // Dynamic import of the Emscripten glue code
            const factory = (yield Promise.resolve().then(() => require('../wasm/lzma/lzma.js'))).default;
            return factory();
        }))();
    }
    return modulePromise;
}
function lzmaWasmCompress(data_1) {
    return __awaiter(this, arguments, void 0, function* (data, preset = 6) {
        const mod = yield getModule();
        const input = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
        const inputPtr = mod._malloc(input.length);
        if (!inputPtr)
            throw new Error('WASM malloc failed for input');
        try {
            mod.HEAPU8.set(input, inputPtr);
            const ret = mod._lzma_wasm_compress(inputPtr, input.length, preset);
            if (ret !== 0) {
                mod._free_result();
                throw new Error(`LZMA WASM compress failed with code ${ret}`);
            }
            const resultPtr = mod._get_result_ptr();
            const resultLen = mod._get_result_len();
            const output = new Uint8Array(resultLen);
            output.set(mod.HEAPU8.subarray(resultPtr, resultPtr + resultLen));
            mod._free_result();
            return output.buffer;
        }
        finally {
            mod._free(inputPtr);
        }
    });
}
function lzmaWasmDecompress(data) {
    return __awaiter(this, void 0, void 0, function* () {
        const mod = yield getModule();
        const input = data instanceof ArrayBuffer ? new Uint8Array(data) : data;
        const inputPtr = mod._malloc(input.length);
        if (!inputPtr)
            throw new Error('WASM malloc failed for input');
        try {
            mod.HEAPU8.set(input, inputPtr);
            const ret = mod._lzma_wasm_decompress(inputPtr, input.length);
            if (ret !== 0) {
                mod._free_result();
                throw new Error(`LZMA WASM decompress failed with code ${ret}`);
            }
            const resultPtr = mod._get_result_ptr();
            const resultLen = mod._get_result_len();
            const output = new Uint8Array(resultLen);
            output.set(mod.HEAPU8.subarray(resultPtr, resultPtr + resultLen));
            mod._free_result();
            return output.buffer;
        }
        finally {
            mod._free(inputPtr);
        }
    });
}
//# sourceMappingURL=lzma-wasm.js.map