import { jsx as _jsx } from "react/jsx-runtime";
import { Portal } from '@gravity-ui/uikit';
import { getReactRendererFromState } from "../../../behavior/ReactRenderer/index.js";
import { generateEntityId, isInvalidEntityId } from "../../../../utils/entity-id.js";
import { YfmHtmlBlockConsts, defaultYfmHtmlBlockEntityId } from "../YfmHtmlBlockSpecs/const.js";
import { STOP_EVENT_CLASSNAME, YfmHtmlBlockView } from "./YfmHtmlBlockView.js";
export class WYfmHtmlBlockNodeView {
    dom;
    node;
    view;
    getPos;
    options = {};
    renderItem;
    constructor({ node, view, getPos, options, }) {
        this.node = node;
        this.dom = document.createElement('div');
        this.dom.classList.add('yfm-html-block-container');
        this.dom.contentEditable = 'false';
        this.view = view;
        this.getPos = getPos;
        this.options = options;
        this.renderItem = getReactRendererFromState(view.state).createItem('yfmHtmlBlock-view', this.renderYfmHtmlBlock.bind(this));
        this.validateEntityId();
    }
    update(node) {
        if (node.type !== this.node.type)
            return false;
        this.node = node;
        this.renderItem.rerender();
        return true;
    }
    destroy() {
        this.renderItem.remove();
    }
    ignoreMutation() {
        return true;
    }
    stopEvent(e) {
        const target = e.target;
        return target.classList.contains(STOP_EVENT_CLASSNAME);
    }
    validateEntityId() {
        if (isInvalidEntityId({
            node: this.node,
            doc: this.view.state.doc,
            defaultId: defaultYfmHtmlBlockEntityId,
        })) {
            const newId = generateEntityId(YfmHtmlBlockConsts.NodeName);
            this.view.dispatch(this.view.state.tr.setNodeAttribute(this.getPos(), YfmHtmlBlockConsts.NodeAttrs.EntityId, newId));
        }
    }
    onChange(attrs) {
        const pos = this.getPos();
        if (pos === undefined)
            return;
        const tr = this.view.state.tr.setNodeMarkup(pos, undefined, {
            ...this.node.attrs,
            ...attrs,
        }, []);
        this.view.dispatch(tr);
    }
    renderYfmHtmlBlock() {
        return (_jsx(Portal, { container: this.dom, children: _jsx(YfmHtmlBlockView, { getPos: this.getPos, node: this.node, onChange: this.onChange.bind(this), options: this.options, view: this.view }) }));
    }
}
//# sourceMappingURL=NodeView.js.map
