function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
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) { _defineProperty(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; }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { DndContext } from '@dnd-kit/core';
import { SortableContext, arrayMove, verticalListSortingStrategy, useSortable } from '@dnd-kit/sortable';
import { isFunction } from '@pisell/utils';
import React, { useCallback, useEffect, useState } from 'react';
import { SortableItem } from "../DragSortList";
import "./index.less";
import { getText } from "../../locales";
export var TRASH_ID = 'void';
var DefaultRenderContainer = function DefaultRenderContainer(props) {
  var id = props.id,
    children = props.children,
    renderContainer = props.renderContainer;
  var _useSortable = useSortable({
      id: id
    }),
    setNodeRef = _useSortable.setNodeRef;
  if (isFunction(renderContainer)) {
    return renderContainer({
      children: children,
      ref: setNodeRef,
      id: id
    });
  }
  return /*#__PURE__*/React.createElement("div", {
    ref: setNodeRef
  }, children);
};
var MultipleContainersSortList = function MultipleContainersSortList(props) {
  var wrapStyle = props.wrapStyle,
    list = props.list,
    children = props.children,
    onChange = props.onChange,
    renderContainer = props.renderContainer,
    _props$rowKey = props.rowKey,
    rowKey = _props$rowKey === void 0 ? 'id' : _props$rowKey,
    quickFilterMaxLength = props.quickFilterMaxLength;
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    containers = _useState2[0],
    setContainers = _useState2[1];
  var getRowKey = useCallback(function (row) {
    if (isFunction(rowKey)) {
      return rowKey(row);
    }
    return row[rowKey];
  }, [rowKey]);
  useEffect(function () {
    setContainers(Object.keys(list));
  }, [list]);
  var findContainer = function findContainer(id) {
    if (id in list) {
      return id;
    }
    return Object.keys(list).find(function (key) {
      return list[key].map(function (item) {
        return getRowKey(item);
      }).includes(id);
    });
  };
  var handleDragOver = function handleDragOver(_ref) {
    var active = _ref.active,
      over = _ref.over;
    var overId = over === null || over === void 0 ? void 0 : over.id;
    if (overId == null || overId === TRASH_ID || active.id in list) {
      return;
    }
    var overContainer = findContainer(overId);
    var activeContainer = findContainer(active.id);
    if (!overContainer || !activeContainer) {
      return;
    }
    if (activeContainer !== overContainer) {
      var activeItems = list[activeContainer];
      var overItems = list[overContainer];
      var overIndex = overItems.findIndex(function (item) {
        return getRowKey(item) === overId;
      });
      var activeIndex = activeItems.findIndex(function (item) {
        return getRowKey(item) === active.id;
      });
      var newIndex;
      if (overId in list) {
        newIndex = overItems.length + 1;
      } else {
        var isBelowOverItem = over && active.rect.current.translated && active.rect.current.translated.top > over.rect.top + over.rect.height;
        var modifier = isBelowOverItem ? 1 : 0;
        newIndex = overIndex >= 0 ? overIndex + modifier : overItems.length + 1;
      }
      var newList = _objectSpread(_objectSpread({}, list), {}, _defineProperty(_defineProperty({}, activeContainer, list[activeContainer].filter(function (item) {
        return getRowKey(item) !== active.id;
      })), overContainer, [].concat(_toConsumableArray(list[overContainer].slice(0, newIndex)), [list[activeContainer][activeIndex]], _toConsumableArray(list[overContainer].slice(newIndex, list[overContainer].length)))));
      if (overContainer === 'quickFilter' && newList[overContainer].length > quickFilterMaxLength) {
        newList = _objectSpread(_objectSpread({}, newList), {}, _defineProperty(_defineProperty({}, overContainer, newList[overContainer].slice(0, quickFilterMaxLength)), activeContainer, [].concat(_toConsumableArray(newList[overContainer].slice(quickFilterMaxLength)), _toConsumableArray(newList[activeContainer]))));
      }
      onChange === null || onChange === void 0 || onChange(newList);
    }
  };
  var handleDragEnd = function handleDragEnd(_ref2) {
    var active = _ref2.active,
      over = _ref2.over;
    var overId = over === null || over === void 0 ? void 0 : over.id;
    var overContainer = findContainer(overId);
    var activeContainer = findContainer(active.id);
    if (!activeContainer) {
      return;
    }
    if (overContainer) {
      var activeIndex = list[activeContainer].findIndex(function (item) {
        return getRowKey(item) === active.id;
      });
      var overIndex = list[overContainer].findIndex(function (item) {
        return getRowKey(item) === overId;
      });
      if (activeIndex !== overIndex) {
        var newList = _objectSpread(_objectSpread({}, list), {}, _defineProperty({}, overContainer, arrayMove(list[overContainer], activeIndex, overIndex)));
        onChange === null || onChange === void 0 || onChange(newList);
      }
    }
  };
  return /*#__PURE__*/React.createElement(DndContext, {
    onDragOver: handleDragOver,
    onDragEnd: handleDragEnd
  }, /*#__PURE__*/React.createElement("div", {
    style: wrapStyle
  }, containers.map(function (container) {
    return /*#__PURE__*/React.createElement(DefaultRenderContainer, {
      id: container,
      key: container,
      renderContainer: renderContainer
    }, /*#__PURE__*/React.createElement(SortableContext, {
      key: container,
      items: list[container].map(function (item) {
        return getRowKey(item);
      }),
      strategy: verticalListSortingStrategy
    }, list[container].length ? list[container].map(function (item, index) {
      return !(item !== null && item !== void 0 && item.isHidden) ? /*#__PURE__*/React.createElement(SortableItem, {
        key: getRowKey(item),
        id: getRowKey(item),
        item: item,
        index: index
      }, children) : null;
    }) : /*#__PURE__*/React.createElement("span", {
      className: "materials-sort-list-empty"
    }, getText('table-action-filter-no-filter'))));
  })));
};
export default MultipleContainersSortList;