"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isMentionNode = exports.createMentionNode = exports.MentionNode = void 0;
const lexical_1 = require("lexical");
function convertMentionElement(domNode) {
    const mention = domNode;
    return {
        node: createMentionNode({
            avatar: '',
            bio: '',
            date_joined: undefined,
            description: '',
            gender: '',
            location: '',
            real_name: '',
            reputation: 0,
            role: '',
            status: '',
            tags: undefined,
            website: '',
            id: domNode.getAttribute('id'),
            ext_id: domNode.getAttribute('ext_id'),
            username: domNode.innerText.replace('@', '')
        })
    };
}
class MentionNode extends lexical_1.TextNode {
    constructor(user, text, key) {
        super(text !== null && text !== void 0 ? text : `@${user.username}`, key);
        this.__user = user;
    }
    static getType() {
        return 'mention';
    }
    static clone(node) {
        return new MentionNode(node.__user, node.__text, node.__key);
    }
    createDOM(config) {
        const tag = 'mention';
        const dom = document.createElement(tag);
        dom.setAttribute('id', `${this.__user.id}`);
        dom.setAttribute('ext-id', `${this.__user.ext_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({ mention: (node) => ({
                conversion: convertMentionElement,
                priority: 0
            }) }, dom);
    }
    exportDOM() {
        const element = document.createElement('mention');
        element.setAttribute('id', `${this.__user.id}`);
        element.setAttribute('ext-id', `${this.__user.ext_id}`);
        element.textContent = `@${this.__user.username}`;
        return { element };
    }
    isTextEntity() {
        return true;
    }
    static importJSON(serializedNode) {
        const { user } = serializedNode;
        const node = createMentionNode(user);
        return node;
    }
    exportJSON() {
        return Object.assign(Object.assign({}, super.exportJSON()), { user: this.__user, type: 'mention', version: 1 });
    }
}
exports.MentionNode = MentionNode;
function createMentionNode(user) {
    const mentionNode = new MentionNode(user);
    mentionNode.setMode('segmented').toggleDirectionless();
    return mentionNode;
}
exports.createMentionNode = createMentionNode;
function isMentionNode(node) {
    return node.getType() === 'mention';
}
exports.isMentionNode = isMentionNode;
