"use strict";

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

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

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

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

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

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 _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }

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); }

var cursors = ['s-resize', 'sw-resize', 'w-resize', 'nw-resize', 'n-resize', 'ne-resize', 'e-resize', 'se-resize'];
/**
 * 通过角度获取当前点击的控制点索引
 *
 * @param {number} angle
 * @memberof TransformComponent
 */

exports.cursors = cursors;

var getIndexByAngle = function getIndexByAngle(angle) {
  if (angle < 25 || angle >= 335 && angle < 360) {
    return 0;
  }

  if (angle >= 25 && angle < 65) {
    return 1;
  }

  if (angle >= 65 && angle < 105) {
    return 2;
  }

  if (angle >= 105 && angle < 165) {
    return 3;
  }

  if (angle >= 165 && angle < 205) {
    return 4;
  }

  if (angle >= 205 && angle < 245) {
    return 5;
  }

  if (angle >= 245 && angle < 285) {
    return 6;
  }

  return 7;
};

exports.getIndexByAngle = getIndexByAngle;

var getGuides = function getGuides(props) {
  var width = props.width,
      height = props.height;
  var guides = [{
    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: 0x005cf9,
      lineWidth: 1,
      fillAlpha: 1
    });
  });
  return guides;
};
/**
 * 获取控制点
 * @param props
 * @return {React.ReactNode} { around: 顶角的控制点, lr: 左右的控制点, tb: 上下的控制点 }
 */


exports.getGuides = getGuides;

var getResizeControl = function getResizeControl(props) {
  var width = props.width,
      height = props.height,
      angle = props.angle,
      commonProps = props.commonProps,
      onRectPointerdown = props.onRectPointerdown,
      onRectPointerup = props.onRectPointerup,
      onPointerupoutside = props.onPointerupoutside;
  var bottomLeftIndex = getIndexByAngle((0, _utils.intLimit)(angle + 45, 360));
  var topLeftIndex = getIndexByAngle((0, _utils.intLimit)(angle + 135, 360));
  var topRightIndex = getIndexByAngle((0, _utils.intLimit)(angle + 225, 360));
  var bottomRightIndex = getIndexByAngle((0, _utils.intLimit)(angle + 315, 360));
  var topIndex = getIndexByAngle((0, _utils.intLimit)(angle + 180, 360));
  var bottomIndex = getIndexByAngle((0, _utils.intLimit)(angle, 360));
  var leftIndex = getIndexByAngle((0, _utils.intLimit)(angle + 90, 360));
  var rightIndex = getIndexByAngle((0, _utils.intLimit)(angle + 270, 360));
  var halfRectWidth = commonProps.width / 2;
  var helfRectHeight = commonProps.height / 2;
  var tb = [{
    key: 'n',
    cursor: cursors[topIndex],
    x: width / 2 - halfRectWidth,
    y: -helfRectHeight,
    hitArea: new PIXI.Rectangle(width / 2 - 19, -15, 30, 30)
  }, {
    key: 's',
    cursor: cursors[bottomIndex],
    x: width / 2 - halfRectWidth,
    y: height - helfRectHeight,
    hitArea: new PIXI.Rectangle(width / 2 - 19, height - 15, 30, 30)
  }].map(function (item) {
    return React.createElement(_rect.default, _extends({}, commonProps, item, {
      pointerdown: function pointerdown(e) {
        return onRectPointerdown(e, item.key);
      },
      pointerup: onRectPointerup,
      pointerupoutside: onPointerupoutside
    }));
  });
  var lr = [{
    key: 'w',
    cursor: cursors[leftIndex],
    x: -halfRectWidth,
    y: height / 2 - helfRectHeight,
    hitArea: new PIXI.Rectangle(-15, height / 2 - 19, 30, 30)
  }, {
    key: 'e',
    cursor: cursors[rightIndex],
    x: width - halfRectWidth,
    y: height / 2 - helfRectHeight,
    hitArea: new PIXI.Rectangle(width - 15, height / 2 - 19, 30, 30)
  }].map(function (item) {
    return React.createElement(_rect.default, _extends({}, commonProps, item, {
      pointerdown: function pointerdown(e) {
        return onRectPointerdown(e, item.key);
      },
      pointerup: onRectPointerup,
      pointerupoutside: onPointerupoutside
    }));
  });
  var around = [{
    key: 'nw',
    cursor: cursors[topLeftIndex],
    x: -halfRectWidth,
    y: -helfRectHeight,
    hitArea: new PIXI.Rectangle(-15, -15, 30, 30)
  }, {
    key: 'sw',
    cursor: cursors[bottomLeftIndex],
    x: -halfRectWidth,
    y: height - helfRectHeight,
    hitArea: new PIXI.Rectangle(-15, height - 15, 30, 30)
  }, {
    key: 'ne',
    cursor: cursors[topRightIndex],
    x: width - halfRectWidth,
    y: -helfRectHeight,
    hitArea: new PIXI.Rectangle(width - 15, -15, 30, 30)
  }, {
    key: 'se',
    cursor: cursors[bottomRightIndex],
    x: width - halfRectWidth,
    y: height - helfRectHeight,
    hitArea: new PIXI.Rectangle(width - 15, height - 15, 30, 30)
  }].map(function (item) {
    return React.createElement(_rect.default, _extends({}, commonProps, item, {
      pointerdown: function pointerdown(e) {
        return onRectPointerdown(e, item.key);
      },
      pointerup: onRectPointerup,
      pointerupoutside: onPointerupoutside
    }));
  });
  return {
    around: around,
    lr: lr,
    tb: tb
  };
};
/**
 * 控制点
 *
 * @param {number} angle
 * @returns {array[React.ReactNode]}
 * @memberof TransformComponent
 */


var ResizesComponent = function ResizesComponent(props) {
  var _props$resize = props.resize,
      resize = _props$resize === void 0 ? 7 : _props$resize;
  var resizes = [];

  var _getResizeControl = getResizeControl(props),
      around = _getResizeControl.around,
      lr = _getResizeControl.lr,
      tb = _getResizeControl.tb;

  if (resize === 1) {
    resizes.push.apply(resizes, around);
  } else if (resize === 2) {
    resizes.push.apply(resizes, tb);
  } else if (resize === 3) {
    resizes.push.apply(resizes, lr);
  } else if (resize === 5) {
    resizes.push.apply(resizes, _toConsumableArray(lr).concat(_toConsumableArray(around)));
  } else if (resize === 7) {
    resizes.push.apply(resizes, _toConsumableArray(around).concat(_toConsumableArray(lr), _toConsumableArray(tb)));
  }

  return React.createElement(ReactPIXI.Container, null, resizes);
};

var _default = ResizesComponent;
exports.default = _default;