import { useLayoutEffect, useMemo } from 'react';
import { getPMTransformers } from "../core/markdown/ProseMirrorTransformer/getTransformers.js";
import { ReactRenderStorage } from "../extensions/index.js";
import { Logger2, globalLogger } from "../logger.js";
import { DirectiveSyntaxContext } from "../utils/directive.js";
import { EditorImpl } from "./Editor.js";
import { BundlePreset } from "./wysiwyg-preset.js";
export function useMarkdownEditor(props, deps = []) {
    const editor = useMemo(() => {
        const { md = {}, initial = {}, handlers = {}, experimental = {}, markupConfig = {}, wysiwygConfig = {}, logger = new Logger2(), mobile, } = props;
        const preset = props.preset ?? 'full';
        const renderStorage = new ReactRenderStorage();
        const pmTransformers = getPMTransformers({
            emptyRowTransformer: experimental.preserveEmptyRows,
        });
        const directiveSyntax = new DirectiveSyntaxContext(experimental.directiveSyntax);
        const extensions = (builder) => {
            const extensionOptions = wysiwygConfig.extensionOptions;
            builder.use(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 EditorImpl({
            ...props,
            logger,
            preset,
            renderStorage,
            directiveSyntax,
            pmTransformers,
            md,
            initial,
            handlers,
            experimental,
            markupConfig,
            wysiwygConfig: {
                ...wysiwygConfig,
                extensions,
            },
        });
    }, deps);
    useLayoutEffect(() => {
        function onToolbarAction({ editorMode, id }) {
            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
