import listCategoryHome from '@/data/tiktok/category/tree.json';
import listCategoryList from '@/data/tiktok/category/list.json';
import listCategorySeler from '@/data/tiktok/category/seller.json';
import { apiTiktoklient } from '@/api/tiktok/client/init';
import { CacheTiktokProductSearch } from '@/plugins/store/cache';

export const getTiktokCategoryHome = () => listCategoryHome;
export const getTiktokCategoryList = () => listCategoryList;
export const getTiktokCategorySeller = () => listCategorySeler;

/**
 *
 * @param {string} categoryId
 * @param {string[]}productIdSkip
 */
export const tiktokSearchListProductCategoryId = async (categoryId, productIdSkip = []) => {
  const CacheStore = new CacheTiktokProductSearch(
    ['category', categoryId, ...productIdSkip].join('-'),
  );

  const getCache = await CacheStore.get();

  if (getCache) {
    return getCache;
  }

  const result = await apiTiktoklient.post(
    '/api/shop/home/product_list',
    {
      category_id: Number(categoryId),
      exclude_product_ids: productIdSkip,
    },
  );

  /** @type {TiktokProductList[]} */
  const listProduct = result.data ? result.data.productList : [];

  const totalProduct = 1000;

  const returnSearch = {
    listProduct,
    totalProduct,
  };

  if (listProduct.length) {
    await CacheStore.set(returnSearch);
  }

  return returnSearch;
};
