import { invoke } from "@tauri-apps/api/tauri";
import { dialog, fs } from "@tauri-apps/api";

export default {
    /**
     * 打开文件夹选择对话框并获取文件夹内容
     * @param {string} defaultPath 默认路径
     * @returns {Promise<Object>} 文件夹树形结构
     */
    async getFolderList(defaultPath = "") {
        try {
            // 打开文件夹选择对话框
            const options = {
                defaultPath: defaultPath || "../",
                directory: true,
                title: "请选择文件夹",
            };

            const selectedPath = await dialog.open(options);

            if (!selectedPath) {
                return {
                    label: '请选择',
                    children: [],
                };
            }

            // 读取文件夹内容并构建树形结构
            const tree = await this.buildFolderTree(selectedPath);
            return tree;
        } catch (error) {
            console.error("获取文件夹列表失败:", error);
            return {
                label: '请选择',
                children: [],
            };
        }
    },

    /**
     * 构建文件夹树形结构
     * @param {string} folderPath 文件夹路径
     * @returns {Promise<Object>} 树形结构对象
     */
    async buildFolderTree(folderPath) {
        try {
            const entries = await fs.readDir(folderPath, { recursive: true });

            // 获取文件夹名称
            const pathParts = folderPath.split(/[\\\/]/);
            const folderName = pathParts[pathParts.length - 1] || folderPath;
            const obj = {
                name: folderName,
                path: folderPath,
                children: entries,
                isFolder: true,
                isLeaf: false
            }
            const data = this.getTreeData(obj);
            return data
        } catch (error) {
            console.error("构建文件夹树失败:", error);
            return {
                label: folderPath,
                children: [],
                path: folderPath,
                isFolder: true,
                isLeaf: false
            };
        }
    },


    getTreeData(entries) {
        if (!entries) {
            return null;
        }
        const obj = {
            label: entries.name,
            path: entries.path,
        }
        if (entries?.children && Array.isArray(entries.children)) {
            // 处理数组
            obj.children = entries.children.map((item) => {
                return this.getTreeData(item);
            });
            obj.isFolder = true;
            obj.isLeaf = false;

        } else {
            obj.isLeaf = true;
            obj.isFolder = false;
        }
        return obj;
    }


};


function deepTraverse(data) {
    if (Array.isArray(data)) {
        // 处理数组
        data.forEach((item, index) => {
            console.log(`数组项 ${index}:`, item);
            deepTraverse(item); // 递归处理数组元素
        });
    } else if (typeof data === 'object' && data !== null) {
        // 处理对象
        Object.entries(data).forEach(([key, value]) => {
            console.log(`属性 ${key}:`, value);
            deepTraverse(value); // 递归处理对象属性
        });
    } else {
        // 基本类型值
        console.log('值:', data);
    }
}