import { computed, createVNode, defineComponent, mergeProps } from "vue";
import { Dropdown } from "antdv-next";
import { EllipsisOutlined } from "@antdv-next/icons";
//#region components/actions/ActionsMenu.tsx
var keySeed = 0;
function findItem(keyPath, items) {
	const keyToFind = keyPath[0];
	for (const item of items) {
		if (!item) return null;
		if (String(item.key) === keyToFind) {
			if (keyPath.length === 1) return item;
			if (item.subItems?.length) return findItem(keyPath.slice(1), item.subItems);
		}
	}
	return null;
}
var ActionsMenu = /* @__PURE__ */ defineComponent({
	name: "XActionsMenu",
	props: {
		item: {
			type: Object,
			required: true
		},
		onClick: {
			type: Function,
			default: void 0
		},
		dropdownProps: {
			type: Object,
			default: void 0
		},
		prefixCls: {
			type: String,
			default: "antd-actions"
		},
		classes: {
			type: Object,
			default: () => ({})
		},
		styles: {
			type: Object,
			default: () => ({})
		}
	},
	setup(props) {
		const fallbackKey = `actions-menu-${++keySeed}`;
		const itemKey = computed(() => {
			if (props.item?.key === void 0 || props.item?.key === null) return fallbackKey;
			return String(props.item.key);
		});
		const mergedDropdownStyles = computed(() => {
			const mergedPropsStyles = props.dropdownProps?.styles;
			if (typeof mergedPropsStyles === "function") return mergedPropsStyles;
			return {
				...mergedPropsStyles,
				root: {
					...props.styles?.itemDropdown,
					...mergedPropsStyles?.root
				}
			};
		});
		const menuProps = computed(() => {
			const subItems = props.item.subItems ?? [];
			return {
				items: subItems,
				triggerSubMenuAction: props.item.triggerSubMenuAction ?? "hover",
				onClick: (info) => {
					const keyPath = (info?.keyPath ?? []).map((key) => String(key));
					const currentItem = findItem(keyPath, subItems);
					if (currentItem?.onItemClick) {
						currentItem.onItemClick(currentItem);
						return;
					}
					props.onClick?.({
						key: String(info.key),
						keyPath: [...keyPath, itemKey.value],
						domEvent: info.domEvent,
						item: currentItem ?? props.item
					});
				}
			};
		});
		return () => createVNode(Dropdown, mergeProps({
			"menu": menuProps.value,
			"trigger": [props.item.triggerSubMenuAction ?? "hover"]
		}, props.dropdownProps, {
			"class": [
				`${props.prefixCls}-dropdown`,
				props.classes?.itemDropdown,
				props.dropdownProps?.classes?.root
			],
			"styles": mergedDropdownStyles.value
		}), { default: () => [createVNode("div", {
			"class": [
				`${props.prefixCls}-item`,
				`${props.prefixCls}-sub-item`,
				props.classes?.item
			],
			"style": props.styles?.item
		}, [createVNode("div", { "class": `${props.prefixCls}-icon` }, [props.item?.icon ?? createVNode(EllipsisOutlined, null, null)])])] });
	}
});
//#endregion
export { ActionsMenu as default, findItem };
