"use strict";

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

var _debounce = _interopRequireDefault(require("lodash/debounce"));

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

var _reactDnd = require("react-dnd");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

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

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

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? Object(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 _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; }

function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }

function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }

function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

/**
 * @class DropZone 空降区。
 * DropZone 封装了可放置的区域，当把对象拖动到 DropZone 并放开鼠标时，会触发 drop 动作。
 *
 * @param {string} direction
 * direction 表示 DropZone 在屏幕上的排列方向，不同的排列方式对应不同的 flex-box 样式，默认为垂直排列。
 * - vertical 表示沿垂直方向排列
 * - horizontal 表示沿水平方向排列
 *
 * @author zhang.xiaofei38@iwhalecloud.com
 */
var DropZone = function DropZone(_ref) {
  var id = _ref.id,
      pid = _ref.pid,
      index = _ref.index,
      onHover = _ref.onHover,
      onDrop = _ref.onDrop,
      isValidDest = _ref.isValidDest,
      acceptTypes = _ref.acceptTypes,
      _ref$direction = _ref.direction,
      direction = _ref$direction === void 0 ? 'vertical' : _ref$direction;

  var _useDrop = (0, _reactDnd.useDrop)({
    accept: _toConsumableArray(acceptTypes),
    hover: (0, _debounce["default"])(function (item, monitor) {
      if (!isOverCurrent) {
        return;
      }

      onHover && onHover(item, pid, index);
    }, 100),
    drop: function drop(item, monitor) {
      isValid && onDrop && onDrop(item, pid, index);
    },
    collect: function collect(monitor) {
      var item = monitor.getItem();
      return {
        isOver: monitor.isOver(),
        isOverCurrent: monitor.isOver({
          shallow: true
        }),
        isValid: item && acceptTypes.includes(item.type) && isValidDest(item, pid, index),
        isDragging: !!item
      };
    }
  }),
      _useDrop2 = _slicedToArray(_useDrop, 2),
      _useDrop2$ = _useDrop2[0],
      isOver = _useDrop2$.isOver,
      isDragging = _useDrop2$.isDragging,
      isOverCurrent = _useDrop2$.isOverCurrent,
      isValid = _useDrop2$.isValid,
      drop = _useDrop2[1]; // DropZone 的外观会根据排列的方向而发生变化，排列的方向只有两种，vertical or horizontal，默认为垂直排列。


  var getStyle = function getStyle() {
    var direction = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 'vertical';
    var isOverCurrent = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
    var isDragging = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    var isValid = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
    var style = {
      border: isDragging && isValid ? '1px dashed rgba(237,33,33,0.8)' : '1px dashed rgba(255,255,255,0)',
      backgroundColor: isDragging && isOverCurrent ? isValid ? 'rgba(237,33,33,0.8)' : 'rgba(214, 40, 40,0)' : 'rgba(250, 250, 250, 0)',
      display: isDragging && isValid ? 'block' : 'none',
      boxShadow: isDragging && isOverCurrent ? isValid ? '0px 0px 1px 1px rgba(237,33,33,0.8)' : 'none' : 'none',
      position: 'relative',
      padding: '0',
      color: 'white'
    };
    style = direction === 'vertical' ? _objectSpread({}, style, {
      // display: isDragging && isValid ? 'block' : 'none',
      // height: isOverCurrent ? '10px' : '10px',
      height: '10px',
      minHeight: '10px'
    }) : _objectSpread({}, style, {
      // display: isDragging && isValid ? 'block' : 'none',
      // width: isOverCurrent ? '10px' : '10px',
      width: '10px',
      minWidth: '10px',
      minHeight: '50px'
    });
    return style;
  };

  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: drop((0, _react.useRef)(null)),
    style: getStyle(direction, isOverCurrent, isDragging, isValid)
  });
};

var _default = DropZone;
exports["default"] = _default;