"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WysiwygEditor = void 0;
const prosemirror_state_1 = require("prosemirror-state");
const prosemirror_view_1 = require("prosemirror-view");
const ContentHandler_1 = require("./ContentHandler.js");
const ExtensionsManager_1 = require("./ExtensionsManager.js");
const SchemaDynamicModifier_1 = require("./SchemaDynamicModifier.js");
const MarkdownParser_1 = require("./markdown/MarkdownParser.js");
const MarkdownSerializerDynamicModifier_1 = require("./markdown/MarkdownSerializerDynamicModifier.js");
const actions_1 = require("./utils/actions.js");
const dynamicModifiers_1 = require("./utils/dynamicModifiers.js");
const metrics_1 = require("./utils/metrics.js");
class WysiwygEditor {
    #view;
    #serializer;
    #parser;
    #actions;
    #contentHandler;
    #escapeConfig;
    get dom() {
        return this.#view.dom;
    }
    get serializer() {
        return this.#serializer;
    }
    get parser() {
        return this.#parser;
    }
    get actions() {
        return this.#actions.actions;
    }
    /** @internal used for prosemirror-dev-tools in demo */
    get view() {
        return this.#view;
    }
    constructor({ domElem, initialContent = '', extensions = () => { }, allowHTML, mdPreset, linkify, pmTransformers, linkifyTlds, escapeConfig, onChange, onDocChange, modifiers, }) {
        const dynamicModifiersConfig = modifiers
            ? (0, dynamicModifiers_1.convertDynamicModifiersConfigs)(modifiers)
            : undefined;
        const dynamicModifiers = dynamicModifiersConfig
            ? {
                schema: new SchemaDynamicModifier_1.SchemaDynamicModifier(dynamicModifiersConfig.schema),
                parser: new MarkdownParser_1.MarkdownParserDynamicModifier(dynamicModifiersConfig.parser),
                serializer: new MarkdownSerializerDynamicModifier_1.MarkdownSerializerDynamicModifier(dynamicModifiersConfig.serializer),
            }
            : undefined;
        const { schema, markupParser: parser, serializer, nodeViews, markViews, plugins, rawActions, actions, } = ExtensionsManager_1.ExtensionsManager.process(extensions, {
            // "breaks" option only affects the renderer, but not the parser
            mdOpts: { html: allowHTML, linkify, breaks: true, preset: mdPreset },
            linkifyTlds,
            pmTransformers,
            dynamicModifiers,
        });
        const state = prosemirror_state_1.EditorState.create({
            schema,
            doc: parser.parse(initialContent),
            plugins,
        });
        const thisOnChange = () => this.tryOnChange(onChange);
        const thisOnDocChange = () => this.tryOnChange(onDocChange);
        this.#view = new prosemirror_view_1.EditorView(domElem ?? null, {
            state,
            nodeViews,
            markViews,
            dispatchTransaction(tr) {
                const newState = this.state.apply(tr);
                // @ts-expect-error
                this.updateState(newState);
                thisOnChange();
                if (tr.docChanged) {
                    thisOnDocChange();
                }
                (0, metrics_1.logTransactionMetrics)(tr);
            },
        });
        this.#actions = actions.setActions((0, actions_1.bindActions)(rawActions)(this.#view));
        this.#serializer = serializer;
        this.#parser = parser;
        this.#contentHandler = new ContentHandler_1.WysiwygContentHandler(this.#view, parser);
        this.#escapeConfig = escapeConfig;
    }
    action(actionName) {
        return this.#actions.action(actionName);
    }
    focus() {
        return this.#view.focus();
    }
    hasFocus() {
        return this.#view.hasFocus();
    }
    getValue() {
        return this.#serializer.serialize(this.#view.state.doc, this.#escapeConfig);
    }
    isEmpty() {
        const { doc } = this.#view.state;
        return (doc.childCount === 1 &&
            doc.firstChild?.type.name === 'paragraph' &&
            doc.firstChild.childCount === 0);
    }
    clear() {
        return this.#contentHandler.clear();
    }
    replace(newMarkup) {
        return this.#contentHandler.replace(newMarkup);
    }
    prepend(markup) {
        return this.#contentHandler.prepend(markup);
    }
    append(markup) {
        return this.#contentHandler.append(markup);
    }
    moveCursor(position) {
        return this.#contentHandler.moveCursor(position);
    }
    destroy() {
        this.#view.destroy();
    }
    tryOnChange(cb) {
        if (cb) {
            try {
                cb(this);
            }
            catch (err) {
                console.error(err);
            }
        }
    }
}
exports.WysiwygEditor = WysiwygEditor;
//# sourceMappingURL=Editor.js.map
