"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.toDataUrl = toDataUrl;
exports.detectBase64MimeType = detectBase64MimeType;
exports.detectMimeType = detectMimeType;
exports.b64toBlob = exports.checkFileSize = void 0;

var _react = _interopRequireDefault(require("react"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

const checkFileSize = file => {
  // The size of the file.
  if (file && file.size) {
    const fsize = Math.round(file.size / 1024);

    if (fsize > 3072) {
      // notify(string.event.fileSizeError)
      return true;
    } else {
      return false;
    }
  }

  return false;
};

exports.checkFileSize = checkFileSize;

function toDataUrl(url, callback) {
  var xhr = new XMLHttpRequest();

  xhr.onload = function () {
    var reader = new FileReader();

    reader.onloadend = function () {
      callback(reader.result);
    };

    reader.readAsDataURL(xhr.response);
  };

  xhr.open('GET', url);
  xhr.responseType = 'blob';
  xhr.send();
}

var b64signatures = {
  JVBERi0: "data:application/pdf;base64,",
  R0lGODdh: "data:image/gif;base64,",
  R0lGODlh: "data:image/gif;base64,",
  iVBORw0KGgo: "data:image/png;base64,",
  "/9j/": "data:image/jpg;base64,"
};
var signatures = {
  JVBERi0: "application/pdf",
  R0lGODlh: "image/gif",
  R0lGODdh: "image/gif",
  iVBORw0KGgo: "image/png",
  "/9j/": "image/jpg"
};

function detectBase64MimeType(b64) {
  for (var s in b64signatures) {
    if (b64.indexOf(s) === 0) {
      return b64signatures[s];
    }
  }
}

function detectMimeType(b64) {
  for (var s in signatures) {
    if (b64.indexOf(s) === 0) {
      return signatures[s];
    }
  }
}

const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
  const byteCharacters = window.atob(b64Data);
  const byteArrays = [];

  for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
    const slice = byteCharacters.slice(offset, offset + sliceSize),
          byteNumbers = new Array(slice.length);

    for (let i = 0; i < slice.length; i++) {
      byteNumbers[i] = slice.charCodeAt(i);
    }

    const byteArray = new Uint8Array(byteNumbers);
    byteArrays.push(byteArray);
  }

  const blob = new Blob(byteArrays, {
    type: contentType
  });
  return blob;
};

exports.b64toBlob = b64toBlob;