function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
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(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
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(arg) { var key = _toPrimitive(arg, "string"); return _typeof(key) === "symbol" ? key : String(key); }
function _toPrimitive(input, hint) { if (_typeof(input) !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (_typeof(res) !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); }
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(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
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 _objectSpread2;
      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), {}, (_objectSpread2 = {}, _defineProperty(_objectSpread2, activeContainer, list[activeContainer].filter(function (item) {
        return getRowKey(item) !== active.id;
      })), _defineProperty(_objectSpread2, overContainer, [].concat(_toConsumableArray(list[overContainer].slice(0, newIndex)), [list[activeContainer][activeIndex]], _toConsumableArray(list[overContainer].slice(newIndex, list[overContainer].length)))), _objectSpread2));
      if (overContainer === 'quickFilter' && newList[overContainer].length > quickFilterMaxLength) {
        var _objectSpread3;
        newList = _objectSpread(_objectSpread({}, newList), {}, (_objectSpread3 = {}, _defineProperty(_objectSpread3, overContainer, newList[overContainer].slice(0, quickFilterMaxLength)), _defineProperty(_objectSpread3, activeContainer, [].concat(_toConsumableArray(newList[overContainer].slice(quickFilterMaxLength)), _toConsumableArray(newList[activeContainer]))), _objectSpread3));
      }
      onChange === null || onChange === void 0 ? 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 ? 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;