"use strict";

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

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

var _constant = require("./constant");

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; if (obj != null) { var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }

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

function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }

var CodeCard = (0, _react.memo)(function (props) {
  var ref = (0, _react.useRef)(null);
  var src = props.src,
      onMoveItem = props.onMoveItem,
      id = props.id,
      _props$onDragEnd = props.onDragEnd,
      onDragEnd = _props$onDragEnd === void 0 ? function () {} : _props$onDragEnd,
      _props$onRemoveItem = props.onRemoveItem,
      onRemoveItem = _props$onRemoveItem === void 0 ? function () {} : _props$onRemoveItem;

  var _useDrag = (0, _reactDnd.useDrag)({
    item: {
      type: _constant.ItemTypes.CARD,
      props: props
    },
    collect: function collect(monitor) {
      return {
        isDragging: !!monitor.isDragging()
      };
    },
    end: function end(dropResult, monitor) {
      var item = monitor.getItem();
      var didDrop = monitor.didDrop();

      if (!didDrop) {
        onRemoveItem(item.id);
      }
    }
  }),
      _useDrag2 = _slicedToArray(_useDrag, 2),
      isDragging = _useDrag2[0].isDragging,
      drag = _useDrag2[1];

  var _useDrop = (0, _reactDnd.useDrop)({
    accept: _constant.ItemTypes.CARD,
    hover: function hover(item) {
      return onHover(item);
    },
    drop: function drop() {
      return onDragEnd();
    },
    collect: function collect(monitor) {
      return {
        isOver: !!monitor.isOver(),
        canDrop: !!monitor.canDrop()
      };
    }
  }),
      _useDrop2 = _slicedToArray(_useDrop, 2),
      _useDrop2$ = _useDrop2[0],
      isOver = _useDrop2$.isOver,
      canDrop = _useDrop2$.canDrop,
      drop = _useDrop2[1];

  var onHover = function onHover(item) {
    if (item.props.id !== id) {
      onMoveItem(item.props.id, id);
    }
  };

  var opacity = isOver && id !== undefined ? 0.2 : 1;
  drag(drop(ref));
  return _react["default"].createElement("img", {
    ref: ref,
    style: {
      width: '100%',
      opacity: opacity
    },
    src: src
  });
});
var _default = CodeCard;
exports["default"] = _default;