import {UserVO} from "@/api/system/user/types";
import {getPersonalize, savePersonalize, getPersonalizeByCode} from "@/api/system/user/index";
import { columnTypes } from "@/components/Table/types";

export const userHobit = defineStore('userHobit', () => {
    const cloumnListMap = reactive<Map<string, string[]>>(new Map());
    const setAllTableCloumn = (data) => {

         Object.keys(data).forEach(key => {
            cloumnListMap.set(key, data[key] ? JSON.parse(data[key]) : [])
        });
    }

    const gethobit = async () => {
        // const res = await getPersonalize()
        // setAllTableCloumn(res.result)
    }

    const getCloumnByCode = (code: string) => {

        return cloumnListMap.get(code)
    }
    const saveCloumn = (code: string, cloumn: columnTypes[]) => {
        if (!code || !cloumn || cloumn.length === 0) return cloumn
        //只获取show为true的cloumn的prop
        let disabledMoveColumnList = cloumn.filter(item => item.show && item.disabledMove).map(item => item.prop)
        let columnList = cloumn.filter(item => item.show && !item.disabledMove).map(item => item.prop)
        cloumnListMap.set(code, disabledMoveColumnList.concat(columnList))
        let columnJson = JSON.stringify(columnList)

        savePersonalize({ code, columnJson })
    }

    const sortCloumn = async (code: string, preCloumn: columnTypes[]) => {
        let cloumn = preCloumn.filter(item => !item.disabledMove )
        let noSortColumn = preCloumn.filter(item => item.disabledMove && item.label != '操作')
        let operationCloumn = preCloumn.filter(item => item.disabledMove && item.label == '操作')
        if(!code) return noSortColumn.concat(cloumn).concat(operationCloumn)
        //根据cloumn中的prop 按照cloumnListMap
        const sortList = cloumnListMap.get(code)
        if (sortList) {
            // 排序函数
            cloumn.sort((a, b) => {
                const indexA = sortList.indexOf(a.prop);
                const indexB = sortList.indexOf(b.prop);

                // 如果 a 或 b 不在 baseOrder 数组中，默认排在后面
                if (indexA === -1) return 1;
                if (indexB === -1) return -1;

                // 按 baseOrder 数组的索引排序
                return indexA - indexB;
            });
            //将cloumn的show设置为true
            cloumn.forEach(item => {
                item.show = sortList.includes(item.prop) || item.label === '操作' || item.disabledMove
            })
            preCloumn = noSortColumn.concat(cloumn).concat(operationCloumn)
        } else {
            const response = await getPersonalizeByCode(code)
            const sortListRes = response.result
            //判断sortListRes是否是空对象
            if(Object.keys(sortListRes).length === 0) {
                return preCloumn =  noSortColumn.concat(cloumn).concat(operationCloumn)
            }
            let sortList =  sortListRes[code] ? JSON.parse(sortListRes[code]) : []
            cloumnListMap.set(code, sortList)
            if(sortList && sortList.length > 0) {
                // 排序函数
                cloumn.sort((a, b) => {
                    const indexA = sortList.indexOf(a.prop);
                    const indexB = sortList.indexOf(b.prop);

                    // 如果 a 或 b 不在 baseOrder 数组中，默认排在后面
                    if (indexA === -1) return 1;
                    if (indexB === -1) return -1;

                    // 按 baseOrder 数组的索引排序
                    return indexA - indexB;
                });

            }

            preCloumn =  noSortColumn.concat(cloumn).concat(operationCloumn)
            //将cloumn的show设置为true
            preCloumn.forEach(item => {
                item.show = sortList.includes(item.prop) || item.label === '操作' || item.disabledMove
            })

        }
        return preCloumn
        // return cloumn.concat(noSortColumn)
    }
    return {
        gethobit,
        cloumnListMap,
        setAllTableCloumn,
        getCloumnByCode,
        sortCloumn,
        saveCloumn
        // state,
        // setUser,
        // getUser
    };
}, {
    persist: {
        key: 'userHobit',
        storage: sessionStorage,
        paths: ['cloumnListMap']
    } as any
});

export default userHobit;
