import initCollapseTransition from "../transition.js";
import { computed, ref, watch } from "vue";
//#region components/_utils/hooks/useCollapsible.ts
var baseConfig = {
	expandedKeys: [],
	defaultExpandedKeys: [],
	onExpand: () => {}
};
function useCollapsible(collapsible, rootPrefixCls) {
	const isUncontrolled = computed(() => {
		return typeof collapsible.value === "boolean" || collapsible.value?.expandedKeys === void 0;
	});
	const mergedConfig = computed(() => {
		if (!collapsible.value) return {
			enableCollapse: false,
			...baseConfig
		};
		if (typeof collapsible.value === "object") return {
			enableCollapse: true,
			...baseConfig,
			...collapsible.value
		};
		return {
			enableCollapse: true,
			...baseConfig
		};
	});
	const innerExpandedKeys = ref([]);
	watch([() => mergedConfig.value.defaultExpandedKeys, isUncontrolled], ([defaultExpandedKeys, uncontrolled]) => {
		if (uncontrolled) innerExpandedKeys.value = [...defaultExpandedKeys ?? []];
	}, { immediate: true });
	const expandedKeys = computed(() => {
		if (isUncontrolled.value) return innerExpandedKeys.value;
		return mergedConfig.value.expandedKeys ?? [];
	});
	const onItemExpand = (curKey) => {
		const currentKeys = expandedKeys.value;
		const targetKeys = currentKeys.includes(curKey) ? currentKeys.filter((key) => key !== curKey) : [...currentKeys, curKey];
		if (isUncontrolled.value) innerExpandedKeys.value = targetKeys;
		mergedConfig.value.onExpand?.(targetKeys);
		return targetKeys;
	};
	return {
		enableCollapse: computed(() => mergedConfig.value.enableCollapse),
		expandedKeys,
		onItemExpand: computed(() => mergedConfig.value.enableCollapse ? onItemExpand : void 0),
		collapseTransition: computed(() => {
			if (!mergedConfig.value.enableCollapse) return void 0;
			return initCollapseTransition(rootPrefixCls?.value);
		})
	};
}
//#endregion
export { useCollapsible as default };
