"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ImageRenderer () {
        return ImageRenderer;
    },
    get LinkRenderer () {
        return LinkRenderer;
    },
    get TableRenderer () {
        return TableRenderer;
    },
    get renderCode () {
        return renderCode;
    }
});
const _react = /*#__PURE__*/ _interop_require_default(require("react"));
const _reactsyntaxhighlighter = require("react-syntax-highlighter");
const _Markdowntokens = require("../Markdown.tokens");
const _Markdownstyles = require("../Markdown.styles");
const _sanitizeUrl = require("../utils/sanitizeUrl");
const _renderUtils = require("./renderUtils");
function _interop_require_default(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}
const getCodeLanguage = (className)=>{
    if (!className) {
        return undefined;
    }
    const match = className.match(/(?:lang(?:uage)?-)([a-z0-9+#.-]+)/i);
    return match && match[1] ? match[1].toLowerCase() : undefined;
};
const CodeRenderer = ({ children, className, inline })=>{
    const value = String(children || '').replace(/\n$/, '');
    if (inline) {
        return /*#__PURE__*/ _react.default.createElement(_Markdownstyles.InlineCode, {
            className: className
        }, children);
    }
    const language = getCodeLanguage(className) || 'text';
    return /*#__PURE__*/ _react.default.createElement(_Markdownstyles.CodeBlock, null, /*#__PURE__*/ _react.default.createElement(_Markdownstyles.CodeBlockHeader, null, /*#__PURE__*/ _react.default.createElement(_Markdownstyles.CodeLanguage, null, language)), /*#__PURE__*/ _react.default.createElement(_Markdownstyles.CodeBlockBody, null, /*#__PURE__*/ _react.default.createElement(_reactsyntaxhighlighter.Prism, {
        PreTag: "div",
        codeTagProps: {
            style: {
                fontFamily: 'SFMono-Regular, Consolas, Liberation Mono, Menlo, monospace'
            }
        },
        customStyle: {
            margin: 0,
            padding: `var(${_Markdowntokens.tokens.codeBlockPadding}, 1rem 1.125rem)`
        },
        language: language,
        style: _Markdownstyles.syntaxTheme,
        wrapLongLines: true
    }, value)));
};
const renderCode = (key, props)=>(0, _renderUtils.renderElement)(key, CodeRenderer, props, props.children);
const ImageRenderer = ({ alt, imageDisplay = 'inline', src, title })=>{
    const sanitizedSrc = (0, _sanitizeUrl.sanitizeUrl)(src);
    if (!sanitizedSrc) {
        return null;
    }
    const Image = imageDisplay === 'block' ? _Markdownstyles.StyledBlockImage : _Markdownstyles.StyledInlineImage;
    return /*#__PURE__*/ _react.default.createElement(Image, {
        alt: alt || '',
        loading: "lazy",
        src: sanitizedSrc,
        title: title
    });
};
const LinkRenderer = ({ href, children, title })=>{
    const sanitizedHref = (0, _sanitizeUrl.sanitizeUrl)(href);
    const isExternal = typeof sanitizedHref === 'string' && /^https?:\/\//.test(sanitizedHref);
    return /*#__PURE__*/ _react.default.createElement("a", {
        href: sanitizedHref,
        rel: isExternal ? 'noreferrer noopener' : undefined,
        target: isExternal ? '_blank' : undefined,
        title: title
    }, children);
};
const TableRenderer = ({ children })=>/*#__PURE__*/ _react.default.createElement(_Markdownstyles.TableScroll, null, /*#__PURE__*/ _react.default.createElement("table", null, children));
