"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useMarkdownEditor = useMarkdownEditor;
const react_1 = require("react");
const getTransformers_1 = require("../core/markdown/ProseMirrorTransformer/getTransformers.js");
const extensions_1 = require("../extensions/index.js");
const logger_1 = require("../logger.js");
const directive_1 = require("../utils/directive.js");
const Editor_1 = require("./Editor.js");
const wysiwyg_preset_1 = require("./wysiwyg-preset.js");
function useMarkdownEditor(props, deps = []) {
    const editor = (0, react_1.useMemo)(() => {
        const { md = {}, initial = {}, handlers = {}, experimental = {}, markupConfig = {}, wysiwygConfig = {}, logger = new logger_1.Logger2(), mobile, } = props;
        const preset = props.preset ?? 'full';
        const renderStorage = new extensions_1.ReactRenderStorage();
        const pmTransformers = (0, getTransformers_1.getPMTransformers)({
            emptyRowTransformer: experimental.preserveEmptyRows,
        });
        const directiveSyntax = new directive_1.DirectiveSyntaxContext(experimental.directiveSyntax);
        const extensions = (builder) => {
            const extensionOptions = wysiwygConfig.extensionOptions;
            builder.use(wysiwyg_preset_1.BundlePreset, {
                ...extensionOptions,
                directiveSyntax,
                preset,
                reactRenderer: renderStorage,
                onCancel: () => {
                    editor.emit('cancel', null);
                    return true;
                },
                onSubmit: () => {
                    editor.emit('submit', null);
                    return true;
                },
                disableMdAttrs: wysiwygConfig.disableMarkdownAttrs,
                preserveEmptyRows: experimental.preserveEmptyRows,
                placeholderOptions: wysiwygConfig.placeholderOptions,
                mdBreaks: md.breaks,
                fileUploadHandler: handlers.uploadFile,
                needToSetDimensionsForUploadedImages: experimental.needToSetDimensionsForUploadedImages,
                enableNewImageSizeCalculation: experimental.enableNewImageSizeCalculation,
                mobile,
            });
            {
                const extraExtensions = wysiwygConfig.extensions;
                if (extraExtensions) {
                    builder.use(extraExtensions, props.wysiwygConfig?.extensionOptions);
                }
            }
        };
        return new Editor_1.EditorImpl({
            ...props,
            logger,
            preset,
            renderStorage,
            directiveSyntax,
            pmTransformers,
            md,
            initial,
            handlers,
            experimental,
            markupConfig,
            wysiwygConfig: {
                ...wysiwygConfig,
                extensions,
            },
        });
    }, deps);
    (0, react_1.useLayoutEffect)(() => {
        function onToolbarAction({ editorMode, id }) {
            logger_1.globalLogger.action({ mode: editorMode, source: 'toolbar', action: id });
            editor.logger.action({ mode: editorMode, source: 'toolbar', action: id });
        }
        editor.on('toolbar-action', onToolbarAction);
        return () => {
            editor.off('toolbar-action', onToolbarAction);
            editor.destroy();
        };
    }, [editor]);
    return editor;
}
//# sourceMappingURL=useMarkdownEditor.js.map
