import { store } from "@/store";
import { defineStore } from "pinia";
import { getUserDicts } from "@/api/auth";

export const useDictionaryStore = defineStore("dictionaryStore", () => {
  const types = ["unqualified_type", "stage"];
  const dicts = ref<{ string?: any[] }>({});

  function checkDicts() {
    if (JSON.stringify(dicts.value) === "{}") {
      getUserDicts(types).then((res) => {
        if (res.data) {
          dicts.value = res?.data ?? [];
        }
      });
    }
  }

  function getLableByValue(type: string, value: string) {
    const dict = dicts.value[type];
    if (dict) {
      const lable = dict.find((item: string) => item.dictValue === value);
      return lable?.dictLabel;
    }
    return "";
  }

  return {
    types,
    dicts,
    checkDicts,
    getLableByValue,
  };
});

export function useDictionaryStoreHook() {
  // console.log('dicts:',useDictionaryStore(store))
  return useDictionaryStore(store);
}
