"use strict";
/**
 * Browser compression utilities
 *
 * Primary: CompressionStream API (Chrome 103+, Firefox 113+, Safari 16.4+)
 * Fallback: Pure JS DEFLATE implementation for older browsers
 *
 * Supported browsers with fallback:
 * - Chrome >= 89
 * - Firefox >= 102
 * - Safari >= 14.1
 * - Edge >= 89
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasCompressionStream = void 0;
exports.compress = compress;
exports.compressSync = compressSync;
exports.decompress = decompress;
exports.decompressSync = decompressSync;
const compress_base_1 = require("./compress.base.js");
Object.defineProperty(exports, "hasCompressionStream", { enumerable: true, get: function () { return compress_base_1.hasCompressionStream; } });
const deflate_fallback_1 = require("./deflate-fallback.js");
const defaults_1 = require("../defaults.js");
/**
 * Compress data using browser's native CompressionStream or JS fallback
 *
 * Note: We always prefer native CompressionStream when available because
 * it's significantly faster than pure JS implementation.
 *
 * @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;
    // Level 0 means no compression
    if (level === 0) {
        return data;
    }
    // Always use native CompressionStream when available - it's much faster than JS
    if ((0, compress_base_1.hasDeflateRawCompressionStream)()) {
        return (0, compress_base_1.compressWithStream)(data);
    }
    // Fallback to pure JS implementation only when native is unavailable
    return (0, deflate_fallback_1.deflateRawCompressed)(data);
}
/**
 * Compress data synchronously using pure JS implementation
 *
 * @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;
    // Level 0 means no compression
    if (level === 0) {
        return data;
    }
    // Pure JS implementation
    return (0, deflate_fallback_1.deflateRawCompressed)(data);
}
/**
 * Decompress data using browser's native DecompressionStream or JS fallback
 *
 * Note: We always prefer native DecompressionStream when available because
 * it's significantly faster than pure JS implementation, regardless of data size.
 * The threshold is only useful for compression where the overhead matters more.
 *
 * @param data - Compressed data (deflate-raw format)
 * @param options - Decompression options (kept for API parity; currently unused in browser)
 * @returns Decompressed data
 */
async function decompress(data, options = {}) {
    void options;
    // Always use native DecompressionStream when available - it's much faster than JS
    if ((0, compress_base_1.hasDeflateRawDecompressionStream)()) {
        return (0, compress_base_1.decompressWithStream)(data);
    }
    // Fallback to pure JS implementation only when native is unavailable
    return (0, deflate_fallback_1.inflateRaw)(data);
}
/**
 * Decompress data synchronously using pure JS implementation
 *
 * @param data - Compressed data (deflate-raw format)
 * @returns Decompressed data
 */
function decompressSync(data) {
    return (0, deflate_fallback_1.inflateRaw)(data);
}
