import { zipToObject } from 'radash'
import { getByDictType } from '@/core/libs/requests'
import { defineStore } from 'pinia'

const initData = ['member_channel_type']
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: string, 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().then()
  return { options, getOptionLabel }
})
