"use strict";
/**
 * Node.js compression utilities using native zlib
 *
 * Uses zlib module (C++ implementation, fastest) with "deflate-raw" format
 * (raw DEFLATE without zlib header/trailer, required for ZIP files)
 */
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 () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasCompressionStream = void 0;
exports.compress = compress;
exports.compressSync = compressSync;
exports.decompress = decompress;
exports.decompressSync = decompressSync;
const util_1 = require("util");
const zlib = __importStar(require("zlib"));
const defaults_1 = require("../defaults.js");
// Re-export shared types and utilities
var compress_base_1 = require("./compress.base.js");
Object.defineProperty(exports, "hasCompressionStream", { enumerable: true, get: function () { return compress_base_1.hasCompressionStream; } });
function uint8ArrayToBufferView(data) {
    return Buffer.from(data.buffer, data.byteOffset, data.byteLength);
}
const deflateRawAsync = (0, util_1.promisify)(zlib.deflateRaw);
const inflateRawAsync = (0, util_1.promisify)(zlib.inflateRaw);
const compress_base_2 = require("./compress.base.js");
/**
 * Compress data using Node.js native zlib
 *
 * @param data - Data to compress
 * @param options - Compression options
 * @returns Compressed data
 *
 * @example
 * ```ts
 * const data = new TextEncoder().encode("Hello, World!");
 * const compressed = await compress(data, { level: 6 });
 * ```
 */
async function compress(data, options = {}) {
    const level = options.level ?? defaults_1.DEFAULT_COMPRESS_LEVEL;
    const thresholdBytes = (0, compress_base_2.resolveCompressThresholdBytes)(options);
    // Level 0 means no compression
    if (level === 0) {
        return data;
    }
    // Small-input fast path: avoid threadpool overhead.
    if (data.byteLength <= thresholdBytes) {
        const result = zlib.deflateRawSync(uint8ArrayToBufferView(data), { level });
        return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
    }
    const result = await deflateRawAsync(uint8ArrayToBufferView(data), { level });
    return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
}
/**
 * Compress data synchronously using Node.js zlib
 *
 * @param data - Data to compress
 * @param options - Compression options
 * @returns Compressed data
 */
function compressSync(data, options = {}) {
    const level = options.level ?? defaults_1.DEFAULT_COMPRESS_LEVEL;
    if (level === 0) {
        return data;
    }
    const result = zlib.deflateRawSync(uint8ArrayToBufferView(data), { level });
    return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
}
/**
 * Decompress data using Node.js native zlib
 *
 * @param data - Compressed data (deflate-raw format)
 * @returns Decompressed data
 */
async function decompress(data, options = {}) {
    const thresholdBytes = (0, compress_base_2.resolveCompressThresholdBytes)(options);
    // Small-input fast path: avoid threadpool overhead.
    if (data.byteLength <= thresholdBytes) {
        const result = zlib.inflateRawSync(uint8ArrayToBufferView(data));
        return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
    }
    const result = await inflateRawAsync(uint8ArrayToBufferView(data));
    return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
}
/**
 * Decompress data synchronously using Node.js zlib
 *
 * @param data - Compressed data (deflate-raw format)
 * @returns Decompressed data
 */
function decompressSync(data) {
    const result = zlib.inflateRawSync(uint8ArrayToBufferView(data));
    return new Uint8Array(result.buffer, result.byteOffset, result.byteLength);
}
