import { DEFAULT_LEVEL, MAX_SIZE, MIN_LEVEL, MAX_LEVEL } from './config.js';
/** Check input */
function isBlob(value) {
    return (typeof value.size == 'number' &&
        typeof value.type == 'string' &&
        typeof value.slice == 'function' &&
        typeof value.arrayBuffer == 'function' &&
        typeof value.text == 'function');
}
/** Check input */
function isArrayBuffer(value) {
    return typeof value.byteLength == 'number' && typeof value.slice == 'function';
}
/** check and coercion input */
export function coercionInput(input, allowBlob) {
    if (input != null && typeof input == 'object') {
        if (ArrayBuffer.isView(input)) {
            if (input.byteLength > MAX_SIZE)
                throw new Error(`Input data is too large`);
            return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
        }
        if (isArrayBuffer(input)) {
            if (input.byteLength > MAX_SIZE)
                throw new Error(`Input data is too large`);
            return new Uint8Array(input);
        }
        if (allowBlob && isBlob(input)) {
            if (input.size > MAX_SIZE)
                throw new Error(`Input data is too large`);
            return input;
        }
    }
    throw new TypeError(`Input data must be BufferSource${allowBlob ? ' or Blob' : ''}.`);
}
/** 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);
}
//# sourceMappingURL=utils.js.map