var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  // If the importer is in node compatibility mode or this is not an ESM
  // file that has been converted to a CommonJS file using a Babel-
  // compatible transform (i.e. "__esModule" has not been set), then set
  // "default" to the CommonJS "module.exports" for node compatibility.
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// src/components/MultipleContainersSortList/index.tsx
var MultipleContainersSortList_exports = {};
__export(MultipleContainersSortList_exports, {
  TRASH_ID: () => TRASH_ID,
  default: () => MultipleContainersSortList_default
});
module.exports = __toCommonJS(MultipleContainersSortList_exports);
var import_core = require("@dnd-kit/core");
var import_sortable = require("@dnd-kit/sortable");
var import_utils = require("@pisell/utils");
var import_react = __toESM(require("react"));
var import_DragSortList = require("../DragSortList");
var import_index = require("./index.less");
var import_locales = require("../../locales");
var TRASH_ID = "void";
var DefaultRenderContainer = (props) => {
  const { id, children, renderContainer } = props;
  const { setNodeRef } = (0, import_sortable.useSortable)({
    id
  });
  if ((0, import_utils.isFunction)(renderContainer)) {
    return renderContainer({ children, ref: setNodeRef, id });
  }
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: setNodeRef }, children);
};
var MultipleContainersSortList = (props) => {
  const {
    wrapStyle,
    list,
    children,
    onChange,
    renderContainer,
    rowKey = "id",
    quickFilterMaxLength
  } = props;
  const [containers, setContainers] = (0, import_react.useState)([]);
  const getRowKey = (0, import_react.useCallback)(
    (row) => {
      if ((0, import_utils.isFunction)(rowKey)) {
        return rowKey(row);
      }
      return row[rowKey];
    },
    [rowKey]
  );
  (0, import_react.useEffect)(() => {
    setContainers(Object.keys(list));
  }, [list]);
  const findContainer = (id) => {
    if (id in list) {
      return id;
    }
    return Object.keys(list).find(
      (key) => list[key].map((item) => getRowKey(item)).includes(id)
    );
  };
  const handleDragOver = ({ active, over }) => {
    const overId = over == null ? void 0 : over.id;
    if (overId == null || overId === TRASH_ID || active.id in list) {
      return;
    }
    const overContainer = findContainer(overId);
    const activeContainer = findContainer(active.id);
    if (!overContainer || !activeContainer) {
      return;
    }
    if (activeContainer !== overContainer) {
      const activeItems = list[activeContainer];
      const overItems = list[overContainer];
      const overIndex = overItems.findIndex(
        (item) => getRowKey(item) === overId
      );
      const activeIndex = activeItems.findIndex(
        (item) => getRowKey(item) === active.id
      );
      let newIndex;
      if (overId in list) {
        newIndex = overItems.length + 1;
      } else {
        const isBelowOverItem = over && active.rect.current.translated && active.rect.current.translated.top > over.rect.top + over.rect.height;
        const modifier = isBelowOverItem ? 1 : 0;
        newIndex = overIndex >= 0 ? overIndex + modifier : overItems.length + 1;
      }
      let newList = {
        ...list,
        [activeContainer]: list[activeContainer].filter(
          (item) => getRowKey(item) !== active.id
        ),
        [overContainer]: [
          ...list[overContainer].slice(0, newIndex),
          list[activeContainer][activeIndex],
          ...list[overContainer].slice(newIndex, list[overContainer].length)
        ]
      };
      if (overContainer === "quickFilter" && newList[overContainer].length > quickFilterMaxLength) {
        newList = {
          ...newList,
          [overContainer]: newList[overContainer].slice(
            0,
            quickFilterMaxLength
          ),
          [activeContainer]: [
            ...newList[overContainer].slice(quickFilterMaxLength),
            ...newList[activeContainer]
          ]
        };
      }
      onChange == null ? void 0 : onChange(newList);
    }
  };
  const handleDragEnd = ({ active, over }) => {
    const overId = over == null ? void 0 : over.id;
    const overContainer = findContainer(overId);
    const activeContainer = findContainer(active.id);
    if (!activeContainer) {
      return;
    }
    if (overContainer) {
      const activeIndex = list[activeContainer].findIndex(
        (item) => getRowKey(item) === active.id
      );
      const overIndex = list[overContainer].findIndex(
        (item) => getRowKey(item) === overId
      );
      if (activeIndex !== overIndex) {
        const newList = {
          ...list,
          [overContainer]: (0, import_sortable.arrayMove)(
            list[overContainer],
            activeIndex,
            overIndex
          )
        };
        onChange == null ? void 0 : onChange(newList);
      }
    }
  };
  return /* @__PURE__ */ import_react.default.createElement(import_core.DndContext, { onDragOver: handleDragOver, onDragEnd: handleDragEnd }, /* @__PURE__ */ import_react.default.createElement("div", { style: wrapStyle }, containers.map((container) => /* @__PURE__ */ import_react.default.createElement(
    DefaultRenderContainer,
    {
      id: container,
      key: container,
      renderContainer
    },
    /* @__PURE__ */ import_react.default.createElement(
      import_sortable.SortableContext,
      {
        key: container,
        items: list[container].map((item) => getRowKey(item)),
        strategy: import_sortable.verticalListSortingStrategy
      },
      list[container].length ? list[container].map(
        (item, index) => !(item == null ? void 0 : item.isHidden) ? /* @__PURE__ */ import_react.default.createElement(
          import_DragSortList.SortableItem,
          {
            key: getRowKey(item),
            id: getRowKey(item),
            item,
            index
          },
          children
        ) : null
      ) : /* @__PURE__ */ import_react.default.createElement("span", { className: "materials-sort-list-empty" }, (0, import_locales.getText)("table-action-filter-no-filter"))
    )
  ))));
};
var MultipleContainersSortList_default = MultipleContainersSortList;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  TRASH_ID
});
