"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WYfmHtmlBlockNodeView = void 0;
const jsx_runtime_1 = require("react/jsx-runtime");
const uikit_1 = require("@gravity-ui/uikit");
const behavior_1 = require("../../../behavior/index.js");
const const_1 = require("../YfmHtmlBlockSpecs/const.js");
const YfmHtmlBlockView_1 = require("./YfmHtmlBlockView.js");
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 = (0, behavior_1.getReactRendererFromState)(view.state).createItem('yfmHtmlBlock-view', this.renderYfmHtmlBlock.bind(this));
    }
    update(node) {
        if (node.type !== this.node.type)
            return false;
        if (node.attrs[const_1.YfmHtmlBlockConsts.NodeAttrs.newCreated] !==
            this.node.attrs[const_1.YfmHtmlBlockConsts.NodeAttrs.newCreated])
            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(YfmHtmlBlockView_1.STOP_EVENT_CLASSNAME);
    }
    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 ((0, jsx_runtime_1.jsx)(uikit_1.Portal, { container: this.dom, children: (0, jsx_runtime_1.jsx)(YfmHtmlBlockView_1.YfmHtmlBlockView, { getPos: this.getPos, node: this.node, onChange: this.onChange.bind(this), options: this.options, view: this.view }) }));
    }
}
exports.WYfmHtmlBlockNodeView = WYfmHtmlBlockNodeView;
//# sourceMappingURL=NodeView.js.map
