"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _draggable = _interopRequireDefault(require("../draggable"));
var _setupSection = _interopRequireDefault(require("../setup-section"));
var _courseSection = _interopRequireDefault(require("../course-section"));
var _listItem = _interopRequireDefault(require("../../organism/list-item"));
var _learningPrioritySetupItem = _interopRequireDefault(require("../learning-priority-setup-item"));
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (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 && {}.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 _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
const ITEMS = {
  'setup-section': _setupSection.default,
  'course-section': _courseSection.default,
  'list-item': _listItem.default,
  'learning-priority-setup-item': _learningPrioritySetupItem.default
};
const DraggableList = ({
  items,
  onDrop,
  itemType
}) => {
  const [dragTo, setDragTo] = (0, _react.useState)(null);
  const [dragFrom, setDragFrom] = (0, _react.useState)(null);
  const dragStartHandler = (0, _react.useCallback)(id => {
    setDragFrom(id);
  }, [setDragFrom]);
  const dragOverHandler = (0, _react.useCallback)(id => {
    if (dragTo === id) return;
    setDragTo(id);
  }, [dragTo, setDragTo]);
  const dragLeaveHandler = (0, _react.useCallback)(id => {
    setDragTo(null);
  }, [setDragTo]);
  const dropHandler = (0, _react.useCallback)(id => {
    if (onDrop && dragFrom) onDrop(dragFrom, id);
    setDragTo(null);
    setDragFrom(null);
  }, [onDrop, setDragTo, setDragFrom, dragFrom]);
  const Item = ITEMS[itemType];
  const itemsView = items.map((item, index) => /*#__PURE__*/_react.default.createElement(_draggable.default, {
    key: item.id,
    id: item.id,
    dragging: dragTo === item.id,
    onDragStart: dragStartHandler,
    onDragOver: dragOverHandler,
    onDragLeave: dragLeaveHandler,
    onDrop: dropHandler
  }, /*#__PURE__*/_react.default.createElement(Item, _extends({}, item, {
    key: item.id,
    order: index
  }))));
  return /*#__PURE__*/_react.default.createElement("div", {
    "data-name": `draggable-list-${itemType}`
  }, itemsView);
};
DraggableList.propTypes = process.env.NODE_ENV !== "production" ? {
  items: _propTypes.default.arrayOf(_propTypes.default.oneOfType([_propTypes.default.shape(_setupSection.default.propTypes), _propTypes.default.shape(_courseSection.default.propTypes), _propTypes.default.shape(_listItem.default.propTypes), _propTypes.default.shape(_learningPrioritySetupItem.default.propTypes)])),
  onDrop: _propTypes.default.func,
  itemType: _propTypes.default.string
} : {};
var _default = exports.default = DraggableList;
//# sourceMappingURL=index.js.map