import useXComponentConfig from "../_utils/hooks/use-x-component-config.js";
import { hasRenderableNode } from "../_utils/vue.js";
import style_default from "./style/index.js";
import { computed, createVNode, defineComponent, mergeProps, nextTick, onMounted, ref, useAttrs } from "vue";
import { useConfig } from "antdv-next/config-provider/context";
//#region components/prompts/Prompts.tsx
function hasChildren(item) {
	const children = item.children;
	return Array.isArray(children) && children.length > 0;
}
function getItemDomAttrs(item) {
	const domAttrs = { ...item };
	delete domAttrs.key;
	delete domAttrs.icon;
	delete domAttrs.label;
	delete domAttrs.description;
	delete domAttrs.disabled;
	delete domAttrs.class;
	delete domAttrs.style;
	delete domAttrs.children;
	return domAttrs;
}
var XPrompts = /* @__PURE__ */ defineComponent({
	name: "AxPrompts",
	inheritAttrs: false,
	props: {
		items: {
			type: Array,
			default: void 0
		},
		title: {
			type: [
				String,
				Number,
				Object,
				Array
			],
			default: void 0
		},
		vertical: {
			type: Boolean,
			default: false
		},
		wrap: {
			type: Boolean,
			default: false
		},
		styles: {
			type: Object,
			default: () => ({})
		},
		classes: {
			type: Object,
			default: () => ({})
		},
		prefixCls: {
			type: String,
			default: "antd-prompts"
		},
		rootClass: {
			type: String,
			default: ""
		},
		class: {
			type: [
				String,
				Array,
				Object
			],
			default: void 0
		},
		style: {
			type: [
				String,
				Object,
				Array
			],
			default: void 0
		},
		fadeIn: {
			type: Boolean,
			default: false
		},
		fadeInLeft: {
			type: Boolean,
			default: false
		}
	},
	emits: ["itemClick"],
	setup(props, { expose, emit, slots }) {
		const configCtx = useConfig();
		const attrs = useAttrs();
		const contextConfig = useXComponentConfig("prompts");
		const rootRef = ref();
		const [hashId, cssVarCls] = style_default(computed(() => props.prefixCls));
		const rootPrefixCls = computed(() => configCtx.value.getPrefixCls());
		const motionName = computed(() => {
			if (!props.fadeIn && !props.fadeInLeft) return "";
			return `${rootPrefixCls.value}-x-fade${props.fadeInLeft ? "-left" : ""}`;
		});
		const motionClass = ref("");
		onMounted(() => {
			if (!motionName.value) return;
			const mn = motionName.value;
			motionClass.value = `${mn} ${mn}-enter ${mn}-appear ${mn}-appear-prepare ${mn}-enter-prepare`;
			nextTick(() => {
				rootRef.value?.getBoundingClientRect();
				motionClass.value = `${mn} ${mn}-enter ${mn}-appear ${mn}-appear-active ${mn}-enter-active`;
				const el = rootRef.value;
				if (!el) return;
				const onEnd = () => {
					motionClass.value = "";
					el.removeEventListener("transitionend", onEnd);
					el.removeEventListener("animationend", onEnd);
				};
				el.addEventListener("transitionend", onEnd);
				el.addEventListener("animationend", onEnd);
			});
		});
		expose({ get nativeElement() {
			return rootRef.value;
		} });
		const domAttrs = computed(() => {
			const { class: _class, style: _style, ...rest } = attrs;
			return rest;
		});
		const mergedClasses = computed(() => {
			return {
				...contextConfig.value.classes,
				...props.classes
			};
		});
		const mergedStyles = computed(() => {
			return {
				...contextConfig.value.styles,
				...props.styles
			};
		});
		const triggerItemClick = (item, nested) => {
			if (item.disabled || !nested && hasChildren(item)) return;
			emit("itemClick", { data: item });
		};
		const renderTitle = () => {
			if (slots.title) return slots.title({ originNode: props.title });
			return props.title;
		};
		const renderItemNode = (slotName, item, index, nested, originNode) => {
			const slot = slots[slotName];
			if (slot) return slot({
				item,
				originNode,
				index,
				nested
			});
			return originNode;
		};
		const renderItems = (items, nested = false) => {
			if (!items?.length) return null;
			return createVNode("div", {
				"class": [
					`${props.prefixCls}-list`,
					nested ? mergedClasses.value.subList : mergedClasses.value.list,
					{
						[`${props.prefixCls}-list-wrap`]: props.wrap && !nested,
						[`${props.prefixCls}-list-vertical`]: nested || props.vertical
					}
				],
				"style": nested ? mergedStyles.value.subList : mergedStyles.value.list
			}, [items.map((item, index) => {
				const itemHasChildren = hasChildren(item);
				const iconNode = renderItemNode("iconRender", item, index, nested, item.icon);
				const labelNode = renderItemNode("labelRender", item, index, nested, item.label);
				const descriptionNode = renderItemNode("description", item, index, nested, item.description);
				return createVNode("div", mergeProps(getItemDomAttrs(item), {
					"key": item.key ?? `key-${index}`,
					"class": [
						`${props.prefixCls}-item`,
						nested ? mergedClasses.value.subItem : mergedClasses.value.item,
						item.class,
						{
							[`${props.prefixCls}-item-disabled`]: item.disabled,
							[`${props.prefixCls}-item-has-nest`]: itemHasChildren
						}
					],
					"style": [nested ? mergedStyles.value.subItem : mergedStyles.value.item, item.style],
					"onClick": () => triggerItemClick(item, nested)
				}), [hasRenderableNode(iconNode) && createVNode("div", { "class": `${props.prefixCls}-icon` }, [iconNode]), createVNode("div", {
					"class": [`${props.prefixCls}-content`, mergedClasses.value.itemContent],
					"style": mergedStyles.value.itemContent
				}, [
					hasRenderableNode(labelNode) && createVNode("div", { "class": `${props.prefixCls}-label` }, [labelNode]),
					hasRenderableNode(descriptionNode) && createVNode("div", { "class": `${props.prefixCls}-desc` }, [descriptionNode]),
					itemHasChildren && createVNode("div", { "class": `${props.prefixCls}-nested` }, [renderItems(item.children, true)])
				])]);
			})]);
		};
		return () => createVNode("div", mergeProps({ "ref": rootRef }, domAttrs.value, {
			"class": [
				props.prefixCls,
				contextConfig.value.classes?.root,
				props.rootClass,
				props.classes?.root,
				hashId.value,
				cssVarCls.value,
				attrs.class,
				props.class,
				motionClass.value,
				{ [`${props.prefixCls}-rtl`]: configCtx.value.direction === "rtl" }
			],
			"style": [
				contextConfig.value.style,
				contextConfig.value.styles?.root,
				props.styles?.root,
				attrs.style,
				props.style
			]
		}), [hasRenderableNode(renderTitle()) && createVNode("div", {
			"class": [`${props.prefixCls}-title`, mergedClasses.value.title],
			"style": mergedStyles.value.title
		}, [renderTitle()]), renderItems(props.items)]);
	}
});
//#endregion
export { XPrompts, XPrompts as default };
