"use strict";
/**
 * CRC32 calculation core (shared between Node.js and browser).
 *
 * Implements CRC-32 IEEE 802.3 using the reversed polynomial 0xEDB88320.
 *
 * Notes:
 * - Uses a lazily-initialized 256-entry lookup table.
 * - Exposes an incremental update API for streaming use cases.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.crc32UpdateJS = crc32UpdateJS;
exports.crc32Finalize = crc32Finalize;
exports.crc32JS = crc32JS;
let _crc32Table = null;
function getCrc32Table() {
    if (_crc32Table) {
        return _crc32Table;
    }
    const table = new Uint32Array(256);
    for (let i = 0; i < 256; i++) {
        let crc = i;
        for (let j = 0; j < 8; j++) {
            crc = crc & 1 ? 0xedb88320 ^ (crc >>> 1) : crc >>> 1;
        }
        table[i] = crc;
    }
    _crc32Table = table;
    return table;
}
/**
 * Update a CRC32 value with a new data chunk.
 *
 * The CRC state here is the non-finalized (inverted) form:
 * - initial state: 0xffffffff
 * - finalize: xor with 0xffffffff
 */
function crc32UpdateJS(crc, data) {
    const table = getCrc32Table();
    for (let i = 0; i < data.length; i++) {
        crc = table[(crc ^ data[i]) & 0xff] ^ (crc >>> 8);
    }
    return crc;
}
/**
 * Finalize CRC32 calculation.
 */
function crc32Finalize(crc) {
    return (crc ^ 0xffffffff) >>> 0;
}
/**
 * Compute CRC32 for the full input using the JS table implementation.
 */
function crc32JS(data) {
    return crc32Finalize(crc32UpdateJS(0xffffffff, data));
}
