import {
    appendPopupPage,
    tiggerInjectPopupPage
} from "@/logic/content/twitter.js";
import { createApp } from 'vue'
import ViewMessage from '@/view/content/message/index.vue'
import CoutomSentry from "@/uilts/sentry.js"

const addDomMessage = (element) => {
    const div = document.createElement('div')
    div.id = 'denet_message'
    document.body.appendChild(div)
    let app = createApp(element)
    app.mount('#denet_message')
    CoutomSentry.initVue(app)
}

let timer = setInterval(() => {
    if (document.body) {
        init()
        clearInterval(timer)
    }
}, 1000)

const init = () => {
    // appendPopupPage();
    addDomMessage(ViewMessage)

    chrome.runtime.sendMessage({
        actionType: "CONTENT_WINDOW_LOADED_SET_POPUP_PAGE",
        data: {}
    }, () => { });
}


chrome.runtime.sendMessage({
    actionType: "CONTENT_SET_POPUP_CONFIG",
    data: {
        popup: 'popup.html'
    }
}, () => { });

chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {

    switch (req.actionType) {
        case 'BACK_PING':
            chrome.runtime.sendMessage({ actionType: "CONTENT_PONG", data: '1' }, (res) => { console.log(res) })
            break
        case 'BG_INJECT_EXTENSION_POPUP':
            tiggerInjectPopupPage();
            break
    }
})
