import request from '@/utils/http'

const QUERY_BASE_URL = '/api/admin/v1/dictionaries'

const normalizeCode = (code: string) => code.trim().toLowerCase()

export async function getDictionary(code: string) {
  const result = await batchGetDictionaries([code])
  return result[code] ?? []
}

export async function batchGetDictionaries(codes: string[]) {
  if (!codes.length) return {}

  const result = await request.post<Record<string, Api.Dictionary.DictionaryItemDto[]>>({
    url: `${QUERY_BASE_URL}/batch`,
    data: { codes }
  })

  const mapped: Record<string, Api.Dictionary.DictionaryItemDto[]> = {}
  codes.forEach((code) => {
    const key = normalizeCode(code)
    mapped[code] = result[key] ?? result[code] ?? []
  })

  return mapped
}
