"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isHashtagNode = exports.createHashtagNode = exports.HashtagNode = void 0;
const lexical_1 = require("lexical");
function convertHashtagElement(domNode) {
    const hashtag = domNode;
    return {
        node: createHashtagNode({
            id: hashtag.getAttribute('id'),
            name: hashtag.innerText.replace('#', '')
        })
    };
}
class HashtagNode extends lexical_1.TextNode {
    static getType() {
        return 'hashtag';
    }
    static clone(node) {
        return new HashtagNode(node.__category, node.__text, node.__key);
    }
    constructor(category, text, key) {
        super(text !== null && text !== void 0 ? text : `#${category.name}`, key);
        this.__category = category;
    }
    createDOM(config) {
        const tag = 'hashtag';
        const dom = document.createElement(tag);
        dom.setAttribute('id', `${this.__category.id}`);
        dom.innerText = this.__text;
        return dom;
    }
    updateDOM(prevNode, dom, config) {
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        const inner = dom.firstChild;
        if (inner === null) {
            return true;
        }
        super.updateDOM(prevNode, inner, config);
        return false;
    }
    static importDOM() {
        // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
        // @ts-ignore
        const dom = lexical_1.TextNode.importDOM();
        return Object.assign({ hashtag: (node) => ({
                conversion: convertHashtagElement,
                priority: 0
            }) }, dom);
    }
    exportDOM() {
        const element = document.createElement('hashtag');
        element.setAttribute('id', `${this.__category.id}`);
        element.textContent = `#${this.__category.name}`;
        return { element };
    }
    isTextEntity() {
        return true;
    }
    static importJSON(serializedNode) {
        const { category } = serializedNode;
        const node = createHashtagNode(category);
        return node;
    }
    exportJSON() {
        return Object.assign(Object.assign({}, super.exportJSON()), { category: this.__category, type: 'hashtag', version: 1 });
    }
}
exports.HashtagNode = HashtagNode;
function createHashtagNode(category) {
    const hashtagNode = new HashtagNode(category);
    hashtagNode.setMode('segmented').toggleDirectionless();
    return hashtagNode;
}
exports.createHashtagNode = createHashtagNode;
function isHashtagNode(node) {
    return node.getType() === 'hashtag';
}
exports.isHashtagNode = isHashtagNode;
