import { sendMessage } from 'webext-bridge/content-script';
import {
  EVENT_GET_USER_INFO,
  EVENT_OPEN_POPUP,
  EVENT_GET_LOCAL_SETTING,
  EVENT_GET_SCRIPT_URL,
  EVENT_SHOPEE_SAVE_PRODUCT,
  EVENT_SHOPEE_LISTEN_FETCH,
  EVENT_GET_AFFILIATE_PLATFORM,
  EVENT_SHOPEE_GET_AFFILIATE_CHAT,
  EVENT_SHOPEE_GET_PRODUCT,
  EVENT_SHOPEE_GET_HEADER_SHOPEE,
  EVENT_FETCH_API_AUTOSHOPEE,
  EVENT_TIKTOK_DOWNLOAD_VIDEO_CHECK,
  EVENT_TIKTOK_DOWNLOAD_VIDEO_INC,
  EVENT_VIDEO_BLOB_STORE,
  EVENT_VIDEO_BLOB_GET,
} from '@/bridge/config/event';
import { allSetting } from '@/plugins/setting/setting';

const listSetting = Object.keys(allSetting);

/**
 *
 * @returns {Promise<BridgeGetUserData>}
 */
export const bridgeGetUserInfo = async () => sendMessage(EVENT_GET_USER_INFO);

/**
 *
 * @param {string} scriptPath
 * @returns {Promise<string>}
 */
export const bridgeGetScriptUrl = async (scriptPath) => sendMessage(EVENT_GET_SCRIPT_URL, { scriptPath });

/**
 *
 * @param {BridgeOpenPopup} payload
 * @returns
 */
export const bridgeOpenPopupUrl = async (payload) => sendMessage(EVENT_OPEN_POPUP, payload);

/**
 *
 * @param {keyof allSetting} keySetting
 * @returns
 */
export const bridgeGetSetting = async (keySetting) => {
  if (!(keySetting && listSetting.includes(keySetting))) {
    throw {
      msg: 'SETTING_KEY_NOT_FOUND',
      key: keySetting,
    };
  }

  return sendMessage(EVENT_GET_LOCAL_SETTING, { key: keySetting });
};

/**
 *
 * @param {ShopPlatform} platform
 * @returns
 */
export const bridgeGetAffiliateUrl = async (platform) => sendMessage(EVENT_GET_AFFILIATE_PLATFORM, { platform });

/**
 *
 * @param {ShopeeProductData} product
 * @returns
 */
export const bridgeShopeeSaveProductId = async (product) => sendMessage(EVENT_SHOPEE_SAVE_PRODUCT, product);

/**
 *
 * @param {BridgeShopeeAffiliateChatMesage}payload
 * @returns {Promise<text>}
 */
export const bridgeShopeeGetAffiliateChatMesage = async (payload) => sendMessage(EVENT_SHOPEE_GET_AFFILIATE_CHAT, payload);

/**
 *
 * @param {BridgeShopeeListenerFetch}payload
 * @returns {Promise<text>}
 */
export const bridgeShopeeListenFetch = async (payload) => sendMessage(EVENT_SHOPEE_LISTEN_FETCH, payload);

/**
 *
 * @param {BridgeShopeeGetProduct}payload
 * @returns {Promise<ShopeeProductData>}
 */
export const bridgeShopeeGetProduct = async (payload) => sendMessage(EVENT_SHOPEE_GET_PRODUCT, payload);

/**
 *
 * @param {'product'|'search'} by
 * @returns {Promise<Array>}
 */
export const bridgeShopeeGetHeaderProduct = async (by) => sendMessage(EVENT_SHOPEE_GET_HEADER_SHOPEE, { by });

/**
 *
 * @param {BridgeFetchApiPayload} payload
 * @returns {Promise<object>}
 */
export const bridgeFetchApiAutoshopee = async (payload) => sendMessage(EVENT_FETCH_API_AUTOSHOPEE, payload);

/**
 *
 * @returns {Promise<{allow: boolean, reason?: string, used: number, dailyLimit: number}>}
 */
export const bridgeTiktokDownloadVideoCheck = async () => sendMessage(EVENT_TIKTOK_DOWNLOAD_VIDEO_CHECK);

/**
 *
 * @param {{awemeId: string, author: string}} payload
 * @returns {Promise<{ok: boolean}>}
 */
export const bridgeTiktokDownloadVideoInc = async (payload = {}) => sendMessage(EVENT_TIKTOK_DOWNLOAD_VIDEO_INC, payload);

/**
 *
 * @param {{base64: string, contentType: string}} payload
 * @returns {Promise<{blobId: string}>}
 */
export const bridgeVideoBlobStore = async (payload) => sendMessage(EVENT_VIDEO_BLOB_STORE, payload);

/**
 *
 * @param {string} blobId
 * @returns {Promise<{base64: string, contentType: string}|null>}
 */
export const bridgeVideoBlobGet = async (blobId) => sendMessage(EVENT_VIDEO_BLOB_GET, { blobId });
