"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 ReactRenderer_1 = require("../../../behavior/ReactRenderer/index.js");
const entity_id_1 = require("../../../../utils/entity-id.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, ReactRenderer_1.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(YfmHtmlBlockView_1.STOP_EVENT_CLASSNAME);
    }
    validateEntityId() {
        if ((0, entity_id_1.isInvalidEntityId)({
            node: this.node,
            doc: this.view.state.doc,
            defaultId: const_1.defaultYfmHtmlBlockEntityId,
        })) {
            const newId = (0, entity_id_1.generateEntityId)(const_1.YfmHtmlBlockConsts.NodeName);
            this.view.dispatch(this.view.state.tr.setNodeAttribute(this.getPos(), const_1.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 ((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
