'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

var React = require('react');
var bytemd = require('bytemd');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

function _interopNamespace(e) {
    if (e && e.__esModule) return e;
    var n = Object.create(null);
    if (e) {
        Object.keys(e).forEach(function (k) {
            if (k !== 'default') {
                var d = Object.getOwnPropertyDescriptor(e, k);
                Object.defineProperty(n, k, d.get ? d : {
                    enumerable: true,
                    get: function () {
                        return e[k];
                    }
                });
            }
        });
    }
    n['default'] = e;
    return Object.freeze(n);
}

var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
var bytemd__namespace = /*#__PURE__*/_interopNamespace(bytemd);

const Editor = ({ children, onChange, ...props }) => {
    const ed = React.useRef();
    const el = React.useRef(null);
    React.useEffect(() => {
        if (!el.current)
            return;
        const editor = new bytemd__namespace.Editor({
            target: el.current,
            props,
        });
        editor.$on('change', (e) => {
            onChange === null || onChange === void 0 ? void 0 : onChange(e.detail.value);
        });
        ed.current = editor;
        return () => {
            editor.$destroy();
        };
    }, []);
    React.useEffect(() => {
        var _a;
        // TODO: performance
        (_a = ed.current) === null || _a === void 0 ? void 0 : _a.$set(props);
    }, [props]);
    return React__default['default'].createElement("div", { ref: el });
};

const Viewer = ({ value, sanitize, plugins }) => {
    var _a;
    const elRef = React.useRef(null);
    const file = React.useMemo(() => {
        try {
            return bytemd__namespace.getProcessor({ sanitize, plugins }).processSync(value);
        }
        catch (err) {
            console.error(err);
        }
    }, [value, sanitize, plugins]);
    React.useEffect(() => {
        const markdownBody = elRef.current;
        if (!markdownBody || !file)
            return;
        const cbs = plugins === null || plugins === void 0 ? void 0 : plugins.map(({ viewerEffect }) => viewerEffect === null || viewerEffect === void 0 ? void 0 : viewerEffect({ markdownBody, file }));
        return () => {
            cbs === null || cbs === void 0 ? void 0 : cbs.forEach((cb) => cb && cb());
        };
    }, [file, plugins]);
    return (React__default['default'].createElement("div", { onClick: (e) => {
            var _a, _b;
            const $ = e.target;
            if ($.tagName !== 'A')
                return;
            const href = $.getAttribute('href');
            if (!(href === null || href === void 0 ? void 0 : href.startsWith('#')))
                return;
            (_b = (_a = elRef.current) === null || _a === void 0 ? void 0 : _a.querySelector('#user-content-' + href.slice(1))) === null || _b === void 0 ? void 0 : _b.scrollIntoView();
        }, ref: elRef, className: "markdown-body", dangerouslySetInnerHTML: { __html: (_a = file === null || file === void 0 ? void 0 : file.toString()) !== null && _a !== void 0 ? _a : '' } }));
};

exports.Editor = Editor;
exports.Viewer = Viewer;
//# sourceMappingURL=index.cjs.js.map
