import { computed, onMounted } from 'vue'
import { useBusinessStore } from '@/store/modules/business'

/**
 * 字典项 hook
 * @param key 字典项 key
 * @param valueType 字典项 value 类型，默认 number
 */
export function useDict(key: string, valueType: 'string' | 'number' = 'number') {
  const store = useBusinessStore()

  const data = computed(() => store.dictData[key] || [])
  const loading = computed(() => store.loadingMap[key] || false)
  const options = computed(() => {
    if (data.value && data.value.length > 0) {
      return data.value.map((item: any) => {
        const rawValue = item.uI_Value || item.UI_Value || item.DicValue
        return {
          label: item.uI_Key || item.UI_Key || item.DicKey,
          value: valueType === 'number' ? Number(rawValue) : String(rawValue),
        }
      })
    }
    return []
  })

  onMounted(() => {
    store.getDict(key)
  })

  return {
    data,
    options,
    loading,
  }
}
