"use strict";
/**
 * @module fly
 * @private
 */
Object.defineProperty(exports, "__esModule", { value: true });
/** @hidden */
class Blob {
    constructor(blobParts, options) {
        if (!blobParts || blobParts.length === 0)
            this.bytes = new Uint8Array();
        else {
            let parts = [];
            let encoder = new TextEncoder();
            for (const part of blobParts) {
                switch (part.constructor) {
                    case Int8Array:
                    case Uint8ClampedArray:
                    case Int16Array:
                    case Uint16Array:
                    case Int32Array:
                    case Uint32Array:
                    case Float32Array:
                    case Float64Array:
                    case DataView:
                        parts.push(new Uint8Array(part.buffer));
                        break;
                    case Uint8Array:
                        parts.push(part);
                        break;
                    case ArrayBuffer:
                        parts.push(new Uint8Array(part));
                        break;
                    case String:
                        parts.push(encoder.encode(part));
                        break;
                    case Blob:
                        parts.push(part.bytes);
                        break;
                }
            }
            if (parts.length === 1)
                this.bytes = parts[0];
            else
                this.bytes = concatenate(...parts);
        }
        if (options && options.type)
            this.type = options.type.toLowerCase();
        else
            this.type = '';
        this.size = this.bytes.byteLength;
    }
    slice(start, end, contentType) {
        return new Blob([this.bytes.slice(start, end)], { type: contentType });
    }
}
exports.default = Blob;
/** @hidden */
function concatenate(...arrays) {
    let totalLength = 0;
    for (let arr of arrays) {
        totalLength += arr.length;
    }
    let result = new Uint8Array(totalLength);
    let offset = 0;
    for (let arr of arrays) {
        result.set(arr, offset);
        offset += arr.length;
    }
    return result;
}
//# sourceMappingURL=blob.js.map