import { getProductCategoryTree } from '@/api/home/index';
import { getGiftCategoryList } from '@/api/home/index-fuli';

export const categoryStore = defineStore('category', () => {
  const categories = ref<any>({
    '/index': [],
    '/indexMro': [],
    '/indexFuli': []
  });

  const fetchCategories = async (key: string) => {
    if (categories.value[key].length > 0) return categories.value[key];
    try {
      const response =
        key == '/index'
          ? await getProductCategoryTree({ platform: 0 })
          : key == '/indexMro'
            ? await getProductCategoryTree({})
            : await getGiftCategoryList({});
      categories.value[key] = response.data;
      return response.data;
    } catch (error) {
      console.error('获取分类失败:', error);
      throw error;
    }
  };

  return {
    categories,
    fetchCategories
  };
});
