"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var React = _interopRequireWildcard(require("react"));

var ReactPIXI = _interopRequireWildcard(require("react-pixi-fiber"));

var _transformComponent = _interopRequireDefault(require("../transform-component/transform-component"));

var _rect = _interopRequireDefault(require("../../elements/rect"));

var _base = _interopRequireDefault(require("../base"));

var _utils = require("../../utils/utils");

var _index = require("../common/index");

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

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }

function _extends() { _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; }; return _extends.apply(this, arguments); }

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

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); }

function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }

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

function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }

function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

var RECT_WIDTH = 4;
var RECT_HEIGHT = 19; // 上、下、左、右四边上的控制点宽度（高度）

var AROUND_HEIGHT = 18; // 控制点可响应范围的宽度与高度

var HIT_AREA = 30; // 裁剪边框颜色

var RECT_COLOR = 0x005bf9; // TODO:
// 不继承 transform，支持传入颜色与控制点样式

var CropperComponent =
/*#__PURE__*/
function (_TransformComponent) {
  _inherits(CropperComponent, _TransformComponent);

  function CropperComponent() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, CropperComponent);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(CropperComponent)).call.apply(_getPrototypeOf2, [this].concat(args)));

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "angle", 0);

    return _this;
  }

  _createClass(CropperComponent, [{
    key: "render",
    value: function render() {
      var props = new _base.default(this.props, {
        omit: ['filters'],
        autoSetEvent: true
      });
      this.angle = this.getAngle(props.rotation, props.skew);
      return React.createElement(ReactPIXI.Container, props, React.createElement(_rect.default, {
        width: this.props.width,
        height: this.props.height,
        lineWidth: 3,
        lineColor: RECT_COLOR,
        lineAlpha: 1
      }), this.resizes, this.guiders);
    }
  }, {
    key: "commonProps",
    get: function get() {
      return {
        buttonMode: false,
        fillAlpha: 1,
        width: RECT_WIDTH,
        height: RECT_HEIGHT,
        lineWidth: 0,
        fill: RECT_COLOR,
        pointerup: this.onRectPointerup,
        pointerupoutside: this.onPointerupoutside
      };
    }
  }, {
    key: "showGuides",
    get: function get() {
      return this.props.active;
    }
  }, {
    key: "guiders",
    get: function get() {
      if (!this.showGuides) {
        return null;
      }

      var _this$props = this.props,
          width = _this$props.width,
          height = _this$props.height;
      return [{
        type: 'v',
        idx: 1
      }, {
        type: 'v',
        idx: 2
      }, {
        type: 'h',
        idx: 1
      }, {
        type: 'h',
        idx: 2
      }].map(function (info) {
        var isV = info.type === 'v';
        var key = "".concat(info.type).concat(info.idx);
        return React.createElement(_rect.default, {
          key: key,
          width: isV ? 1 : width,
          height: isV ? height : 1,
          x: isV ? width / 3 * info.idx : 0,
          y: isV ? 0 : height / 3 * info.idx,
          lineColor: 0xFFFFFF,
          lineAlpha: 0.4,
          lineWidth: 1,
          fillAlpha: 1
        });
      });
    }
  }, {
    key: "resizes",
    get: function get() {
      var _this$props2 = this.props,
          width = _this$props2.width,
          height = _this$props2.height;
      var angle = this.angle,
          commonProps = this.commonProps;

      var getIndexByAngelLimited = function getIndexByAngelLimited(increment) {
        return (0, _index.getIndexByAngle)((0, _utils.intLimit)(angle + increment, 360));
      };

      var bottomLeftIndex = getIndexByAngelLimited(45);
      var topLeftIndex = getIndexByAngelLimited(135);
      var topRightIndex = getIndexByAngelLimited(225);
      var bottomRightIndex = getIndexByAngelLimited(315);
      var topIndex = getIndexByAngelLimited(180);
      var bottomIndex = getIndexByAngelLimited(0);
      var leftIndex = getIndexByAngelLimited(90);
      var rightIndex = getIndexByAngelLimited(270);
      var halfRectWidth = commonProps.width / 2;
      var around = [{
        key: 'n',
        cursor: _index.cursors[topIndex],
        width: AROUND_HEIGHT,
        height: commonProps.width,
        x: (width - AROUND_HEIGHT) / 2,
        y: -halfRectWidth * 2 + 1,
        hitArea: [width / 2 - 19, -HIT_AREA / 2, HIT_AREA, HIT_AREA]
      }, {
        key: 's',
        cursor: _index.cursors[bottomIndex],
        width: AROUND_HEIGHT,
        height: commonProps.width,
        x: (width - AROUND_HEIGHT) / 2,
        y: height,
        hitArea: [width / 2 - 19, height - HIT_AREA / 2, HIT_AREA, HIT_AREA]
      }, {
        key: 'w',
        cursor: _index.cursors[leftIndex],
        height: AROUND_HEIGHT,
        hitArea: [-HIT_AREA / 2, height / 2 - 19, HIT_AREA, HIT_AREA],
        x: -commonProps.width + 1,
        y: (height - AROUND_HEIGHT) / 2
      }, {
        key: 'e',
        cursor: _index.cursors[rightIndex],
        height: AROUND_HEIGHT,
        hitArea: [width - HIT_AREA / 2, height / 2 - 19, HIT_AREA, HIT_AREA],
        x: width - 1,
        y: (height - AROUND_HEIGHT) / 2
      }].map(function (item) {
        return React.createElement(_rect.default, _extends({}, commonProps, item, {
          width: item.width || commonProps.width,
          height: item.height || commonProps.height // hitArea={new PIXI.Rectangle(...item.hitArea)}
          // pointerdown={(e: any) => onRectPointerdown(e, item.key)}

        }));
      });
      var corner = [{
        key: 'nw',
        children: [{
          x: -halfRectWidth * 2 + 1,
          y: -halfRectWidth * 2 + 1,
          width: commonProps.height,
          height: commonProps.width
        }, {
          x: -halfRectWidth * 2 + 1,
          y: -halfRectWidth * 2 + 1
        }, {
          width: commonProps.height,
          height: commonProps.height,
          cursor: _index.cursors[topLeftIndex],
          hitArea: [-HIT_AREA / 2, -HIT_AREA / 2, HIT_AREA, HIT_AREA]
        }]
      }, {
        key: 'sw',
        x: -halfRectWidth,
        y: height,
        children: [{
          x: -halfRectWidth + 1,
          width: commonProps.height,
          height: commonProps.width
        }, {
          x: -halfRectWidth + 1,
          y: -commonProps.height + 2 * halfRectWidth
        }, {
          y: -commonProps.height / 2,
          width: commonProps.height,
          height: commonProps.height,
          cursor: _index.cursors[bottomLeftIndex],
          hitArea: [-HIT_AREA / 2, height - HIT_AREA / 2, HIT_AREA, HIT_AREA]
        }]
      }, {
        key: 'ne',
        x: width - 1,
        y: -halfRectWidth,
        children: [{
          x: -commonProps.height + halfRectWidth * 2,
          y: -halfRectWidth + 1,
          width: commonProps.height,
          height: commonProps.width
        }, {
          y: -halfRectWidth + 1
        }, {
          x: -commonProps.height / 2,
          width: commonProps.height,
          height: commonProps.height,
          cursor: _index.cursors[topRightIndex],
          hitArea: [width - HIT_AREA / 2, -HIT_AREA / 2, HIT_AREA, HIT_AREA]
        }]
      }, {
        key: 'se',
        x: width - 1,
        y: height,
        children: [{
          x: -commonProps.height + 2 * halfRectWidth,
          width: commonProps.height,
          height: commonProps.width
        }, {
          y: -commonProps.height + 2 * halfRectWidth
        }, {
          x: -commonProps.height / 2,
          y: -commonProps.height / 2,
          width: commonProps.height,
          height: commonProps.height,
          cursor: _index.cursors[bottomRightIndex],
          hitArea: [width - HIT_AREA / 2, height - HIT_AREA / 2, HIT_AREA, HIT_AREA]
        }]
      }].map(function (cornerProps) {
        return React.createElement(ReactPIXI.Container, cornerProps, React.createElement(_rect.default, _extends({}, commonProps, cornerProps.children[0])), React.createElement(_rect.default, _extends({}, commonProps, cornerProps.children[1])), React.createElement(_rect.default, cornerProps.children[2]));
      });
      return React.createElement(React.Fragment, null, corner, around);
    }
  }]);

  return CropperComponent;
}(_transformComponent.default);

exports.default = CropperComponent;