import { apiShopeeSeller } from '@/api/shopee/seller/init';
import { getShopeeSubAccountInfo } from './subaccount';
import { getShopeeProfileClient } from '@/api/shopee/client/shop';

/**
 *
 * @returns {Promise<ShopeeSellerInfo>}
 */
export const getShopeeSellerInfo = async () => {
  try {
    const sellerInfo = await apiShopeeSeller.get('/api/forward/accountservice/v2/shop/');
    return sellerInfo.shop_id ? sellerInfo : null;
  } catch (error) {
    return null;
  }
};

/**
 *
 * @returns {Promise<ShopeeSellerAccountInfo>}
 */
export const getShopeeSellerAccountInfo = async () => {
  try {
    const accountInfo = await apiShopeeSeller.get('/api/selleraccount/shop_info/');
    return accountInfo.data && accountInfo.data.shop_id ? accountInfo.data : null;
  } catch (error) {
    return null;
  }
};

/**
 *
 * @returns {Promise<ShopeeSellerFrameworkInfo>}
 */
export const getShopeeSellerFrameworkInfo = async () => {
  try {
    const frameworkInfo = await apiShopeeSeller.get('/api/framework/selleraccount/shop_info/');
    return frameworkInfo.data && frameworkInfo.data.shop_id ? frameworkInfo.data : null;
  } catch (error) {
    return null;
  }
};

/**
 *
 * @param SPC_CDS
 * @returns {Promise<ShopeeSellerLogin>}
 */
export const getShopeeSellerLogin = async (SPC_CDS = '') => {
  try {
    const params = {};

    if (SPC_CDS) {
      params.SPC_CDS = SPC_CDS;
    }

    const sellerLogin = await apiShopeeSeller.get('/api/v2/login/', {
      params,
    });

    return sellerLogin;
  } catch (error) {
    return null;
  }
};

/**
 *
 * @returns {Promise<{shopId: number, username: string, shopName: string, isPrimary: boolean, isSubAccount: boolean}>}
 */
export const getShopeeSellerFallbackInfo = async () => {
  if (typeof window !== 'undefined' && window?.location?.href?.includes('shopee.vn')) {
    const profileClient = await getShopeeProfileClient();

    if (profileClient && profileClient.shopid) {
      return {
        shopId: profileClient.shopid,
        username: profileClient.username,
        shopName: profileClient.username,
        isPrimary: true,
        isSubAccount: false,
      };
    }
  }

  const shopInfo = await getShopeeSellerInfo();

  if (shopInfo && shopInfo.shop_id) {
    return {
      shopId: shopInfo.shop_id,
      username: shopInfo.username,
      shopName: shopInfo.shop_name,
      isPrimary: true,
      isSubAccount: false,
    };
  }

  const loginInfo = await getShopeeSellerLogin();

  if (loginInfo && loginInfo.shopid) {
    return {
      shopId: loginInfo.shopid,
      username: loginInfo.username,
      shopName: loginInfo.username,
      isPrimary: false,
      isSubAccount: false,
    };
  }

  const accountInfo = await getShopeeSellerAccountInfo();

  if (accountInfo && accountInfo.shop_id) {
    return {
      shopId: accountInfo.shop_id,
      username: accountInfo.shop_id.toString(),
      shopName: accountInfo.name,
      isPrimary: false,
      isSubAccount: true,
    };
  }

  const frameworkInfo = await getShopeeSellerFrameworkInfo();

  if (frameworkInfo && frameworkInfo.shop_id) {
    return {
      shopId: frameworkInfo.shop_id,
      username: frameworkInfo.shop_id.toString(),
      shopName: frameworkInfo.name,
      isPrimary: false,
      isSubAccount: true,
    };
  }

  const subAccountInfo = await getShopeeSubAccountInfo();

  if (subAccountInfo && subAccountInfo.sub_account_id) {
    return {
      shopId: subAccountInfo.sub_account_id,
      username: subAccountInfo.username,
      shopName: subAccountInfo.shop_name,
      isPrimary: false,
      isSubAccount: true,
    };
  }

  throw new Error('Get seller info failed');
};

/**
 *
 * @returns {Promise<ShopeeShopSetting>}
 */
export const getShopeeSellerConfig = async () => {
  try {
    const result = await apiShopeeSeller.get('/api/sellermisc/sc_conf/get_shop_settings/');

    return result.data ? result.data : null;
  } catch (error) {
    return null;
  }
};
