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/drag-table/index.tsx
var drag_table_exports = {};
__export(drag_table_exports, {
  default: () => drag_table_default
});
module.exports = __toCommonJS(drag_table_exports);
var import_react = __toESM(require("react"));
var import_react_sortable_hoc = require("react-sortable-hoc");
var import_antd = require("antd");
var import_hooks = require("../_util/hooks");
var import_iconfont = __toESM(require("../iconfont"));
var import_index = require("./index.less");
var DragHandle = (0, import_react_sortable_hoc.SortableHandle)(() => /* @__PURE__ */ import_react.default.createElement(import_iconfont.default, { type: "icon-ehr-ui-drag", style: { cursor: "move", color: "#999" } }));
var SortableItem = (0, import_react_sortable_hoc.SortableElement)((props) => /* @__PURE__ */ import_react.default.createElement("tr", { ...props }));
var Container = (0, import_react_sortable_hoc.SortableContainer)((props) => /* @__PURE__ */ import_react.default.createElement("tbody", { ...props }));
var DEFAULT_VALUE = [];
var useDragParams = (props) => {
  const { columns = [], dataSource: originData = DEFAULT_VALUE, rowKey = "id", onSort, components } = props;
  if (components) {
    console.error("useDragTable函数会覆盖传入Table组件的components属性哦，若原组件必须设置components属性请避免结合useDragTable函数使用");
  }
  const [dataSource, setDataSource] = (0, import_react.useState)([...originData]);
  (0, import_react.useEffect)(() => {
    setDataSource([...originData]);
  }, [originData]);
  const onSortEnd = ({ oldIndex, newIndex }) => {
    if (oldIndex !== newIndex) {
      let list = [...dataSource];
      const [item] = list.splice(oldIndex, 1);
      list.splice(newIndex, 0, item);
      setDataSource(list);
      onSort && onSort(oldIndex, newIndex, list);
    }
  };
  const DraggableContainer = (props2) => /* @__PURE__ */ import_react.default.createElement(Container, { useDragHandle: true, helperClass: "row-dragging", onSortEnd, ...props2 });
  const DraggableBodyRow = ({ className, style, ...restProps }) => {
    const index = dataSource.findIndex(
      (x) => x[typeof rowKey === "string" || typeof rowKey === "number" ? rowKey : rowKey(x)] === restProps["data-row-key"]
    );
    return /* @__PURE__ */ import_react.default.createElement(SortableItem, { index, ...restProps });
  };
  const newColumns = [
    {
      title: "",
      dataIndex: "sort",
      width: 30,
      className: "drag-visible",
      render: () => /* @__PURE__ */ import_react.default.createElement(DragHandle, null)
    },
    ...columns
  ];
  return {
    rowKey,
    dataSource,
    columns: newColumns,
    components: {
      body: {
        wrapper: DraggableContainer,
        row: DraggableBodyRow
      }
    }
  };
};
var useDragTable = (element, option) => {
  const extraParams = useDragParams({ ...element.props, ...option });
  return (0, import_hooks.useConfig)(
    (0, import_react.cloneElement)(element, {
      ...element.props,
      ...extraParams
    })
  );
};
var DragTable = (props) => {
  const extraParams = useDragParams(props);
  return (0, import_hooks.useConfig)(/* @__PURE__ */ import_react.default.createElement(import_antd.Table, { ...props, ...extraParams }));
};
DragTable.useDragTable = useDragTable;
var drag_table_default = DragTable;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {});
