import { __awaiter } from 'tslib';

function arrayBufferToBase64(arrayBuffer) {
    return window.btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
}

function base64ToArrayBuffer(base64) {
    const binaryString = window.atob(base64);
    const len = binaryString.length;
    const bytes = new Uint8Array(len);
    for (let i = 0; i < len; i++) {
        bytes[i] = binaryString.charCodeAt(i);
    }
    return bytes.buffer;
}

// eslint-disable-next-line no-restricted-syntax
var EncodingType;
(function (EncodingType) {
    EncodingType["GZIP"] = "gzip";
    EncodingType["DEFLATE"] = "deflate";
})(EncodingType || (EncodingType = {}));

function compress(text, encoding = EncodingType.DEFLATE) {
    return __awaiter(this, void 0, void 0, function* () {
        const byteArray = new TextEncoder().encode(text);
        const stream = new CompressionStream(encoding);
        const readable = stream.readable;
        const writable = stream.writable;
        const writer = writable.getWriter();
        // noinspection ES6MissingAwait
        writer.write(byteArray);
        // noinspection ES6MissingAwait
        writer.close();
        return new Response(readable).arrayBuffer();
    });
}

function decodeBase64ToUnicode(str) {
    const HEX_LENGTH = -2;
    const ZEROS = '00';
    const HEX = 16;
    return decodeURIComponent(Array.prototype.map
        .call(window.atob(str), (c) => {
        const hexCode = c.charCodeAt(0).toString(HEX);
        const preparedHexCode = `${ZEROS}${hexCode}`.slice(HEX_LENGTH);
        return `%${preparedHexCode}`;
    })
        .join(''));
}

function decompress(bytes, encoding = EncodingType.DEFLATE) {
    return __awaiter(this, void 0, void 0, function* () {
        const decompressionStream = new DecompressionStream(encoding);
        const writable = decompressionStream.writable;
        const writer = writable.getWriter();
        // noinspection ES6MissingAwait
        writer.write(bytes);
        // noinspection ES6MissingAwait
        writer.close();
        const arrayBuffer = yield new Response(decompressionStream.readable).arrayBuffer();
        return new TextDecoder().decode(arrayBuffer);
    });
}

function encodeUnicodeToBase64(str) {
    return window.btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, (_match, p1) => String.fromCharCode(parseInt(p1, 16))));
}

/**
 * Generated bundle index. Do not edit.
 */

export { EncodingType, arrayBufferToBase64, base64ToArrayBuffer, compress, decodeBase64ToUnicode, decompress, encodeUnicodeToBase64 };
//# sourceMappingURL=angular-ru-cdk-stream.js.map
