import { notifications, sendContentMessage } from "../../utils/chrome";
import { FrMesagesKeys } from "../../utils/constants";
import { searchTaobaoShopInBackground } from "./background";

/**
 * 搜索淘宝店铺右键事件注册
 * @param clickData
 */
export function searchTaobaoShopInEvent(
  clickData: chrome.contextMenus.OnClickData
) {
  const shopName = clickData.selectionText;
  if (!shopName) {
    notifications("apptip", "未选中相关名称,无法进行店铺搜索");
    return;
  }
  chrome.tabs.query({ active: true, currentWindow: true }).then((tabs) => {
    if (tabs[0].url?.indexOf(".chuntianxiaozhu.com") == -1) {
      notifications("apptip", "该插件只允许在特定域名下使用");
      return;
    }
    searchTaobaoShopInBackground({
      shopName,
    }).then((result) => {
      sendContentMessage({
        type: FrMesagesKeys.frShowShopList,
        data: result,
      });
    });
  });
}
