"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != 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__);
__webpack_require__.d(__webpack_exports__, {
    PluginDocument: ()=>PluginDocument
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const external_antd_namespaceObject = require("antd");
const index_js_namespaceObject = require("../../api-client/index.js");
const style_js_namespaceObject = require("../../schema-component/antd/markdown/style.js");
const util_js_namespaceObject = require("../../schema-component/antd/markdown/util.js");
const theme_index_js_namespaceObject = require("../../style/theme/index.js");
const external_style_js_namespaceObject = require("./style.js");
const PLUGIN_STATICS_PATH = '/static/plugins/';
const PluginDocument = /*#__PURE__*/ (0, external_react_namespaceObject.memo)((props)=>{
    const { isDarkTheme } = (0, theme_index_js_namespaceObject.useGlobalTheme)();
    const { componentCls, hashId } = (0, style_js_namespaceObject.useStyles)({
        isDarkTheme
    });
    const { styles } = (0, external_style_js_namespaceObject.useStyles)();
    const { url, packageName } = props;
    const [docUrl, setDocUrl] = (0, external_react_namespaceObject.useState)(url);
    const { data, loading, error } = (0, index_js_namespaceObject.useRequest)({
        url: docUrl,
        baseURL: '/'
    }, {
        refreshDeps: [
            docUrl
        ]
    });
    const { html, loading: parseLoading } = (0, util_js_namespaceObject.useParseMarkdown)(data);
    const htmlWithOutRelativeDirect = (0, external_react_namespaceObject.useMemo)(()=>{
        if (html) {
            let res = html;
            const pattern = /<a\s+href="\..*?\/([^/]+)"/g;
            res = res.replace(pattern, (match, $1)=>match + 'onclick="return false;"');
            res = res.replace(/src="(.*?)"/g, (match, src)=>{
                if (src.startsWith('http') || src.startsWith('//:')) return match;
                return 'src="'.concat(PLUGIN_STATICS_PATH).concat(packageName, "/").concat(src, '"');
            });
            return res;
        }
        return '';
    }, [
        html,
        packageName
    ]);
    const handleSwitchDocLang = (0, external_react_namespaceObject.useCallback)((e)=>{
        const url = e.target.getAttribute('href');
        if (!url) return;
        const parsedUrl = new URL(docUrl, window.location.origin);
        const combinedUrl = new URL(url, parsedUrl);
        setDocUrl(combinedUrl.pathname);
    }, []);
    (0, external_react_namespaceObject.useEffect)(()=>{
        const md = document.getElementById('pm-md-preview');
        md.addEventListener('click', handleSwitchDocLang);
        return ()=>{
            removeEventListener('click', handleSwitchDocLang);
        };
    }, [
        handleSwitchDocLang
    ]);
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
        className: styles.PluginDocument,
        id: "pm-md-preview",
        children: loading || parseLoading ? /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {}) : /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("div", {
            className: "".concat(componentCls, " ").concat(hashId, " tb-markdown tb-markdown-default tb-markdown-table"),
            dangerouslySetInnerHTML: {
                __html: error ? '' : htmlWithOutRelativeDirect
            }
        })
    });
});
PluginDocument.displayName = 'PluginDocument';
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
