"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.waitFileReaderResult = waitFileReaderResult;
exports.readBlobAsDataUrl = readBlobAsDataUrl;
exports.readBlobAsDataUrl_ = readBlobAsDataUrl_;
exports.readBlobAsArrayBuffer_ = readBlobAsArrayBuffer_;
exports.readBlobAsText_ = readBlobAsText_;
exports.wrapFileReader = wrapFileReader;
const _1 = require("../Class/$");
function waitFileReaderResult(reader) {
    return new Promise((resolve, reject) => {
        const listener = async function (event) {
            reader.removeEventListener('loadend', listener);
            const { error, result } = reader;
            if (error) {
                reject(error.message);
            }
            else {
                resolve((await result));
            }
        };
        reader.addEventListener('loadend', listener);
    });
}
function readBlobAsDataUrl(system, blob) {
    const { api: { file: { FileReader }, }, } = system;
    const reader = new FileReader();
    reader.readAsDataURL(blob);
    return waitFileReaderResult(reader);
}
function readBlobAsDataUrl_(reader, blob) {
    reader.readAsDataURL(blob);
    return waitFileReaderResult(reader);
}
function readBlobAsArrayBuffer_(reader, blob) {
    reader.readAsArrayBuffer(blob);
    return waitFileReaderResult(reader);
}
function readBlobAsText_(reader, blob) {
    reader.readAsText(blob);
    return waitFileReaderResult(reader);
}
function wrapFileReader(reader, system) {
    const reader_ = new (class FileReader_ extends _1.$ {
        constructor() {
            super(...arguments);
            this.__ = ['FR'];
        }
        readAsText(blob) {
            return readBlobAsText_(reader, blob);
        }
        readAsDataUrl(blob) {
            return readBlobAsDataUrl_(reader, blob);
        }
        readAsArrayBuffer(blob) {
            return readBlobAsArrayBuffer_(reader, blob);
        }
    })(system);
    return reader_;
}
//# sourceMappingURL=FileReader.js.map