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); }
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import Draggable from '../draggable';
import SetupSection from '../setup-section';
import CourseSection from '../course-section';
import ListItem from '../../organism/list-item';
import LearningPrioritySetupItem from '../learning-priority-setup-item';
const ITEMS = {
  'setup-section': SetupSection,
  'course-section': CourseSection,
  'list-item': ListItem,
  'learning-priority-setup-item': LearningPrioritySetupItem
};
const DraggableList = ({
  items,
  onDrop,
  itemType
}) => {
  const [dragTo, setDragTo] = useState(null);
  const [dragFrom, setDragFrom] = useState(null);
  const dragStartHandler = useCallback(id => {
    setDragFrom(id);
  }, [setDragFrom]);
  const dragOverHandler = useCallback(id => {
    if (dragTo === id) return;
    setDragTo(id);
  }, [dragTo, setDragTo]);
  const dragLeaveHandler = useCallback(id => {
    setDragTo(null);
  }, [setDragTo]);
  const dropHandler = 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.createElement(Draggable, {
    key: item.id,
    id: item.id,
    dragging: dragTo === item.id,
    onDragStart: dragStartHandler,
    onDragOver: dragOverHandler,
    onDragLeave: dragLeaveHandler,
    onDrop: dropHandler
  }, /*#__PURE__*/React.createElement(Item, _extends({}, item, {
    key: item.id,
    order: index
  }))));
  return /*#__PURE__*/React.createElement("div", {
    "data-name": `draggable-list-${itemType}`
  }, itemsView);
};
DraggableList.propTypes = process.env.NODE_ENV !== "production" ? {
  items: PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.shape(SetupSection.propTypes), PropTypes.shape(CourseSection.propTypes), PropTypes.shape(ListItem.propTypes), PropTypes.shape(LearningPrioritySetupItem.propTypes)])),
  onDrop: PropTypes.func,
  itemType: PropTypes.string
} : {};
export default DraggableList;
//# sourceMappingURL=index.js.map