/* eslint-disable no-restricted-globals */
import { register } from "@substrate/light-client-extension-helpers/content-script"
import { CHANNEL_ID } from "../constants"

try {
    const s = document.createElement("script")
    s.type = "module"
    s.src = chrome.runtime.getURL("inpage/inpage.js")
    s.onload = () => s.remove()
    ;(document.head || document.documentElement).appendChild(s)
} catch (error) {
    console.error("error injecting inpage/inpage.js", error)
}

register(CHANNEL_ID)

const port = chrome.runtime.connect({ name: CHANNEL_ID })
port.onMessage.addListener((msg) =>
// origin is needed to filter from other postMessages
window.postMessage({ origin: `${CHANNEL_ID}/extension`, msg }),
)
window.addEventListener("message", ({ data }) => {
    if (data.origin !== `${CHANNEL_ID}/web`) return
    port.postMessage(data.msg)
})
