import { ref } from 'vue';
import { defineStore } from 'pinia';
import Api from '@/api';
import { store } from '@/store';
import { DictEnum } from '@/enums/dictEnum';
const needCachedKey = [
  DictEnum.ContractType, 
  DictEnum.Unit,
  DictEnum.Vehicle
];

export const useDictStore = defineStore('dict', () => {
  const dictTypes = ref<API.DictTypeDto[]>([]);
  const getDictTypes = async () => {
    dictTypes.value = await Api.systemDictType.dictTypeGetAll({
      storeCodes: needCachedKey,
      withItems: true,
    });
  };
  getDictTypes();
  const getDictItemsByCode = (code: DictEnum): API.DictItemEntity[] => {
    return dictTypes.value.find((item) => item.code === code)?.dictItems || [];
  };

  return { dictTypes, getDictTypes, getDictItemsByCode };
});

// 在组件setup函数外使用
export function useDictStoreWithOut() {
  return useDictStore(store);
}
