"use strict";

exports.__esModule = true;
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _provider = _interopRequireDefault(require("../../atom/provider"));
var _loader = _interopRequireDefault(require("../../atom/loader"));
var _select = require("../../atom/select");
var _draggableList = _interopRequireDefault(require("../../molecule/draggable-list"));
var _buttonLink = _interopRequireDefault(require("../../atom/button-link"));
var _learningPriorityModal = _interopRequireDefault(require("../../molecule/learning-priority-modal"));
var _style = _interopRequireDefault(require("./style.css"));
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; }
const BrandLearningPriorities = (props, context) => {
  const {
    content,
    priorities,
    filters,
    isLoading,
    onAdd
  } = props;
  const {
    items,
    onDrop,
    itemType
  } = content;
  const [itemList, setItemList] = (0, _react.useState)([...items]);
  const [openModal, setOpenModal] = (0, _react.useState)(false);
  const {
    translate
  } = context;
  const selectedPriorities = (0, _react.useMemo)(() => items.map(priority => priority.priorityRef), [items]);
  const handleDropItem = (0, _react.useCallback)((dragRef, dropRef) => {
    const dragIndex = itemList.findIndex(item => item.priorityRef === dragRef);
    const dropIndex = itemList.findIndex(item => item.priorityRef === dropRef);
    if (dragIndex === dropIndex) return;
    const newItems = [...itemList];
    const [dragItem] = newItems.splice(dragIndex, 1);
    newItems.splice(dropIndex, 0, dragItem);
    setItemList(newItems);
    onDrop(newItems.map(item => {
      return {
        type: item.type,
        ref: item.priorityRef
      };
    }));
  }, [itemList, setItemList, onDrop]);
  const Loading = (0, _react.useCallback)(() => /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.loaderContainer
  }, /*#__PURE__*/_react.default.createElement(_loader.default, {
    className: _style.default.loader,
    theme: "coorpmanager"
  })), []);
  (0, _react.useEffect)(() => {
    setItemList(items);
  }, [items]);
  const handleOpenModal = (0, _react.useCallback)(() => setOpenModal(true), [setOpenModal]);
  const handleCloseModal = (0, _react.useCallback)(() => setOpenModal(false), [setOpenModal]);
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.container
  }, /*#__PURE__*/_react.default.createElement(_learningPriorityModal.default, {
    isLoading: isLoading,
    isOpen: openModal,
    onCancel: handleCloseModal,
    onClose: handleCloseModal,
    onAdd: onAdd,
    priorities: priorities,
    filters: filters,
    preselected: selectedPriorities
  }), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.LPtitle
  }, translate('learning_priorities')), /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.LPdescription
  }, translate('learning_priorities_brand_description')), isLoading ? /*#__PURE__*/_react.default.createElement(Loading, null) : /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.ctaWrapper
  }, /*#__PURE__*/_react.default.createElement("div", {
    className: _style.default.priorityCount,
    "data-name": "learning-priority-count"
  }, `${items.length} ${translate('items')}`), /*#__PURE__*/_react.default.createElement(_buttonLink.default, {
    type: "primary",
    customStyle: {
      width: 'fit-content'
    },
    onClick: handleOpenModal,
    label: translate('add_learning_priority'),
    "data-name": "add-learning-priority",
    icon: {
      position: 'left',
      faIcon: {
        name: 'plus',
        color: '#FFFFFF',
        size: 16
      }
    }
  })), /*#__PURE__*/_react.default.createElement(_draggableList.default, {
    itemType: itemType,
    items: itemList,
    onDrop: handleDropItem
  })));
};
BrandLearningPriorities.contextTypes = {
  translate: _provider.default.childContextTypes.translate
};
BrandLearningPriorities.propTypes = process.env.NODE_ENV !== "production" ? {
  content: _propTypes.default.shape(_draggableList.default.propTypes),
  priorities: _propTypes.default.arrayOf(_propTypes.default.shape({
    ref: _propTypes.default.string,
    title: _propTypes.default.string,
    courses: _propTypes.default.number,
    type: _propTypes.default.string
  })),
  filters: _propTypes.default.arrayOf(_propTypes.default.shape({
    ..._select.SelectOptionPropTypes,
    count: _propTypes.default.number
  })),
  isLoading: _propTypes.default.bool,
  onAdd: _propTypes.default.func
} : {};
var _default = exports.default = BrandLearningPriorities;
//# sourceMappingURL=index.js.map