import { html } from 'lit-html';
import { unsafeHTML } from 'lit-html/directives/unsafe-html';
import RichTextManager from '../helpers/RichTextManager';
import AbstractNode from './AbstractNode';
export default class TextNode extends AbstractNode {
    constructor(arena, id, attributes, text) {
        super(arena, id, attributes);
        this.hasParent = true;
        this.hasChildren = false;
        this.hasText = true;
        this.inline = false;
        this.single = false;
        if (text && text instanceof RichTextManager) {
            this.richTextManager = text.clone();
        }
        else {
            this.richTextManager = new RichTextManager(text);
        }
    }
    getTemplate(frms) {
        const content = this.richTextManager.getHtml(frms);
        return this.arena.getTemplate(html `${unsafeHTML(content)}`, this.getGlobalIndex(), this.attributes);
    }
    getPublicHtml(frms) {
        if (this.isEmpty()) {
            return '';
        }
        const text = this.richTextManager.getHtml(frms);
        return this.arena.getPublicHtml(text, this.attributes, this, frms);
    }
    getOutputHtml(frms, start, end) {
        if (this.isEmpty()) {
            return '';
        }
        const text = this.richTextManager.getHtml(frms, start, end);
        return this.arena.getOutputTemplate(text, this.attributes, true);
    }
    getPlainText(start, end) {
        if (this.isEmpty()) {
            return '';
        }
        let text = this.richTextManager.getText();
        if (end === undefined) {
            text = text.slice(start || 0);
        }
        else {
            text = text.slice(start || 0, end);
        }
        return this.arena.getPlain(text, this);
    }
    getTextCursor(index) {
        return {
            node: this,
            offset: index === -1 ? this.getTextLength() : index,
        };
    }
    // Text node methods //
    insertText(text, offset, keepFormatings = false) {
        return {
            node: this,
            offset: this.richTextManager.insertText(text, offset, keepFormatings),
        };
    }
    insertFormating(name, start, end) {
        this.richTextManager.insertFormating(name, start, end);
    }
    togglePromiseFormating(formating, offset) {
        this.richTextManager.togglePromiseFormating(formating, offset);
    }
    ltrim() {
        this.richTextManager.ltrim();
    }
    rtrim() {
        this.richTextManager.rtrim();
    }
    clearSpaces() {
        this.richTextManager.clearSpaces();
    }
    toggleFormating(name, start, end) {
        this.richTextManager.toggleFormating(name, start, end);
    }
    clearFormatings(start, end) {
        this.richTextManager.clearFormatings(start, end);
    }
    getText() {
        return this.richTextManager;
    }
    getRawText() {
        return this.richTextManager.getText();
    }
    setRawText(text) {
        return this.richTextManager.setText(text);
    }
    cutText(start, end) {
        return this.richTextManager.cutText(start, end);
    }
    removeText(start, end) {
        this.richTextManager.removeText(start, end);
    }
    clearText() {
        this.richTextManager.clearText();
    }
    getTextLength() {
        return this.richTextManager.getTextLength();
    }
    isEmpty() {
        return /^[\s\n]*$/.test(this.getRawText());
    }
    addInlineNode(arena, start, end) {
        return this.richTextManager.addInlineNode(arena, start, end);
    }
    getInlineNode(arena, start, end) {
        return this.richTextManager.getInlineNode(arena, start, end);
    }
    removeInlineNode(node) {
        this.richTextManager.removeInlineNode(node);
    }
    updateInlineNode(node, start, end) {
        this.richTextManager.updateInlineNode(node, start, end);
    }
    clone() {
        return new TextNode(this.arena, this.id, this.attributes, this.richTextManager.clone());
    }
}
//# sourceMappingURL=TextNode.js.map