import { DEFAULT_LEVEL } from './config.js';
import { MAX_SIZE, MIN_LEVEL, MAX_LEVEL } from './config.js';
/** check input before coercion */
export function checkInput(input) {
    if (input == null || typeof input != 'object')
        throw new TypeError(`Input data must be BinaryData`);
    if (ArrayBuffer.isView(input)) {
        if (input.byteLength > MAX_SIZE)
            throw new Error(`Input data is too large`);
        return;
    }
    if ((typeof ArrayBuffer == 'function' && input instanceof ArrayBuffer) ||
        (typeof SharedArrayBuffer == 'function' && input instanceof SharedArrayBuffer)) {
        if (input.byteLength > MAX_SIZE)
            throw new Error(`Input data is too large`);
        return;
    }
    throw new TypeError(`Input data must be BinaryData`);
}
/** check and clamp compress level */
export function checkLevel(level) {
    if (level == null)
        return DEFAULT_LEVEL;
    if (typeof level != 'number')
        throw new Error(`level must be an integer`);
    if (Number.isNaN(level))
        return DEFAULT_LEVEL;
    if (level < MIN_LEVEL)
        return MIN_LEVEL;
    if (level > MAX_LEVEL)
        return MAX_LEVEL;
    return Math.trunc(level);
}
/** warp module */
export function createModule(options) {
    const { coercion, compress, decompress } = options;
    return {
        compress: (data, level = DEFAULT_LEVEL) => {
            level = checkLevel(level);
            checkInput(data);
            const input = coercion(data);
            const output = compress(input, level);
            return output;
        },
        decompress: (data) => {
            checkInput(data);
            const input = coercion(data);
            const output = decompress(input);
            return output;
        },
    };
}
//# sourceMappingURL=common.js.map