"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.md5 = void 0;
const tslib_1 = require("tslib");
const spark_md5_1 = tslib_1.__importDefault(require("spark-md5"));
const md5 = (file, chunkSize, callback) => {
    chunkSize = chunkSize || 2097152; // Read in chunks of 2MB
    // eslint-disable-next-line @typescript-eslint/unbound-method
    let blobSlice = File.prototype.slice || File.prototype['mozSlice'] || File.prototype['webkitSlice'], chunks = Math.ceil(file.size / chunkSize), currentChunk = 0, spark = new spark_md5_1.default.ArrayBuffer(), fileReader = new FileReader();
    fileReader.onload = function (e) {
        // console.log('read chunk nr', currentChunk + 1, 'of', chunks);
        spark.append(e.target.result); // Append array buffer
        currentChunk++;
        if (currentChunk < chunks) {
            loadNext();
        }
        else {
            callback(spark.end());
        }
    };
    fileReader.onerror = function () {
        console.warn('oops, something went wrong.');
    };
    function loadNext() {
        let start = currentChunk * chunkSize, end = start + chunkSize >= file.size ? file.size : start + chunkSize;
        fileReader.readAsArrayBuffer(blobSlice.call(file, start, end));
    }
    loadNext();
};
exports.md5 = md5;
