"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = getCroppedImg;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var createImage = function createImage(url) {
  return new Promise(function (resolve, reject) {
    var image = new Image();
    image.addEventListener('load', function () {
      return resolve(image);
    });
    image.addEventListener('error', function (error) {
      return reject(error);
    });
    image.setAttribute('crossOrigin', 'anonymous'); // needed to avoid cross-origin issues on CodeSandbox

    image.src = url;
  });
};

function getRadianAngle(degreeValue) {
  return degreeValue * Math.PI / 180;
}
/**
 * This function was adapted from the one in the ReadMe of https://github.com/DominicTobias/react-image-crop
 * @param {File} image - Image File url
 * @param {Object} pixelCrop - pixelCrop Object provided by react-easy-crop
 * @param {number} rotation - optional rotation parameter
 */


function getCroppedImg(_x, _x2) {
  return _getCroppedImg.apply(this, arguments);
}

function _getCroppedImg() {
  _getCroppedImg = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee(imageSrc, pixelCrop) {
    var rotation,
        imageType,
        image,
        canvas,
        ctx,
        maxSize,
        safeArea,
        data,
        _args = arguments;
    return _regenerator["default"].wrap(function _callee$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            rotation = _args.length > 2 && _args[2] !== undefined ? _args[2] : 0;
            imageType = _args.length > 3 ? _args[3] : undefined;
            _context.next = 4;
            return createImage(imageSrc);

          case 4:
            image = _context.sent;
            canvas = document.createElement('canvas');
            ctx = canvas.getContext('2d');
            maxSize = Math.max(image.width, image.height);
            safeArea = 2 * (maxSize / 2 * Math.sqrt(2)); // set each dimensions to double largest dimension to allow for a safe area for the
            // image to rotate in without being clipped by canvas context

            canvas.width = safeArea;
            canvas.height = safeArea; // translate canvas context to a central location on image to allow rotating around the center.

            ctx.translate(safeArea / 2, safeArea / 2);
            ctx.rotate(getRadianAngle(rotation));
            ctx.translate(-safeArea / 2, -safeArea / 2); // draw rotated image and store data.

            ctx.drawImage(image, safeArea / 2 - image.width * 0.5, safeArea / 2 - image.height * 0.5);
            data = ctx.getImageData(0, 0, safeArea, safeArea); // set canvas width to final desired crop size - this will clear existing context

            canvas.width = pixelCrop.width;
            canvas.height = pixelCrop.height; // paste generated rotate image with correct offsets for x,y crop values.

            ctx.putImageData(data, 0 - safeArea / 2 + image.width * 0.5 - pixelCrop.x, 0 - safeArea / 2 + image.height * 0.5 - pixelCrop.y); // As Base64 string
            // return canvas.toDataURL('image/jpeg');
            // As a blob

            return _context.abrupt("return", new Promise(function (resolve) {
              canvas.toBlob(function (file) {
                resolve(URL.createObjectURL(file));
              }, "image/".concat(imageType));
            }));

          case 20:
          case "end":
            return _context.stop();
        }
      }
    }, _callee);
  }));
  return _getCroppedImg.apply(this, arguments);
}
//# sourceMappingURL=CropImage.js.map