/**
 * Created by yeanzhi on 16/12/12.
 */
let CanvasPrototype = window.HTMLCanvasElement && window.HTMLCanvasElement.prototype;
let hasBlobConstructor =
    window.Blob &&
    (function() {
        try {
            return Boolean(new Blob());
        } catch (e) {
            return false;
        }
    })();
let hasArrayBufferViewSupport =
    hasBlobConstructor &&
    window.Uint8Array &&
    (function() {
        try {
            return new Blob([new Uint8Array(100)]).size === 100;
        } catch (e) {
            return false;
        }
    })();
let BlobBuilder =
    window.BlobBuilder || window.WebKitBlobBuilder || window.MozBlobBuilder || window.MSBlobBuilder;
let dataURIPattern = /^data:((.*?)(;charset=.*?)?)(;base64)?,/;
// eslint-disable-next-line import/no-mutable-exports
let dataURLtoBlob =
    (hasBlobConstructor || BlobBuilder) &&
    window.atob &&
    window.ArrayBuffer &&
    window.Uint8Array &&
    function(dataURI) {
        let matches, mediaType, isBase64, dataString, byteString, arrayBuffer, intArray, i, bb;
        // Parse the dataURI components as per RFC 2397
        matches = dataURI.match(dataURIPattern);
        if (!matches) {
            throw new Error('invalid data URI');
        }
        // Default to text/plain;charset=US-ASCII
        mediaType = matches[2] ? matches[1] : `text/plain${matches[3] || ';charset=US-ASCII'}`;
        isBase64 = !!matches[4];
        dataString = dataURI.slice(matches[0].length);
        if (isBase64) {
            // Convert base64 to raw binary data held in a string:
            byteString = atob(dataString);
        } else {
            // Convert base64/URLEncoded data component to raw binary:
            byteString = decodeURIComponent(dataString);
        }
        // Write the bytes of the string to an ArrayBuffer:
        arrayBuffer = new ArrayBuffer(byteString.length);
        intArray = new Uint8Array(arrayBuffer);
        for (i = 0; i < byteString.length; i += 1) {
            intArray[i] = byteString.charCodeAt(i);
        }
        // Write the ArrayBuffer (or ArrayBufferView) to a blob:
        if (hasBlobConstructor) {
            return new Blob([hasArrayBufferViewSupport ? intArray : arrayBuffer], {
                type: mediaType
            });
        }
        bb = new BlobBuilder();
        bb.append(arrayBuffer);
        return bb.getBlob(mediaType);
    };
if (window.HTMLCanvasElement && !CanvasPrototype.toBlob) {
    if (CanvasPrototype.mozGetAsFile) {
        CanvasPrototype.toBlob = function(callback, type, quality) {
            if (quality && CanvasPrototype.toDataURL && dataURLtoBlob) {
                callback(dataURLtoBlob(this.toDataURL(type, quality)));
            } else {
                callback(this.mozGetAsFile('blob', type));
            }
        };
    } else if (CanvasPrototype.toDataURL && dataURLtoBlob) {
        CanvasPrototype.toBlob = function(callback, type, quality) {
            callback(dataURLtoBlob(this.toDataURL(type, quality)));
        };
    }
}
export default dataURLtoBlob;
window.dataURLtoBlob = dataURLtoBlob;
