import { zipToObject } from 'radash'
import { DictType } from '../core/libs/models'
import { getByDictType } from '../core/libs/requests'
import { defineStore } from 'pinia'

const initData = [DictType.circleSpaceType, DictType.memberDesignStyle]
export const useDictStore = defineStore('dict', () => {
  //   const options = ref({})
  const { data: options, run: fetchOptions } = useRequest(async () => ({
    data: zipToObject(
      initData,
      await Promise.all(initData.map((it) => getByDictType(it).then((it) => it.data))),
    ),
    msg: '',
    code: 0,
  }))
  const getOptionLabel = (type: DictType, value: string | number) =>
    options.value[type]?.find((it) => it.value === value)?.label
  //   const fetchOptions = async () => {
  //     options.value = await zipToObject(
  //       initData,
  //       await Promise.all(initData.map((it) => getByDictType(it).then((it) => it.data))),
  //     )
  //   }
  fetchOptions()
  return { options, getOptionLabel }
})
