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

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

const enumKeys = ['ZSCategory', 'ZSCurrencyType', 'ZSCurrencyType', 'ZSColorType', 'ZSClarityType', 'ZSCutType', 'ZSShapeType', 'ZSSymmetryType', 'ZSPolishType', 'ZSFluorescenceType', 'ZSCertType', 'ZSCrystalType', 'ZSCZColor1Type', 'ZSCZColor2Type', 'ZSCZColor3Type', 'ZSStyleType', 'signstatus', 'applystatus', 'executetype', 'certificatetype', 'clientType', 'wrApplyStatus', 'performanceStatus', 'stepStatus', 'GZCJAccountType', 'GZCJCategoryType', 'GZCJDeliveryType', 'GZCJShapeType', 'GZCJMarkType', 'GZCJPublishType', 'GZCJServiceType', 'GZCJStatus', 'GZBSStatus', 'GZBSDeliveryType', 'GZBSCOrderType', 'GZBSCOrderStatus', 'GZBSCOutType', 'GZBSCPayStatus', 'GZBSCPayMode', 'YSZSCategory', 'YSProductionMode', 'WRPresaleStatus', 'inoutapplystatus', 'WRTradeOrderStatus', 'accountBusinessCode'] as const

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

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

/**
 * 枚举存储对象
 */
export const useEnumStore = defineStore(() => {
    const loading = shallowRef(false)
    const allEnums = sessionData.getRef('allEnums')

    // 获取所有枚举列表
    const getAllEnumList = async () => {
        if (!allEnums.value.length) {
            loading.value = true
            await queryAllEnums().then((res) => {
                allEnums.value = res.data
            }).finally(() => {
                loading.value = false
            })
        }

        // 清空列表数据
        for (const item of enumMap.values()) {
            item.value = []
        }

        allEnums.value.forEach((e) => {
            const mapKey = enumKeys.find((key) => key === e.enumdiccode)
            if (mapKey && e.enumitemstatus === 1) {
                const enumRef = enumMap.get(mapKey)
                enumRef?.value.push(e)
            }
        })
    }

    // 获取枚举信息
    const getEnumTypeInfo = (enumKey: typeof enumKeys[number], value: number) => {
        const enums = enumMap.get(enumKey)
        return enums?.value.find((e) => e.enumitemname === value)
    }

    // 获取枚举列表
    const getEnumTypeList = (enumKey: typeof enumKeys[number], propertys?: (keyof Ermcp.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 []
    }

    // 根据枚举值获取枚举名称
    const getEnumTypeName = (enums: EnumType[], value?: number) => {
        const item = enums.find((e) => e.value === value)
        return item?.label ?? value?.toString() ?? '无效枚举'
    }

    // 根据枚举名称获取对应的值
    const getEnumTypeValue = (enums: EnumType[], label?: string) => {
        const item = enums.find((e) => e.label === label)
        return item?.value ?? label
    }

    return {
        loading,
        allEnums,
        enumMap,
        getAllEnumList,
        getEnumTypeInfo,
        getEnumTypeList,
        getEnumTypeName,
        getEnumTypeValue
    }
})