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

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

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

  if (false) {
    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, 60);

  return listChannel;
};

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

  if (false) {
    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, 60);

    return listWarehouse;
  } catch (error) {
    await CacheWarehouse.set([]);
    return [];
  }
};
