"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.CheckboxInputView = void 0;
const const_1 = require("./const.js");
class CheckboxInputView {
    static create = (node, view, getPos) => new this(node, view, getPos);
    dom;
    _node;
    _view;
    _getPos;
    constructor(node, view, getPos) {
        this._node = node;
        this._view = view;
        this._getPos = getPos;
        this.dom = this._createDomElem();
        this._applyNodeAttrsToDomElem();
    }
    ignoreMutation() {
        return true;
    }
    update(node) {
        if (node.type !== this._node.type)
            return false;
        this._node = node;
        this._applyNodeAttrsToDomElem();
        return true;
    }
    destroy() {
        this.dom.removeEventListener('click', this._onInputClick);
    }
    _createDomElem() {
        const dom = document.createElement('input');
        dom.setAttribute('class', (0, const_1.b)('input'));
        dom.addEventListener('click', this._onInputClick);
        return dom;
    }
    _applyNodeAttrsToDomElem() {
        const { dom, _node: node } = this;
        for (const [key, value] of Object.entries(node.attrs)) {
            if (value)
                dom.setAttribute(key, value);
            else
                dom.removeAttribute(key);
        }
        const checked = node.attrs[const_1.CheckboxAttr.Checked] === 'true';
        this.dom.checked = checked;
    }
    _onInputClick = (event) => {
        if (event.target instanceof HTMLInputElement) {
            const { checked } = event.target;
            const pos = this._getPos();
            if (pos !== undefined)
                this._view.dispatch(this._view.state.tr.setNodeAttribute(pos, const_1.CheckboxAttr.Checked, checked ? 'true' : null));
        }
        this._view.focus();
    };
}
exports.CheckboxInputView = CheckboxInputView;
//# sourceMappingURL=nodeviews.js.map
