"use strict";
/**
 * CRC32 calculation utility for ZIP files
 *
 * - Node.js: Uses native zlib.crc32 (C++ implementation, ~100x faster)
 * - Browser: Uses lookup table optimization
 *
 * The polynomial used is the standard CRC-32 IEEE 802.3:
 * x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 + x^7 + x^5 + x^4 + x^2 + x + 1
 * Represented as 0xEDB88320 in reversed (LSB-first) form
 */
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.crc32Finalize = void 0;
exports.crc32 = crc32;
exports.ensureCrc32 = ensureCrc32;
exports.crc32Update = crc32Update;
const env_1 = require("../../../utils/env.js");
const crc32_base_1 = require("./crc32.base.js");
Object.defineProperty(exports, "crc32Finalize", { enumerable: true, get: function () { return crc32_base_1.crc32Finalize; } });
// Lazy-loaded zlib module for Node.js
let _zlib = null;
let _zlibLoading = null;
// Auto-initialize zlib in Node.js environment
if ((0, env_1.isNode)()) {
    _zlibLoading = Promise.resolve().then(() => __importStar(require("zlib"))).then(module => {
        _zlib = module.default ?? module;
        return _zlib;
    })
        .catch(() => {
        _zlib = null;
        return null;
    });
}
/**
 * Calculate CRC32 checksum for the given data
 * Uses native zlib.crc32 in Node.js for ~100x better performance
 *
 * @param data - Input data as Uint8Array or Buffer
 * @returns CRC32 checksum as unsigned 32-bit integer
 *
 * @example
 * ```ts
 * const data = new TextEncoder().encode("Hello, World!");
 * const checksum = crc32(data);
 * console.log(checksum.toString(16)); // "ec4ac3d0"
 * ```
 */
function crc32(data) {
    // Use native zlib.crc32 if available (Node.js)
    if (_zlib && typeof _zlib.crc32 === "function") {
        return _zlib.crc32(data) >>> 0;
    }
    // Fallback to JS implementation
    return (0, crc32_base_1.crc32JS)(data);
}
/**
 * Ensure zlib is loaded (for use before calling crc32)
 */
async function ensureCrc32() {
    if (_zlibLoading) {
        await _zlibLoading;
    }
}
/**
 * Calculate CRC32 incrementally (useful for streaming)
 * Call with initial crc of 0xffffffff, then finalize with crc32Finalize
 * In Node.js, this uses native zlib.crc32 when available for performance.
 * The internal CRC state remains the same as the JS table implementation:
 * - initial state: 0xffffffff
 * - finalize: xor with 0xffffffff
 *
 * @param crc - Current CRC value (start with 0xffffffff)
 * @param data - Input data chunk
 * @returns Updated CRC value (not finalized)
 *
 * @example
 * ```ts
 * let crc = 0xffffffff;
 * crc = crc32Update(crc, chunk1);
 * crc = crc32Update(crc, chunk2);
 * const checksum = crc32Finalize(crc);
 * ```
 */
function crc32Update(crc, data) {
    // If available, use native zlib.crc32 but preserve our internal state shape.
    // zlib.crc32 returns a finalized CRC value and can accept the previous finalized
    // CRC as the second parameter (chainable). Our internal state is the inverted
    // (non-finalized) CRC, so convert with xor before/after.
    if (_zlib && typeof _zlib.crc32 === "function") {
        const prevFinal = (crc ^ 0xffffffff) >>> 0;
        const nextFinal = _zlib.crc32(data, prevFinal) >>> 0;
        return (nextFinal ^ 0xffffffff) >>> 0;
    }
    return (0, crc32_base_1.crc32UpdateJS)(crc, data);
}
