import { _isLittleEndian } from '@syncfusion/ej2-pdf';
export var imageKind = { grayScale1Bpp: 1, rgb24BPP: 2, rgba32BPP: 3 }; // eslint-disable-line
export var maximumCount = Math.pow(2, 31) - 1;
export function _convertToRGBA(kind, src, dest, width, height, inverseDecode) {
    switch (kind) {
        case imageKind.grayScale1Bpp:
            return _convertBlackAndWhiteToRGBA(src, 0, dest, width, height, 0xffffffff, inverseDecode);
        case imageKind.rgb24BPP:
            return _convertRGBToRGBA(src, 0, dest, 0, width, height);
    }
    return null;
}
export function _convertBlackAndWhiteToRGBA(src, srcPos, dest, width, // eslint-disable-line
height, nonBlackColor, inverseDecode) {
    if (nonBlackColor === void 0) { nonBlackColor = 0xffffffff; }
    if (inverseDecode === void 0) { inverseDecode = false; }
    var black = _isLittleEndian() ? 0xff000000 : 0x000000ff;
    var _a = inverseDecode
        ? [nonBlackColor, black]
        : [black, nonBlackColor], zeroMapping = _a[0], oneMapping = _a[1];
    var widthInSource = width >> 3;
    var widthRemainder = width & 7;
    var srcLength = src.length;
    dest = new Uint32Array(dest.buffer);
    var destPos = 0;
    for (var i = 0; i < height; i++) {
        for (var max = srcPos + widthInSource; srcPos < max; srcPos++) {
            var elem_1 = srcPos < srcLength ? src[srcPos] : 255;
            dest[destPos++] = elem_1 & 128 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 64 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 32 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 16 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 8 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 4 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 2 ? oneMapping : zeroMapping;
            dest[destPos++] = elem_1 & 1 ? oneMapping : zeroMapping;
        }
        if (widthRemainder === 0) {
            continue;
        }
        var elem = srcPos < srcLength ? src[srcPos++] : 255;
        for (var j = 0; j < widthRemainder; j++) {
            dest[destPos++] = elem & (1 << (7 - j)) ? oneMapping : zeroMapping;
        }
    }
    return { srcPos: srcPos, destPos: destPos };
}
export function _convertRGBToRGBA(src, srcPos, dest, destPos, width, // eslint-disable-line
height) {
    if (srcPos === void 0) { srcPos = 0; }
    if (destPos === void 0) { destPos = 0; }
    var i = 0;
    var len = width * height * 3;
    var len32 = len >> 2;
    var src32 = new Uint32Array(src.buffer, srcPos, len32);
    if (_isLittleEndian()) {
        for (; i < len32 - 2; i += 3, destPos += 4) {
            var s1 = src32[i];
            var s2 = src32[i + 1];
            var s3 = src32[i + 2];
            dest[destPos] = s1 | 0xff000000;
            dest[destPos + 1] = (s1 >>> 24) | (s2 << 8) | 0xff000000;
            dest[destPos + 2] = (s2 >>> 16) | (s3 << 16) | 0xff000000;
            dest[destPos + 3] = (s3 >>> 8) | 0xff000000;
        }
        for (var j = i * 4, jj = srcPos + len; j < jj; j += 3) {
            dest[destPos++] =
                src[j] | (src[j + 1] << 8) | (src[j + 2] << 16) | 0xff000000;
        }
    }
    else {
        for (; i < len32 - 2; i += 3, destPos += 4) {
            var s1 = src32[i];
            var s2 = src32[i + 1];
            var s3 = src32[i + 2];
            dest[destPos] = s1 | 0xff;
            dest[destPos + 1] = (s1 << 24) | (s2 >>> 8) | 0xff;
            dest[destPos + 2] = (s2 << 16) | (s3 >>> 16) | 0xff;
            dest[destPos + 3] = (s3 << 8) | 0xff;
        }
        for (var j = i * 4, jj = srcPos + len; j < jj; j += 3) {
            dest[destPos++] =
                (src[j] << 24) | (src[j + 1] << 16) | (src[j + 2] << 8) | 0xff;
        }
    }
    return { srcPos: srcPos + len, destPos: destPos };
}
/**
 * Converts a grayscale image represented by a Uint8Array into an RGBA format stored in a Uint32Array.
 *
 * @param {Uint8Array} src - The source array containing grayscale pixel values (0–255).
 * @param {Uint32Array} dest - The destination array where converted RGBA values will be stored.
 * @returns {void} This function does not return a value.
 */
export function _grayToRGBA(src, dest) {
    if (_isLittleEndian()) {
        for (var i = 0, ii = src.length; i < ii; i++) {
            dest[i] = (src[i] * 0x10101) | 0xff000000;
        }
    }
    else {
        for (var i = 0, ii = src.length; i < ii; i++) {
            dest[i] = (src[i] * 0x1010100) | 0x000000ff;
        }
    }
}
