"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.r = (exports1)=>{
        if ("u" > typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
const EXTJS_MARK = '__extjs__';
const REQ_TYPE = 'EXTJS_WTW_LOAD';
const RES_TYPE = 'EXTJS_WTW_LOADED';
const EXTJS_BASE_KEY = '__EXTJS_EXTENSION_BASE__';
const EXTJS_BASE_ATTR = 'data-extjs-extension-base';
function safeString(x) {
    return 'string' == typeof x && x.length > 0;
}
function hasForbiddenProtocol(url) {
    return /^[a-zA-Z][a-zA-Z0-9+.-]*:\/\//.test(url) && !/^((chrome|moz)-extension):\/\//.test(url);
}
function getRuntime() {
    try {
        if ('object' == typeof globalThis.browser) return globalThis.browser;
    } catch  {}
    try {
        if ('object' == typeof globalThis.chrome) return globalThis.chrome;
    } catch  {}
    return null;
}
function setExtensionBaseUrl() {
    try {
        const rt = getRuntime();
        const getURL = rt?.runtime?.getURL;
        if ('function' != typeof getURL) return;
        const base = String(getURL('/'));
        if (base) {
            window[EXTJS_BASE_KEY] = base;
            try {
                const docEl = document && document.documentElement;
                if (docEl && 'function' == typeof docEl.setAttribute) docEl.setAttribute(EXTJS_BASE_ATTR, base);
            } catch  {}
        }
    } catch  {}
}
setExtensionBaseUrl();
function resolveExtensionUrl(inputUrl) {
    if (!safeString(inputUrl)) return null;
    if (hasForbiddenProtocol(inputUrl)) return null;
    if (/^((chrome|moz)-extension):\/\//.test(inputUrl)) return inputUrl;
    const rt = getRuntime();
    const getURL = rt?.runtime?.getURL;
    if ('function' != typeof getURL) return null;
    let p = inputUrl;
    try {
        if (p.startsWith('/')) p = new URL('https://example.invalid' + p).pathname;
        else if (p.includes('?') || p.includes('#')) p = p.split('?')[0].split('#')[0];
    } catch  {}
    if (!p) return null;
    if (!p.startsWith('/')) p = '/' + p;
    return String(getURL(p));
}
function injectScript(src) {
    return new Promise((resolve, reject)=>{
        const el = document.createElement("script");
        el.src = src;
        el.async = false;
        el.onload = ()=>{
            try {
                el.remove();
            } catch  {}
            resolve();
        };
        el.onerror = ()=>{
            try {
                el.remove();
            } catch  {}
            reject(new Error("Failed to load script: " + src));
        };
        (document.head || document.documentElement).appendChild(el);
    });
}
function postResponse(res) {
    try {
        window.postMessage(res, '*');
    } catch  {}
}
window.addEventListener('message', (event)=>{
    if (event.source !== window) return;
    const data = event.data;
    if (!data || true !== data[EXTJS_MARK]) return;
    if (data.type !== REQ_TYPE) return;
    if (!safeString(data.requestId) || !safeString(data.url)) return;
    const requestId = data.requestId;
    const resolved = resolveExtensionUrl(data.url);
    if (!resolved) return void postResponse({
        [EXTJS_MARK]: true,
        type: RES_TYPE,
        requestId,
        ok: false,
        error: 'No extension runtime available to resolve URL'
    });
    injectScript(resolved).then(()=>{
        postResponse({
            [EXTJS_MARK]: true,
            type: RES_TYPE,
            requestId,
            ok: true
        });
    }).catch((e)=>{
        postResponse({
            [EXTJS_MARK]: true,
            type: RES_TYPE,
            requestId,
            ok: false,
            error: e instanceof Error ? e.message : String(e)
        });
    });
});
for(var __rspack_i in __webpack_exports__)exports[__rspack_i] = __webpack_exports__[__rspack_i];
Object.defineProperty(exports, '__esModule', {
    value: true
});
