import { shallowRef, ShallowRef } from 'vue'
import { queryAllEnums } from '@/services/api/common'
import { createStore } from '../base'
import { sessionData } from '../storage'

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

const enumKeys = ['clientType', 'scoreConfigType', 'accountBusinessCode', 'certificatetype', 'signstatus', 'thjOrderStatus', 'THJDeliveryMode', 'goodsunit', 'WROutInApplyStatus2', 'THJTransferStatus', 'WRTradeOrderStatus', 'THJMarket', 'THJProfitRoleType'] as const

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

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

/**
 * 枚举存储对象
 */
export const enumStore = createStore({
    state() {
        return {
            loading: false,
            allEnums: sessionData.getRef('allEnums'),
        }
    },
    actions: {
        // 获取所有枚举列表
        async getAllEnumList() {
            await new Promise<void>((resolve) => {
                if (this.state.allEnums.length) {
                    resolve()
                } else {
                    this.state.loading = true
                    queryAllEnums().then((res) => {
                        this.state.allEnums = res.data
                        resolve()
                    }).finally(() => {
                        this.state.loading = false
                    })
                }
            })
            // 清空列表数据
            for (const item of enumMap.values()) {
                item.value = []
            }
            this.state.allEnums.forEach((e) => {
                const mapKey = enumKeys.find((key) => key === e.enumdiccode)
                if (mapKey && e.enumitemstatus === 1) {
                    const enumRef = enumMap.get(mapKey)
                    enumRef?.value.push(e)
                }
            })
        },
        // 获取枚举信息
        getEnumTypeInfo(enumKey: typeof enumKeys[number], value: number) {
            const enums = enumMap.get(enumKey)
            return enums?.value.find((e) => e.enumitemname === value)
        },
        // 获取枚举列表
        getEnumTypeList: (enumKey: typeof enumKeys[number], propertys?: (keyof Model.EnumRsp)[]) => {
            const enums = enumMap.get(enumKey)
            if (enums) {
                return enums.value.map((e) => {
                    const props = propertys?.reduce((res, prop) => {
                        const value = e[prop]
                        if (value) res.push(value)
                        return res
                    }, [] as unknown[])
                    return {
                        label: props?.length ? props.join('-') : e.enumdicname,
                        value: e.enumitemname,
                    }
                })
            }
            return []
        },
        // 根据枚举值获取枚举名称
        getEnumTypeName(enums: EnumType[], value?: number) {
            const item = enums.find((e) => e.value === value)
            return item?.label ?? value?.toString() ?? '无效枚举'
        },
        // 根据枚举名称获取对应的值
        getEnumTypeValue(enums: EnumType[], label?: string) {
            const item = enums.find((e) => e.label === label)
            return item?.value ?? label
        }
    },
})