import { Fragment, computed, createVNode, defineComponent } from "vue";
import { DirectoryTree } from "antdv-next";
import { FileOutlined, FolderOutlined } from "@antdv-next/icons";
//#region components/folder/DirectoryTree.tsx
function isFolder(node) {
	return !!node.children && node.children.length > 0;
}
function buildPathSegments(node, parentSegments) {
	if (node.path === "/" && parentSegments.length === 0) return ["/"];
	return [...parentSegments, node.path].filter((seg) => seg !== "");
}
function getIcon(node, directoryIcons) {
	if (directoryIcons === false || directoryIcons === null) return null;
	if (isFolder(node)) {
		const icon = directoryIcons?.directory;
		if (typeof icon === "function") return icon();
		return icon ?? createVNode(FolderOutlined, null, null);
	}
	const ext = node.path.toLowerCase().split(".").pop();
	if (ext) {
		const icon = directoryIcons?.[ext];
		if (icon) return typeof icon === "function" ? icon() : icon;
	}
	return createVNode(FileOutlined, null, null);
}
function convertToTreeData(nodes, directoryIcons, parentSegments = []) {
	return nodes.map((node) => {
		const pathSegments = buildPathSegments(node, parentSegments);
		const fullPath = pathSegments.join("/").replace(/^\/+/, "");
		return {
			key: fullPath,
			path: fullPath,
			pathSegments,
			title: node.title,
			icon: getIcon(node, directoryIcons),
			isLeaf: !isFolder(node),
			content: node.content,
			children: node.children ? convertToTreeData(node.children, directoryIcons, pathSegments) : void 0
		};
	});
}
var FolderDirectoryTree = /* @__PURE__ */ defineComponent({
	name: "AxFolderDirectoryTree",
	props: {
		prefixCls: {
			type: String,
			required: true
		},
		treeData: {
			type: Array,
			default: () => []
		},
		directoryIcons: {
			type: [Boolean, Object],
			default: void 0
		},
		selectedKeys: {
			type: Array,
			default: () => []
		},
		expandedKeys: {
			type: Array,
			default: void 0
		},
		defaultExpandAll: {
			type: Boolean,
			default: true
		},
		showLine: {
			type: Boolean,
			default: false
		},
		classes: {
			type: Object,
			default: () => ({})
		},
		styles: {
			type: Object,
			default: () => ({})
		},
		directoryTitle: {
			type: [
				Boolean,
				String,
				Number,
				Object,
				Array,
				Function
			],
			default: void 0
		}
	},
	emits: {
		select: (_keys, _info) => true,
		expand: (_keys, _info) => true
	},
	setup(props, { emit }) {
		const treeDataConverted = computed(() => convertToTreeData(props.treeData, props.directoryIcons));
		const titleNode = computed(() => {
			const t = props.directoryTitle;
			if (t === false || t === null || t === void 0) return null;
			if (typeof t === "function") return t();
			return t;
		});
		return () => {
			const { prefixCls } = props;
			return createVNode(Fragment, null, [titleNode.value ? createVNode("div", {
				"class": [`${prefixCls}-directory-tree-title`, props.classes?.directoryTitle],
				"style": props.styles?.directoryTitle
			}, [titleNode.value]) : null, createVNode(DirectoryTree, {
				treeData: treeDataConverted.value,
				selectedKeys: props.selectedKeys,
				expandedKeys: props.expandedKeys,
				multiple: false,
				blockNode: true,
				showLine: props.showLine,
				defaultExpandAll: props.defaultExpandAll,
				class: [`${prefixCls}-directory-tree-content`, props.classes?.directoryTree],
				style: props.styles?.directoryTree,
				classes: { itemTitle: `${prefixCls}-directory-tree-item-title` },
				onSelect: (keys, info) => emit("select", keys, info),
				onExpand: (keys, info) => emit("expand", keys, info)
			}, null)]);
		};
	}
});
//#endregion
export { FolderDirectoryTree as default };
