// 行业类别
import { defHttp } from '/@/utils/http/axios';
import { defineStore } from 'pinia'
// 获取行业类别接口并存起来
const categoryList = () => {
    return defHttp.get({
        url: '/sys/api/tree/hylb'
    });
};
// 获取行业类别的数据
let list = []
async function getCategoryList() {
    const res = await categoryList()
    list = transformArray(res)
}
getCategoryList()

function transformArray(arr) {
    return arr.map(node => {
        const newNode = {};
        for (const key in node) {
            if (key === 'name') {
                newNode['title'] = node[key];

            } else if (key === 'id') {
                newNode['key'] = node[key];
            } else if (Array.isArray(node[key])) {
                newNode[key] = transformArray(node[key]);
            } else {
                newNode[key] = node[key];
            }
        }
        return newNode;
    });
}

interface Category {
    category: any,
}

export const categoryStore = defineStore('category', {
    state: (): Category => ({
        category: list || [],
    }),
    getters: {
        getCategory(): any {
            return this.category
        }
    },
    actions: {
        setCategory(text: any) {
            this.category = text
        }
    }
})
