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/table/Table/tableConfig/SortRow/index.tsx
var SortRow_exports = {};
__export(SortRow_exports, {
  default: () => SortRow_default
});
module.exports = __toCommonJS(SortRow_exports);
var import_react = __toESM(require("react"));
var import_utils = require("@pisell/utils");
var import_core = require("@dnd-kit/core");
var import_modifiers = require("@dnd-kit/modifiers");
var import_sortable = require("@dnd-kit/sortable");
var import_utils2 = require("../../utils");
var import_hooks = require("../../../../../hooks");
var import_model = require("../../../model");
var import_index = require("./index.less");
var SortRow = ({ children, rowKey, onSortChange }) => {
  const { state, dispatch } = (0, import_hooks.useSharedState)(import_model.Context);
  const { originDataSource } = state;
  const oldOriginDataSource = (0, import_react.useRef)(null);
  const onDragEnd = async ({ active, over }) => {
    if (active.id !== (over == null ? void 0 : over.id)) {
      const activeIndex = originDataSource.findIndex(
        (i) => (0, import_utils2.getIdByRowKey)(i, rowKey) === active.id
      );
      const overIndex = originDataSource.findIndex(
        (i) => (0, import_utils2.getIdByRowKey)(i, rowKey) === (over == null ? void 0 : over.id)
      );
      const newData = (0, import_sortable.arrayMove)(originDataSource, activeIndex, overIndex);
      dispatch({
        type: "setOriginDataSource",
        payload: newData
      });
      if ((0, import_utils.isFunction)(onSortChange)) {
        oldOriginDataSource.current = originDataSource;
        const isError = await onSortChange({
          fromIndex: activeIndex,
          toIndex: overIndex,
          dataSource: newData,
          fromId: active.id,
          toId: over == null ? void 0 : over.id,
          fromItem: originDataSource[activeIndex],
          toItem: originDataSource[overIndex]
        });
        if (isError) {
          dispatch({
            type: "setOriginDataSource",
            payload: oldOriginDataSource.current
          });
        } else {
          oldOriginDataSource.current = null;
        }
      }
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { className: "grid-view-table-wrap" }, /* @__PURE__ */ import_react.default.createElement(import_core.DndContext, { modifiers: [import_modifiers.restrictToVerticalAxis], onDragEnd }, /* @__PURE__ */ import_react.default.createElement(
    import_sortable.SortableContext,
    {
      items: originDataSource.map((i) => (0, import_utils2.getIdByRowKey)(i, rowKey)),
      strategy: import_sortable.verticalListSortingStrategy
    },
    children
  )));
};
var SortRow_default = SortRow;
