import lodash from 'lodash';
import { apiShopeeSeller } from '@/api/shopee/seller/init';
import { CacheShopeeSellerConfig } from '@/plugins/store/cache';

const isCacheEnabled = true;

/**
 *
 * @returns {Promise<ShopeeWebchatLogin>}
 */
export const getShopeeWebchatLogin = async () => {
  const result = await apiShopeeSeller.post('/webchat/api/coreapi/v1.2/mini/login/sc');
  return result;
};

/**
 *
 * @param  {string} shopId
 * @returns {Promise<[]>}
 */
export const getShopeSellerLogisticList = async (shopId) => {
  const CacheLogistic = new CacheShopeeSellerConfig(['logistic', shopId].join('-'));

  if (isCacheEnabled) {
    const getCache = await CacheLogistic.get();

    if (getCache) {
      return getCache;
    }
  }

  const result = await apiShopeeSeller.get('/api/v3/logistics/get_channel_list/');

  const listChannel = lodash.get(result, 'data.list', []);
  await CacheLogistic.set(listChannel, 30);

  return listChannel;
};

/**
 *
 * @param  {string} shopId
 * @returns {Promise<ShopeeShopWarehouse[]>}
 */
export const getShopeSellerWarehouseList = async (shopId) => {
  const CacheWarehouse = new CacheShopeeSellerConfig(['warehouse', shopId].join('-'));

  if (isCacheEnabled) {
    const getCache = await CacheWarehouse.get();

    if (getCache) {
      return getCache;
    }
  }

  try {
    const result = await apiShopeeSeller.get('/api/v3/product/get_shop_warehouse/');

    const listWarehouse = lodash.get(result, 'data.warehouse_list', []);

    await CacheWarehouse.set(listWarehouse, 30);

    return listWarehouse;
  } catch (error) {
    return [];
  }
};
