import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__ from "../../api-client/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_markdown_style_mjs__ from "../../schema-component/antd/markdown/style.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__schema_component_antd_markdown_util_mjs__ from "../../schema-component/antd/markdown/util.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__ from "../../style/theme/index.mjs";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
const PLUGIN_STATICS_PATH = '/static/plugins/';
const PluginDocument = /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react__.memo)((props)=>{
    const { isDarkTheme } = (0, __WEBPACK_EXTERNAL_MODULE__style_theme_index_mjs__.useGlobalTheme)();
    const { componentCls, hashId } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_markdown_style_mjs__.useStyles)({
        isDarkTheme
    });
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__.useStyles)();
    const { url, packageName } = props;
    const [docUrl, setDocUrl] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(url);
    const { data, loading, error } = (0, __WEBPACK_EXTERNAL_MODULE__api_client_index_mjs__.useRequest)({
        url: docUrl,
        baseURL: '/'
    }, {
        refreshDeps: [
            docUrl
        ]
    });
    const { html, loading: parseLoading } = (0, __WEBPACK_EXTERNAL_MODULE__schema_component_antd_markdown_util_mjs__.useParseMarkdown)(data);
    const htmlWithOutRelativeDirect = (0, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.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, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const md = document.getElementById('pm-md-preview');
        md.addEventListener('click', handleSwitchDocLang);
        return ()=>{
            removeEventListener('click', handleSwitchDocLang);
        };
    }, [
        handleSwitchDocLang
    ]);
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: styles.PluginDocument,
        id: "pm-md-preview",
        children: loading || parseLoading ? /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {}) : /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
            className: "".concat(componentCls, " ").concat(hashId, " tb-markdown tb-markdown-default tb-markdown-table"),
            dangerouslySetInnerHTML: {
                __html: error ? '' : htmlWithOutRelativeDirect
            }
        })
    });
});
PluginDocument.displayName = 'PluginDocument';
export { PluginDocument };
