import { reactive, toRefs, onUnmounted } from 'vue'
import { Language } from '@/constants/language'
import { enumType } from '@/services/api/common'
import { i18n } from '@/stores'

export function useEnum(code: string) {
    const state = reactive({
        loading: true,
        enums: <Model.EnumTypeRsp[]>[]
    })

    const controller = new AbortController()
    const readyCallbackMap = new Map<symbol, () => void>()

    // 获取本地化属性键
    const getLocalizedKey: () => keyof Model.EnumTypeRsp = () => {
        switch (i18n.global.locale) {
            case Language.Simplified:
                return 'enumdicname'
            case Language.Thai:
                return 'enumdicnameth'
            case Language.Traditional:
                return 'enumdicnamezhtw'
            case Language.Vietnamese:
                return 'enumdicnamevi'
            default:
                return 'enumdicnameen'
        }
    }

    // 获取枚举名称
    const getEnumTypeName = (value?: number) => {
        const localizedKey = getLocalizedKey()
        const item = state.enums.find((e) => e.enumitemname === value)
        return item ? item[localizedKey].toString() : value?.toString()
    }

    // 获取枚举列表
    const getEnumOptions = (filtered: number | number[] = [], reverse = false) => {
        const localizedKey = getLocalizedKey()
        const valuesSet = new Set(Array.isArray(filtered) ? filtered : [filtered])

        if (valuesSet.size === 0) {
            return state.enums.map((e) => ({
                label: e[localizedKey].toString(),
                value: e.enumitemname
            }))
        }

        const result = state.enums.filter((e) => {
            if (reverse) {
                // 排除枚举
                return !valuesSet.has(e.enumitemname)
            } else {
                // 匹配枚举
                return valuesSet.has(e.enumitemname)
            }
        })

        return result.map((e) => ({
            label: e[localizedKey].toString(),
            value: e.enumitemname
        }))
    }

    // 注册成功回调
    const registerEnumReadyCallback = (callback: () => void) => {
        if (state.loading) {
            const key = Symbol()
            readyCallbackMap.set(key, callback)
        } else {
            callback()
        }
    }

    // 获取枚举列表
    enumType({
        controller,
        data: {
            typeCode: code
        }
    }).then((res) => {
        state.enums = res.data
        readyCallbackMap.forEach((callback) => callback())
        readyCallbackMap.clear()
    })

    onUnmounted(() => {
        controller.abort()
        readyCallbackMap.clear()
    })

    return {
        ...toRefs(state),
        getEnumOptions,
        getEnumTypeName,
        registerEnumReadyCallback
    }
}