import lodash from 'lodash';
import queryString from 'query-string';
import { configShopeeApi, SHOPEE_DOMAIN_HOME, SHOPEE_DOMAIN_SELLER } from '@/config/api';

export const fixPrice = (price) => {
  if (!price) {
    return 0;
  }
  return Number(price.toString().replace(/\.00$/, ''));
};

export const imageId = (url) => {
  if (!url) {
    return '';
  }

  return url.replace(`${configShopeeApi.image}/`, '').replace('_tn', '');
};

export const imageUrl = (id, isZip = false) => {
  if (!id) {
    return '';
  }

  if (id.includes(configShopeeApi.image)) {
    return id;
  }
  return [`${configShopeeApi.image}/${id}`, isZip ? '_tn' : ''].join('');
};

export const videoUrl = (id) => {
  return `${configShopeeApi.video}/${id}`;
};

export const productSeller = (id) => {
  return `${SHOPEE_DOMAIN_SELLER}/portal/product/${id}`;
};

export const flashSaleSeller = (id) => {
  return `${SHOPEE_DOMAIN_SELLER}/portal/marketing/shop-flash-sale/${id}`;
};

export const productUrl = (productId, shopId) => {
  return `${SHOPEE_DOMAIN_HOME}/product/${shopId}/${productId}`;
};

export const shopUrlName = (username) => {
  return `${SHOPEE_DOMAIN_HOME}/${username}}`;
};

export const shopUrlId = (shopId) => {
  return `${SHOPEE_DOMAIN_HOME}/shop/${shopId}`;
};

export const searchByKeyword = (keyword) => {
  return queryString.stringifyUrl({
    url: `${SHOPEE_DOMAIN_HOME}/search`,
    query: {
      keyword,
    },
  });
};

export const videoData = (videoInfo) => {
  const id = lodash.get(videoInfo, '[0].video_id', '');

  return {
    id,
    version: lodash.get(videoInfo, '[0].version', ''),
    urlRaw: id ? `${configShopeeApi.video}/${id}` : '',
    listUrl: (lodash.get(videoInfo, '[0].formats', []) || []).map((item) => `${configShopeeApi.videoPlay}/${item.path}`),
    urlDefault: lodash.get(videoInfo, '[0].default_format.url', ''),
  };
};

export const sellerLoginUrl = `${SHOPEE_DOMAIN_SELLER}/account/signin`;
export const buyerLoginUrl = `${SHOPEE_DOMAIN_HOME}/buyer/login`;
