import store from '@/store'
import {genAnti} from "@/api/genAnti";

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export async function sendChromeAPIMessage(message) {
  message.type = 'api'
  if (!message.url.startsWith('http')) {
    message.url = "https://seller.kuajingmaihuo.com/" + message.url;
  }
  message.anti = message.anti || false
  if (message.needMallId) {
    // 如果参数中没有携带MallId，则从state中获取
    if (!message.mallId) {
      message.mallId = store.state.mallId;
    }
  }
  if (message.anti) {
    message.anti = await genAnti.a()
  }
  return new Promise((resolve) => {
    // @ts-ignore
    chrome.runtime.sendMessage(message, resolve)
  })
}

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export async function sendTemuAPIMessage(message) {
  message.type = 'temuApi'
  message.url = "https://www.temu.com/" + message.url;
  message.anti = message.anti || false
  if (message.anti) {
    message.anti = await genAnti.a()
    message.data.anti_content = message.anti
  }
  return new Promise((resolve) => {
    // @ts-ignore
    chrome.runtime.sendMessage(message, resolve)
  })
}

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export async function sendChromeWebReqMessage(message) {
  return new Promise((resolve) => {
    // @ts-ignore
    chrome.runtime.sendMessage(message, resolve)
  })
}

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export async function sendSheinAPIMessage(message) {
  message.type = 'sheinApi'
  message.url = "https://www.shein.com/" + message.url;
  return new Promise((resolve) => {
    // @ts-ignore
    chrome.runtime.sendMessage(message, resolve)
  })
}

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export function sendChromeNotification(message) {
  message.type = 'notify'
  return new Promise((resolve) => {
    // @ts-ignore
    chrome.runtime.sendMessage(message, resolve)
  })
}

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export function sendAliexpressAPIMessage(message) {
  message.type = 'aliexpress'
  const {mtopConfig = {appKey: "12574478"}} = window
  message.appKey = message.appKey || mtopConfig.appKey
  return new Promise((resolve) => {
    chrome.runtime.sendMessage(message, resolve)
  })
}
