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/buttonGroupEdit/index.tsx
var buttonGroupEdit_exports = {};
__export(buttonGroupEdit_exports, {
  default: () => buttonGroupEdit_default
});
module.exports = __toCommonJS(buttonGroupEdit_exports);
var import_react = __toESM(require("react"));
var import_antd = require("antd");
var import_icons = require("@ant-design/icons");
var import_core = require("@dnd-kit/core");
var import_sortable = require("@dnd-kit/sortable");
var import_utils = require("../../utils/index");
var commonStyle = {
  cursor: "move",
  transition: "transform 0.3s ease"
};
var DraggableButton = ({ buttonItem, buttonList, onEdit }) => {
  const { id, isMore = false, children = "", buttonProps = {} } = buttonItem;
  const { listeners, setNodeRef, transform, transition, isDragging } = (0, import_sortable.useSortable)({
    id
  });
  const style = transform ? {
    ...commonStyle,
    transform: `translate3d(${transform.x}px, ${transform.y}px, 0)`,
    transition: isDragging ? "unset" : transition
  } : commonStyle;
  const handleClick = (e) => {
    if (isDragging) {
      e.stopPropagation();
      e.preventDefault();
      return;
    }
  };
  const menu = {
    items: buttonList.filter((item) => !item.isMore).map((item) => ({ key: item.id, label: item.children })),
    onClick: (e) => {
      const item = buttonList.find(
        (item2) => item2.id === e.key
      );
      onEdit(item);
    }
  };
  return /* @__PURE__ */ import_react.default.createElement("div", { ref: setNodeRef, ...listeners, style }, isMore ? /* @__PURE__ */ import_react.default.createElement(import_antd.Dropdown, { menu }, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { onClick: handleClick }, /* @__PURE__ */ import_react.default.createElement(import_icons.MoreOutlined, null))) : /* @__PURE__ */ import_react.default.createElement(
    import_antd.Button,
    {
      style,
      ...buttonProps,
      onClick: () => onEdit(buttonItem)
    },
    children
  ));
};
var ButtonGroupEdit = (0, import_react.forwardRef)(
  (props, ref) => {
    const [buttonList, setButtonList] = (0, import_react.useState)(
      props.buttonList
    );
    const [buttonGroupList, setButtonGroupList] = (0, import_react.useState)(props.buttonGroupList);
    const sensors = (0, import_core.useSensors)(
      (0, import_core.useSensor)(import_core.PointerSensor, {
        activationConstraint: {
          distance: 5
        }
      })
    );
    const [isModalVisible, setIsModalVisible] = (0, import_react.useState)(false);
    const [currentButton, setCurrentButton] = (0, import_react.useState)(null);
    const [form] = import_antd.Form.useForm();
    (0, import_react.useEffect)(() => {
      setButtonList(props.buttonList);
      if (props.buttonList.length > 0) {
        setButtonList([
          ...buttonList,
          {
            id: (0, import_utils.uuid)(),
            isMore: true
          }
        ]);
      } else {
        setButtonGroupList(props.buttonGroupList);
      }
    }, [props.buttonList, props.buttonGroupList]);
    const addRow = () => {
      const newButton = {
        id: (0, import_utils.uuid)(),
        children: `按钮${(0, import_utils.uuid)()}`,
        // 默认按钮文本
        buttonProps: {
          type: "default"
        }
      };
      setButtonGroupList((prev) => [...prev, [newButton]]);
    };
    (0, import_react.useEffect)(() => {
      var _a;
      if (currentButton) {
        form.setFieldsValue({
          children: currentButton.children,
          type: (_a = currentButton.buttonProps) == null ? void 0 : _a.type
        });
      }
    }, [currentButton, form]);
    const handleEditButton = (button) => {
      setCurrentButton(button);
      setIsModalVisible(true);
    };
    const handleCancel = () => {
      setIsModalVisible(false);
      setCurrentButton(null);
    };
    const handleSave = (values) => {
      if (!currentButton) return;
      const updatedButton = {
        ...currentButton,
        children: values.children,
        buttonProps: values
      };
      setButtonList(
        (prevList) => prevList.map(
          (item) => item.id === currentButton.id ? { ...item, ...updatedButton } : item
        )
      );
      setButtonGroupList(
        (prevGroupList) => prevGroupList.map(
          (row) => row.map(
            (item) => item.id === currentButton.id ? { ...item, ...updatedButton } : item
          )
        )
      );
      handleCancel();
    };
    const onDragEnd = (event) => {
      var _a, _b, _c, _d, _e;
      const { active, over } = event;
      if (!over) return;
      let activeId = active.id;
      let overId = over.id;
      let activeContainerId = "";
      let overContainerId = "";
      if ((_a = active == null ? void 0 : active.data) == null ? void 0 : _a.current) {
        activeContainerId = active.data.current.sortable.containerId;
      }
      if ((_b = over == null ? void 0 : over.data) == null ? void 0 : _b.current) {
        overContainerId = over.data.current.sortable.containerId;
      }
      try {
        if (activeContainerId === "list1" && overContainerId === "list2") {
          const activeIndex = buttonList.findIndex(
            (item) => item.id === activeId
          );
          if (activeIndex === -1) return;
          const movedItem = { ...buttonList[activeIndex] };
          const overRowIndex = buttonGroupList.findIndex(
            (row) => row.some((item) => item.id === overId)
          );
          if (overRowIndex === -1) return;
          const updatedGroupList = [...buttonGroupList];
          updatedGroupList[overRowIndex].push(movedItem);
          const updatedButtonList = buttonList.filter(
            (item) => item.id !== activeId
          );
          setButtonGroupList(updatedGroupList);
          setButtonList(updatedButtonList);
        }
        if (activeContainerId === "list2" && overContainerId === "list1") {
          const overRowIndex = buttonGroupList.findIndex(
            (row) => row.some((item) => item.id === overId)
          );
          if (overRowIndex === -1) return;
          const overColIndex = (_c = buttonGroupList[overRowIndex]) == null ? void 0 : _c.findIndex(
            (item) => item.id === overId
          );
          if (overColIndex === -1) return;
          const [movedItem] = buttonGroupList[overRowIndex].splice(
            overColIndex,
            1
          );
          setButtonGroupList([...buttonGroupList]);
          setButtonList((prev) => [...prev, movedItem]);
        }
        if (activeContainerId === "list1" && overContainerId === "list1") {
          const activeIndex = buttonList.findIndex(
            (item) => item.id === activeId
          );
          const overIndex = buttonList.findIndex((item) => item.id === overId);
          if (activeIndex === -1 || overIndex === -1) return;
          const updatedList = [...buttonList];
          const [movedItem] = updatedList.splice(activeIndex, 1);
          updatedList.splice(overIndex, 0, movedItem);
          setButtonList(updatedList);
        }
        if (activeContainerId === "list2" && overContainerId === "list2") {
          const activeRowIndex = buttonGroupList.findIndex(
            (row) => row.some((item) => item.id === activeId)
          );
          const activeColIndex = (_d = buttonGroupList[activeRowIndex]) == null ? void 0 : _d.findIndex(
            (item) => item.id === activeId
          );
          const overRowIndex = buttonGroupList.findIndex(
            (row) => row.some((item) => item.id === overId)
          );
          const overColIndex = (_e = buttonGroupList[overRowIndex]) == null ? void 0 : _e.findIndex(
            (item) => item.id === overId
          );
          if (activeRowIndex === -1 || overRowIndex === -1 || activeColIndex === -1 || overColIndex === -1)
            return;
          const updatedGroupList = [...buttonGroupList];
          const [movedItem] = updatedGroupList[activeRowIndex].splice(
            activeColIndex,
            1
          );
          updatedGroupList[overRowIndex].splice(overColIndex, 0, movedItem);
          setButtonGroupList(updatedGroupList);
        }
      } catch (error) {
        console.error("拖拽结束时出错:", error);
      }
    };
    return /* @__PURE__ */ import_react.default.createElement(import_antd.Flex, { wrap: true, justify: "space-between", align: "center", gap: "middle" }, /* @__PURE__ */ import_react.default.createElement(
      import_core.DndContext,
      {
        onDragEnd,
        sensors,
        collisionDetection: import_core.closestCenter
      },
      /* @__PURE__ */ import_react.default.createElement(import_antd.Card, { title: "按钮列表" }, /* @__PURE__ */ import_react.default.createElement(
        import_sortable.SortableContext,
        {
          id: "list1",
          items: buttonList.map((item) => item.id),
          strategy: import_sortable.verticalListSortingStrategy
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Flex, { gap: "middle", vertical: true }, buttonList.map((item) => /* @__PURE__ */ import_react.default.createElement(
          DraggableButton,
          {
            key: item.id,
            buttonItem: item,
            buttonList,
            onEdit: handleEditButton
          }
        )))
      )),
      /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", icon: /* @__PURE__ */ import_react.default.createElement(import_icons.SwapOutlined, null) }),
      /* @__PURE__ */ import_react.default.createElement(import_antd.Card, { title: "调整列表", style: { flex: 1 } }, /* @__PURE__ */ import_react.default.createElement(
        import_sortable.SortableContext,
        {
          id: "list2",
          items: buttonGroupList.flat().map((item) => item.id),
          strategy: import_sortable.verticalListSortingStrategy
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Flex, { gap: "middle", vertical: true }, buttonGroupList.map((subItem, rowIndex) => /* @__PURE__ */ import_react.default.createElement(import_antd.Card, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Flex, { key: rowIndex, gap: "middle" }, subItem.map((item) => /* @__PURE__ */ import_react.default.createElement(
          DraggableButton,
          {
            key: item.id,
            buttonItem: item,
            buttonList,
            onEdit: handleEditButton
          }
        ))))))
      ))
    ), /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", onClick: addRow }, "新增一行"), /* @__PURE__ */ import_react.default.createElement(
      import_antd.Modal,
      {
        title: "编辑按钮属性",
        open: isModalVisible,
        onCancel: handleCancel,
        footer: null
      },
      currentButton && /* @__PURE__ */ import_react.default.createElement(import_antd.Form, { form, onFinish: handleSave }, /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          name: "children",
          label: "按钮名称",
          rules: [{ required: true, message: "请输入按钮名称" }]
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Input, { placeholder: "请输入按钮名称" })
      ), /* @__PURE__ */ import_react.default.createElement(
        import_antd.Form.Item,
        {
          name: "type",
          label: "按钮类型",
          rules: [{ required: true, message: "请选择按钮类型" }]
        },
        /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Group, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Button, { value: "primary" }, "primary"), /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Button, { value: "dashed" }, "dashed"), /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Button, { value: "link" }, "link"), /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Button, { value: "text" }, "text"), /* @__PURE__ */ import_react.default.createElement(import_antd.Radio.Button, { value: "default" }, "default"))
      ), /* @__PURE__ */ import_react.default.createElement(import_antd.Form.Item, null, /* @__PURE__ */ import_react.default.createElement(import_antd.Button, { type: "primary", htmlType: "submit" }, "保存")))
    ));
  }
);
var buttonGroupEdit_default = (0, import_react.memo)(ButtonGroupEdit);
