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

/**
 * 向Chrome发送消息
 * @param message 消息
 */
export async function sendChromeAPIMessage(message) {
    message.type = 'api'
    message.url = "https://kuajing.pinduoduo.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 function sendChromeNotification(message) {
    message.type = 'notify'
    return new Promise((resolve) => {
        // @ts-ignore
        chrome.runtime.sendMessage(message, resolve)
    })
}
