"use strict";

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

var _defaults2 = _interopRequireDefault(require("lodash/defaults"));

var _isFunction2 = _interopRequireDefault(require("lodash/isFunction"));

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

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

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

var _common = _interopRequireWildcard(require("../common"));

var _default = require("../../constants/default");

var _svg = _interopRequireDefault(require("../../elements/svg"));

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

var _text = _interopRequireDefault(require("../../elements/text"));

var _transformModel = _interopRequireDefault(require("./transform-model"));

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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

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 _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }

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 TransformComponent =
/*#__PURE__*/
function (_React$PureComponent) {
  _inherits(TransformComponent, _React$PureComponent);

  function TransformComponent() {
    var _getPrototypeOf2;

    var _this;

    _classCallCheck(this, TransformComponent);

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

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

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "type", '');

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "resizing", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "dragging", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "rotating", false);

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onPointerdown", function (e) {
      var state = {
        dragging: false
      };

      if (!_this.resizing && (0, _isFunction2.default)(_this.props.onDragStart)) {
        state.dragging = true;

        _this.props.onDragStart(e);
      }

      Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), state);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onPointermove", function (e) {
      if (_this.resizing && (0, _isFunction2.default)(_this.props.onResizeMove)) {
        _this.props.onResizeMove(e, _this.type);
      }

      if (!_this.resizing && _this.dragging && (0, _isFunction2.default)(_this.props.onDragMove)) {
        _this.props.onDragMove(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onPointerup", function (e) {
      if (!_this.resizing && (0, _isFunction2.default)(_this.props.onDragEnd)) {
        _this.props.onDragEnd(e);
      }

      Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), {
        dragging: false,
        resizing: false
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onPointerupoutside", function (e) {
      Object.assign({
        dragging: false,
        resizing: false,
        rotating: false
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onRectPointerdown", function (e, direction) {
      var state = {
        type: direction,
        resizing: false
      };

      if (_common.cursors.indexOf(e.target.cursor) !== -1 && (0, _isFunction2.default)(_this.props.onResizeStart)) {
        state.resizing = true;
        state.type = direction;

        _this.props.onResizeStart(e, direction);
      }

      Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), state);
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onRectPointerup", function (e) {
      if ((0, _isFunction2.default)(_this.props.onResizeEnd) && _this.resizing) {
        _this.props.onResizeEnd(_this.type, e);
      }

      Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), {
        resizing: false
      });
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onSvgPointerdown", function (e) {
      _this.rotating = true;

      if ((0, _isFunction2.default)(_this.props.onRotateStart) && _this.rotating) {
        _this.props.onRotateStart(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onSvgPointermove", function (e) {
      if ((0, _isFunction2.default)(_this.props.onRotateMove) && _this.rotating) {
        _this.props.onRotateMove(e);
      }
    });

    _defineProperty(_assertThisInitialized(_assertThisInitialized(_this)), "onSvgPointerup", function (e) {
      if ((0, _isFunction2.default)(_this.props.onRotateEnd) && _this.rotating) {
        _this.props.onRotateEnd(e);
      }

      Object.assign(_assertThisInitialized(_assertThisInitialized(_this)), {
        rotating: false
      });
    });

    return _this;
  }

  _createClass(TransformComponent, [{
    key: "getAngle",
    // 获取角度
    value: function getAngle(rotation, skew) {
      if (rotation) {
        var angle = Math.round(rotation / Math.PI * 180);
        return angle;
      }

      return -Math.round(skew && skew.x && skew.x / Math.PI * 180 || 0);
    }
    /**
     * 获取旋转后的坐标
     *
     * @param {*} point 当前坐标
     * @param {number} angle 旋转角度
     * @param {*} center 中心点坐标
     * @returns {object}
     * @memberof TransformComponent
     */

  }, {
    key: "getRotatePoint",
    value: function getRotatePoint(point, angle, center) {
      var cos = Math.cos,
          sin = Math.sin,
          PI = Math.PI;
      var rad = angle * PI / 180;
      return {
        x: (point.x - center.x) * cos(rad) - (point.y - center.y) * sin(rad) + center.x,
        y: (point.y - center.y) * cos(rad) + (point.x - center.x) * sin(rad) + center.y
      };
    } // 角度文字props

  }, {
    key: "getAngleTextProps",
    value: function getAngleTextProps(angle, rotation, rotatorProps) {
      var length = angle.toString().length - 1; // 文字长度

      var wordLength = 4 - length * 3;

      var _this$getRotatePoint = this.getRotatePoint({
        x: rotatorProps.x + wordLength,
        y: rotatorProps.y + 25
      }, -angle, {
        x: rotatorProps.x + 10,
        y: rotatorProps.y + 10
      }),
          x = _this$getRotatePoint.x,
          y = _this$getRotatePoint.y;

      return {
        x: x,
        y: y,
        rotation: -rotation,
        text: "".concat(angle, "\xB0"),
        style: {
          stroke: 'white',
          fill: this.props.textStyle && this.props.textStyle.color || 'black',
          fontSize: this.props.textStyle && this.props.textStyle.fontSize || 14
        }
      };
    }
  }, {
    key: "getCornerProps",
    value: function getCornerProps() {
      var cornerStyle = (0, _defaults2.default)(this.props.cornerStyle, {
        color: 0x000000,
        width: 8,
        height: 8,
        radius: 0,
        borderWidth: 1,
        borderColor: 0x000000,
        backgroundColor: 0xFFFFFF
      });
      return {
        buttonMode: false,
        fillAlpha: 1,
        fill: cornerStyle.backgroundColor,
        width: cornerStyle.width,
        height: cornerStyle.height,
        lineWidth: cornerStyle.borderWidth,
        lineColor: cornerStyle.borderColor,
        radius: cornerStyle.radius
      };
    }
  }, {
    key: "getBorderProps",
    value: function getBorderProps() {
      var borderStyle = (0, _defaults2.default)(this.props.borderStyle, {
        color: 0x000000,
        width: 1,
        opacity: 1,
        dottedWidth: 3
      });
      return {
        width: this.props.width,
        height: this.props.height,
        x: 0,
        y: 0,
        lineWidth: borderStyle.width,
        dottedWidth: borderStyle.dottedWidth,
        lineColor: borderStyle.color,
        lineAlpha: borderStyle.opacity
      };
    }
  }, {
    key: "getRotatorProps",
    value: function getRotatorProps() {
      var rotatorStyle = (0, _defaults2.default)(this.props.rotatorStyle, {
        url: _default.DEFAULT_ROTATOR_ICON,
        width: 20,
        height: 20
      });
      return _objectSpread({}, rotatorStyle, {
        x: this.props.width / 2 - rotatorStyle.width / 2,
        y: this.props.height + rotatorStyle.height + 10,
        interactive: true,
        cursor: 'pointer',
        pointerdown: this.onSvgPointerdown,
        pointermove: this.onSvgPointermove,
        pointerup: this.onSvgPointerup,
        pointerupoutside: this.onPointerupoutside
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var props = new _transformModel.default(this.props, {
        omit: ['filters', 'alpha', 'scale'],
        autoSetEvent: true
      });
      var angle = this.getAngle(props.rotation, props.skew); // 计算后删除 skew

      (0, _utils.deleteProperty)(props, ['skew']); // skew 转 rotation 解决翻转后点击 rotation 为0的 bug

      props.rotation = angle / 180 * Math.PI;
      var cornerProps = this.getCornerProps();
      return React.createElement(ReactPIXI.Container, props, React.createElement(_rect.default, {
        width: this.props.width,
        height: this.props.height,
        lineWidth: 1,
        lineColor: 0xFFFFFF,
        cursor: 'move',
        pointerdown: this.onPointerdown,
        pointermove: this.onPointermove,
        pointerup: this.onPointerup,
        pointerupoutside: this.onPointerupoutside
      }), function () {
        if (_this2.props.opacity) {
          var borderProps = _this2.getBorderProps();

          var rotatorProps = _this2.getRotatorProps();

          var angleTextProps = _this2.getAngleTextProps((0, _utils.intLimit)(angle, 360), props.rotation, rotatorProps);

          return React.createElement(React.Fragment, null, React.createElement(_rect.default, borderProps), React.createElement(_text.default, angleTextProps), React.createElement(_svg.default, rotatorProps));
        }

        return null;
      }(), React.createElement(_common.default, {
        commonProps: cornerProps,
        width: this.props.width,
        height: this.props.height,
        angle: angle,
        onPointerupoutside: this.onPointerupoutside,
        onRectPointerup: this.onRectPointerup,
        onRectPointerdown: this.onRectPointerdown,
        resize: this.props.resize
      }));
    }
  }, {
    key: "hitArea",
    get: function get() {
      return new PIXI.Rectangle(0, 0, this.props.width, this.props.height);
    }
  }]);

  return TransformComponent;
}(React.PureComponent);

exports.default = TransformComponent;

_defineProperty(TransformComponent, "cursors", _common.cursors);