const require_runtime = require("../../_virtual/_rolldown/runtime.js");
const require_objectSpread2 = require("../../_virtual/_@oxc-project_runtime@0.122.0/helpers/objectSpread2.js");
const require_index = require("../../utils/index.js");
let react = require("react");
react = require_runtime.__toESM(react);
let antd = require("antd");
let _ant_design_icons = require("@ant-design/icons");
let _dnd_kit_core = require("@dnd-kit/core");
let _dnd_kit_sortable = require("@dnd-kit/sortable");
//#region src/components/buttonGroupEdit/index.tsx
const commonStyle = {
	cursor: "move",
	transition: "transform 0.3s ease"
};
const DraggableButton = ({ buttonItem, buttonList, onEdit }) => {
	const { id, isMore = false, children = "", buttonProps = {} } = buttonItem;
	const { listeners, setNodeRef, transform, transition, isDragging } = (0, _dnd_kit_sortable.useSortable)({ id });
	const style = transform ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, 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) => {
			onEdit(buttonList.find((item) => item.id === e.key));
		}
	};
	return /* @__PURE__ */ react.default.createElement("div", require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ ref: setNodeRef }, listeners), {}, { style }), isMore ? /* @__PURE__ */ react.default.createElement(antd.Dropdown, { menu }, /* @__PURE__ */ react.default.createElement(antd.Button, { onClick: handleClick }, /* @__PURE__ */ react.default.createElement(_ant_design_icons.MoreOutlined, null))) : /* @__PURE__ */ react.default.createElement(antd.Button, require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({ style }, buttonProps), {}, { onClick: () => onEdit(buttonItem) }), children));
};
const ButtonGroupEdit = (0, react.forwardRef)((props, ref) => {
	const [buttonList, setButtonList] = (0, react.useState)(props.buttonList);
	const [buttonGroupList, setButtonGroupList] = (0, react.useState)(props.buttonGroupList);
	const sensors = (0, _dnd_kit_core.useSensors)((0, _dnd_kit_core.useSensor)(_dnd_kit_core.PointerSensor, { activationConstraint: { distance: 5 } }));
	const [isModalVisible, setIsModalVisible] = (0, react.useState)(false);
	const [currentButton, setCurrentButton] = (0, react.useState)(null);
	const [form] = antd.Form.useForm();
	(0, react.useEffect)(() => {
		setButtonList(props.buttonList);
		if (props.buttonList.length > 0) setButtonList([...buttonList, {
			id: require_index.uuid(),
			isMore: true
		}]);
		else setButtonGroupList(props.buttonGroupList);
	}, [props.buttonList, props.buttonGroupList]);
	const addRow = () => {
		const newButton = {
			id: require_index.uuid(),
			children: `按钮${require_index.uuid()}`,
			buttonProps: { type: "default" }
		};
		setButtonGroupList((prev) => [...prev, [newButton]]);
	};
	(0, react.useEffect)(() => {
		if (currentButton) {
			var _currentButton$button;
			form.setFieldsValue({
				children: currentButton.children,
				type: (_currentButton$button = currentButton.buttonProps) === null || _currentButton$button === void 0 ? void 0 : _currentButton$button.type
			});
		}
	}, [currentButton, form]);
	const handleEditButton = (button) => {
		setCurrentButton(button);
		setIsModalVisible(true);
	};
	const handleCancel = () => {
		setIsModalVisible(false);
		setCurrentButton(null);
	};
	const handleSave = (values) => {
		if (!currentButton) return;
		const updatedButton = require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, currentButton), {}, {
			children: values.children,
			buttonProps: values
		});
		setButtonList((prevList) => prevList.map((item) => item.id === currentButton.id ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), updatedButton) : item));
		setButtonGroupList((prevGroupList) => prevGroupList.map((row) => row.map((item) => item.id === currentButton.id ? require_objectSpread2._objectSpread2(require_objectSpread2._objectSpread2({}, item), updatedButton) : item)));
		handleCancel();
	};
	const onDragEnd = (event) => {
		var _active$data, _over$data;
		const { active, over } = event;
		if (!over) return;
		let activeId = active.id;
		let overId = over.id;
		let activeContainerId = "";
		let overContainerId = "";
		if (active === null || active === void 0 || (_active$data = active.data) === null || _active$data === void 0 ? void 0 : _active$data.current) activeContainerId = active.data.current.sortable.containerId;
		if (over === null || over === void 0 || (_over$data = over.data) === null || _over$data === void 0 ? void 0 : _over$data.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 = require_objectSpread2._objectSpread2({}, 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") {
				var _buttonGroupList$over;
				const overRowIndex = buttonGroupList.findIndex((row) => row.some((item) => item.id === overId));
				if (overRowIndex === -1) return;
				const overColIndex = (_buttonGroupList$over = buttonGroupList[overRowIndex]) === null || _buttonGroupList$over === void 0 ? void 0 : _buttonGroupList$over.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") {
				var _buttonGroupList$acti, _buttonGroupList$over2;
				const activeRowIndex = buttonGroupList.findIndex((row) => row.some((item) => item.id === activeId));
				const activeColIndex = (_buttonGroupList$acti = buttonGroupList[activeRowIndex]) === null || _buttonGroupList$acti === void 0 ? void 0 : _buttonGroupList$acti.findIndex((item) => item.id === activeId);
				const overRowIndex = buttonGroupList.findIndex((row) => row.some((item) => item.id === overId));
				const overColIndex = (_buttonGroupList$over2 = buttonGroupList[overRowIndex]) === null || _buttonGroupList$over2 === void 0 ? void 0 : _buttonGroupList$over2.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__ */ react.default.createElement(antd.Flex, {
		wrap: true,
		justify: "space-between",
		align: "center",
		gap: "middle"
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_core.DndContext, {
		onDragEnd,
		sensors,
		collisionDetection: _dnd_kit_core.closestCenter
	}, /* @__PURE__ */ react.default.createElement(antd.Card, { title: "按钮列表" }, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		id: "list1",
		items: buttonList.map((item) => item.id),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, /* @__PURE__ */ react.default.createElement(antd.Flex, {
		gap: "middle",
		vertical: true
	}, buttonList.map((item) => /* @__PURE__ */ react.default.createElement(DraggableButton, {
		key: item.id,
		buttonItem: item,
		buttonList,
		onEdit: handleEditButton
	}))))), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		icon: /* @__PURE__ */ react.default.createElement(_ant_design_icons.SwapOutlined, null)
	}), /* @__PURE__ */ react.default.createElement(antd.Card, {
		title: "调整列表",
		style: { flex: 1 }
	}, /* @__PURE__ */ react.default.createElement(_dnd_kit_sortable.SortableContext, {
		id: "list2",
		items: buttonGroupList.flat().map((item) => item.id),
		strategy: _dnd_kit_sortable.verticalListSortingStrategy
	}, /* @__PURE__ */ react.default.createElement(antd.Flex, {
		gap: "middle",
		vertical: true
	}, buttonGroupList.map((subItem, rowIndex) => /* @__PURE__ */ react.default.createElement(antd.Card, null, /* @__PURE__ */ react.default.createElement(antd.Flex, {
		key: rowIndex,
		gap: "middle"
	}, subItem.map((item) => /* @__PURE__ */ react.default.createElement(DraggableButton, {
		key: item.id,
		buttonItem: item,
		buttonList,
		onEdit: handleEditButton
	}))))))))), /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		onClick: addRow
	}, "新增一行"), /* @__PURE__ */ react.default.createElement(antd.Modal, {
		title: "编辑按钮属性",
		open: isModalVisible,
		onCancel: handleCancel,
		footer: null
	}, currentButton && /* @__PURE__ */ react.default.createElement(antd.Form, {
		form,
		onFinish: handleSave
	}, /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "children",
		label: "按钮名称",
		rules: [{
			required: true,
			message: "请输入按钮名称"
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.Input, { placeholder: "请输入按钮名称" })), /* @__PURE__ */ react.default.createElement(antd.Form.Item, {
		name: "type",
		label: "按钮类型",
		rules: [{
			required: true,
			message: "请选择按钮类型"
		}]
	}, /* @__PURE__ */ react.default.createElement(antd.Radio.Group, null, /* @__PURE__ */ react.default.createElement(antd.Radio.Button, { value: "primary" }, "primary"), /* @__PURE__ */ react.default.createElement(antd.Radio.Button, { value: "dashed" }, "dashed"), /* @__PURE__ */ react.default.createElement(antd.Radio.Button, { value: "link" }, "link"), /* @__PURE__ */ react.default.createElement(antd.Radio.Button, { value: "text" }, "text"), /* @__PURE__ */ react.default.createElement(antd.Radio.Button, { value: "default" }, "default"))), /* @__PURE__ */ react.default.createElement(antd.Form.Item, null, /* @__PURE__ */ react.default.createElement(antd.Button, {
		type: "primary",
		htmlType: "submit"
	}, "保存")))));
});
var buttonGroupEdit_default = (0, react.memo)(ButtonGroupEdit);
//#endregion
exports.default = buttonGroupEdit_default;
