import { getDict } from '@/hooks/web/useDictionaries'

const query = {
  areaList: { dictName: 'menduner_area_type', params: {}, itemText: 'name', itemValue: 'id' },
  positionData: { dictName: 'positionData', params: {}, itemText: 'nameCn', itemValue: 'id' },
}

export const getText = (value, arr, itemText = 'label', itemValue = 'value') => { // 一维数组
  if (!arr?.length || !(value && value !== 0)) return
  const item = arr.find(formItem => formItem[itemValue] === value)
  if (!item) return
  return item[itemText]
}

export const getNameIdText = (value, arr) => {
  getText(value, arr, 'name', 'id')
}

export const getDictText = async (value, type) => {
  if (!type) return
  if (!query[type]) return
  const { data } = await getDict(query[type].dictName, query[type].params, type)
  const itemText = query[type].itemText || 'label'
  const itemValue = query[type].itemValue || 'value'
  getText(value, data, itemText, itemValue)
}

export const getDictOptions = async (type) => {
  if (!type) return
  if (!query[type]) return
  const { data } = await getDict(query[type].dictName, query[type].params, type)
  return data || []
}

