import { toRefs, shallowRef, shallowReadonly, ShallowRef } from 'vue'
import { queryAllEnums } from '@/services/api/common'
import { VueStore } from '../base'
import WebStorage from '@/utils/storage/base'

/**
 * 枚举类型
 */
export interface EnumType {
    label: string;
    value: number;
    disabled?: boolean;
}

interface StoreState {
    loading: boolean;
    allEnums: ShallowRef<Model.EnumRsp[]>;
}
const enumKeys = ['clientType', 'scoreConfigType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'thjDeliveryMode', 'goodsunit'] as const

/**
 * 枚举存储类
 */
const store = new (class extends VueStore<StoreState>{
    constructor() {
        const storage = new WebStorage<Model.EnumRsp[]>(sessionStorage, 'allEnums', [])
        const state: StoreState = {
            loading: false,
            allEnums: storage.getRef(),
        }
        super(state)

        // 初始化枚举列表
        for (const key of enumKeys) {
            this.enumMap.set(key, shallowRef<Model.EnumRsp[]>([]))
        }
    }

    private enumMap = new Map<typeof enumKeys[number], ShallowRef<Model.EnumRsp[]>>()

    private setEnumMap = () => {
        // 清空列表数据
        for (const item of this.enumMap.values()) {
            item.value = []
        }
        this.state.allEnums.forEach((e) => {
            const mapKey = enumKeys.find((key) => key === e.enumdiccode)
            if (mapKey && e.enumitemstatus === 1) {
                const enumRef = this.enumMap.get(mapKey)
                enumRef?.value.push(e)
            }
        })
    }

    actions = {
        /** 获取所有枚举列表 */
        getAllEnumList: () => {
            if (this.state.allEnums.length) {
                this.setEnumMap()
                return Promise.resolve()
            }
            this.state.loading = true
            return queryAllEnums({
                success: (res) => {
                    this.state.allEnums = res.data
                    this.setEnumMap()
                },
                complete: () => {
                    this.state.loading = false
                }
            })
        },
        /** 获取枚举信息 */
        getEnumTypeInfo: (enumKey: typeof enumKeys[number], value: number) => {
            const enums = this.enumMap.get(enumKey)
            return enums?.value.find((e) => e.enumitemname === value)
        },
        /** 获取枚举列表 */
        getEnumTypeList: (enumKey: typeof enumKeys[number]) => {
            const enums = this.enumMap.get(enumKey)
            if (enums) {
                return enums.value.map((e) => ({
                    label: e.enumdicname,
                    value: e.enumitemname,
                }))
            }
            return []
        },
        /** 根据枚举值获取枚举名称 */
        getEnumTypeName: (enums: EnumType[], value?: number) => {
            const item = enums.find((e) => e.value === value)
            return item?.label ?? value
        },
        /** 根据枚举名称获取对应的值 */
        getEnumTypeValue: (enums: EnumType[], label?: string) => {
            const item = enums.find((e) => e.label === label)
            return item?.value ?? label
        }
    }
})

export function useEnumStore() {
    return shallowReadonly({
        ...toRefs(store.state),
        ...store.actions,
        ...store.methods,
    })
}