"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
var _typeof = require("@babel/runtime/helpers/typeof");
Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = _default;
var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactDnd = require("react-dnd");
var _ItemTypes = _interopRequireDefault(require("./ItemTypes"));
var _reactDom = require("react-dom");
var _excluded = ["id", "index", "moveCard"];
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
var style = {
  border: '1px dashed gray',
  padding: '0.5rem 1rem',
  marginBottom: '.5rem',
  backgroundColor: 'white',
  cursor: 'pointer'
};
function _default(ComposedComponent) {
  var Card = function Card(_ref) {
    var id = _ref.id,
      index = _ref.index,
      moveCard = _ref.moveCard,
      props = (0, _objectWithoutProperties2["default"])(_ref, _excluded);
    var ref = (0, _react.useRef)(null);
    var _useDrop = (0, _reactDnd.useDrop)({
        accept: [_ItemTypes["default"].CARD, _ItemTypes["default"].BOX],
        hover: function hover(item, monitor) {
          if (!ref.current) {
            return;
          }
          var dragIndex = item.index;
          var hoverIndex = index;

          // Don't replace items with themselves
          if (dragIndex === hoverIndex) {
            return;
          }

          // Determine rectangle on screen
          var hoverBoundingRect = (0, _reactDom.findDOMNode)(ref.current).getBoundingClientRect();

          // Get vertical middle
          var hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;

          // Determine mouse position
          var clientOffset = monitor.getClientOffset();

          // Get pixels to the top
          var hoverClientY = clientOffset.y - hoverBoundingRect.top;

          // Only perform the move when the mouse has crossed half of the items height
          if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
            return;
          }
          if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
            return;
          }

          // Time to actually perform the action
          moveCard(dragIndex, hoverIndex);
          item.index = hoverIndex;
        },
        drop: function drop(item, monitor) {
          if (!ref.current) {
            return;
          }
          var dragIndex = item.index;
          var hoverIndex = index;

          // Don't replace items with themselves
          if (dragIndex === hoverIndex) {
            return;
          }

          // Determine rectangle on screen
          var hoverBoundingRect = (0, _reactDom.findDOMNode)(ref.current).getBoundingClientRect();

          // Get vertical middle
          var hoverMiddleY = (hoverBoundingRect.bottom - hoverBoundingRect.top) / 2;

          // Determine mouse position
          var clientOffset = monitor.getClientOffset();

          // Get pixels to the top
          var hoverClientY = clientOffset.y - hoverBoundingRect.top;

          // Only perform the move when the mouse has crossed half of the items height
          if (dragIndex < hoverIndex && hoverClientY < hoverMiddleY) {
            return;
          }
          if (dragIndex > hoverIndex && hoverClientY > hoverMiddleY) {
            return;
          }
          moveCard(hoverIndex, dragIndex);
          item.index = hoverIndex;

          // (cardTarget.drop logic goes here)
        }
      }),
      _useDrop2 = (0, _slicedToArray2["default"])(_useDrop, 2),
      drop = _useDrop2[1];
    var _useDrag = (0, _reactDnd.useDrag)({
        type: _ItemTypes["default"].CARD,
        item: {
          id: id,
          index: index
        },
        collect: function collect(monitor) {
          return {
            isDragging: monitor.isDragging()
          };
        }
      }),
      _useDrag2 = (0, _slicedToArray2["default"])(_useDrag, 3),
      isDragging = _useDrag2[0].isDragging,
      drag = _useDrag2[1],
      preview = _useDrag2[2];
    drag(drop(ref));
    var opacity = isDragging ? 0 : 1;
    return /*#__PURE__*/_react["default"].createElement("div", {
      ref: ref,
      style: _objectSpread(_objectSpread({}, style), {}, {
        opacity: opacity
      })
    }, /*#__PURE__*/_react["default"].createElement(ComposedComponent, props));
  };
  Card.propTypes = {
    id: _propTypes["default"].any.isRequired,
    index: _propTypes["default"].number.isRequired,
    moveCard: _propTypes["default"].func.isRequired
  };
  return Card;
}