import { create } from 'zustand'
import { getMapList } from '@/api/base'

interface State {
  mapList: MapState[]
  setMap: (data: MapState[]) => void
  getMap: () => Promise<void>
  getMapValuesByKey: (key: string) => MapValues[]
  getNameByValue: (value: string, list: MapValues[]) => string
}
interface MapState {
  id: string
  key: string
  name: string
  remark: string
  values: MapValues[]
}

interface MapValues {
  data_dictionary_id: string
  id: string
  label: string
  remark: string
  sort: string
  value: string
}
export const useMapStore = create<State>((set, get) => ({
  mapList: [],
  setMap: (data: MapState[]) => {
    set({ mapList: data })
  },
  getMap: async () => {
    const { data } = await getMapList()
    set({ mapList: data })
  },
  getMapValuesByKey: (key) => {
    return get().mapList.find((map) => map.key === key)?.values || []
  },
  getNameByValue: (value, list) => {
    return list.find((item) => item.value == value)?.label || value
  },
}))
