import { apiTiktoklient } from '@/api/tiktok/client/init';
import { CacheTiktokProductId } from '@/plugins/store/cache';

/**
 *
 * @param {string } productId
 * @returns {Promise<TiktokPdpProductId>}
 */
export const getTiktokProductId = async (productId) => {
  const CacheStore = new CacheTiktokProductId(productId);
  const getCache = await CacheStore.get();

  if (getCache) {
    return getCache;
  }

  const result = await apiTiktoklient.get(`/pdp/${productId}?__loader=(shop$)/(pdp)/(name$)/(id)/page`);

  if (result) {
    await CacheStore.set(result);
  }

  return result;
};
