"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.backgroundPort = void 0;
const extension_1 = require("../../../extension");
const backgroundPort = () => {
    const extensionElement = (0, extension_1.getExtensionElement)();
    if (!extensionElement) {
        throw new Error('extension element not found');
    }
    const port = {
        send(message) {
            extensionElement.dispatchEvent(new CustomEvent('message', { detail: { type: 'send', data: message } }));
        },
        onmessage(event) { },
        onerror(event) { },
        terminate() {
            //
        },
    };
    extensionElement.addEventListener('error', (event) => {
        const { detail } = event;
        port.onerror(detail);
    });
    extensionElement.addEventListener('message', (event) => {
        const { detail } = event;
        if (detail.type === 'send') {
            return;
        }
        port.onmessage({ data: detail.data });
    });
    return port;
};
exports.backgroundPort = backgroundPort;
//# sourceMappingURL=extension.js.map