var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _class, _temp;

function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

import React, { Component } from 'react';
import PropTypes from 'prop-types';
import Instance from 'jcrop';
import 'jcrop/dist/jcrop.css'; // Jcrop

var Jcrop = (_temp = _class = function (_Component) {
  _inherits(Jcrop, _Component);

  function Jcrop(props) {
    _classCallCheck(this, Jcrop);

    var _this = _possibleConstructorReturn(this, _Component.call(this, props));

    _this.formatPos = function (argPos) {
      var pos = argPos;
      for (var n in argPos) {
        if (n === 'h') pos['height'] = argPos[n];else if (n === 'w') pos['width'] = argPos[n];
      }
      return pos;
    };

    _this.update = function () {
      if (!_this.props.src) return;
      var _this$props = _this.props,
          rect = _this$props.rect,
          scale = _this$props.scale;

      Instance.load(_this.$el).then(function (img) {
        var jcp = Instance.attach(img, _this.props.options);
        var frame = null;
        var pos = {};
        // 按尺寸创建
        if (rect) {
          pos = {
            x: rect[0],
            y: rect[1],
            w: rect[2],
            h: rect[3]
          };
          frame = Instance.Rect.create(pos.x, pos.y, pos.w, pos.h);
          jcp.newWidget(frame);
          // 按比例创建
        } else {
          var _frame;

          frame = Instance.Rect.sizeOf(jcp.el);
          pos = (_frame = frame).scale.apply(_frame, scale).center(frame.w, frame.h);
          jcp.newWidget(pos);
        }
        if (_this.props.onChange) _this.props.onChange({ pos: _this.formatPos(pos), src: _this.props.src });
        jcp.listen('crop.change', function (widget, e) {
          pos = _this.formatPos(widget.pos);
          if (_this.props.onChange) _this.props.onChange({ pos: pos, src: _this.props.src });
        });
      }).catch(function (err) {
        console.error(err);
      });
    };

    return _this;
  }

  Jcrop.prototype.componentDidMount = function componentDidMount() {
    this.update();
  };

  Jcrop.prototype.render = function render() {
    var _this2 = this;

    var _props = this.props,
        rect = _props.rect,
        scale = _props.scale,
        options = _props.options,
        onChange = _props.onChange,
        src = _props.src,
        style = _props.style,
        others = _objectWithoutProperties(_props, ['rect', 'scale', 'options', 'onChange', 'src', 'style']);

    return React.createElement('img', _extends({ alt: '', ref: function ref(el) {
        _this2.$el = el;
      } }, others, { style: Object.assign({ maxWidth: '100%' }, style), src: src }));
  };

  return Jcrop;
}(Component), _class.defaultProps = {
  scale: [.7, .5],
  options: {
    multi: false
  }
}, _temp);
export { Jcrop as default };
Jcrop.propTypes = process.env.NODE_ENV !== "production" ? {
  style: PropTypes.object,
  src: PropTypes.string,
  rect: PropTypes.array, // [10,10,100,100]
  scale: PropTypes.array, // [.7,.5]
  options: PropTypes.object,
  onChange: PropTypes.func
} : {};