import { TextNode } from 'lexical';
function convertHashtagElement(domNode) {
    const hashtag = domNode;
    return {
        node: createHashtagNode({
            id: hashtag.getAttribute('id'),
            name: hashtag.innerText.replace('#', '')
        })
    };
}
export class HashtagNode extends TextNode {
    constructor(category, text, key) {
        super(text !== null && text !== void 0 ? text : `#${category.name}`, key);
        this.__category = category;
    }
    static getType() {
        return 'hashtag';
    }
    static clone(node) {
        return new HashtagNode(node.__category, node.__text, node.__key);
    }
    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 = 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 });
    }
}
export function createHashtagNode(category) {
    const hashtagNode = new HashtagNode(category);
    hashtagNode.setMode('segmented').toggleDirectionless();
    return hashtagNode;
}
export function isHashtagNode(node) {
    return node.getType() === 'hashtag';
}
