import { html } from 'lit-html';
import { repeat } from 'lit-html/directives/repeat';
import AbstractNode from './AbstractNode';
export default class AbstractParentNode extends AbstractNode {
    constructor(arena, id, children, attributes) {
        super(arena, id, attributes);
        this.hasParent = false;
        this.hasChildren = true;
        this.hasText = false;
        this.inline = false;
        this.single = false;
        this.children = [];
        if (children) {
            children.forEach((child) => child.setParent(this));
            this.children = children;
        }
    }
    get automerge() {
        return this.arena.automerge;
    }
    get group() {
        return this.arena.group;
    }
    get protected() {
        return this.arena.protected;
    }
    getTemplate(frms) {
        // TODO pseudoCursor
        // let pseudoCursorBefore: TemplateResult | string = '';
        // let pseudoCursorAfter: TemplateResult | string = '';
        // if (this.children.length > 0) {
        //   if (!this.children[0].hasText) {
        //     pseudoCursorBefore = html`<div class="pseudo-cursor"><br/></div>`;
        //   }
        //   if (!this.children[this.children.length - 1].hasText) {
        //     pseudoCursorAfter = html`<div class="pseudo-cursor"><br/></div>`;
        //   }
        // }
        // if (this.cache) {
        //   return this.cache;
        // }
        if (this.children.length === 0) {
            this.cache = '';
        }
        else {
            const id = this.getGlobalIndex();
            const content = this.arena.getTemplate(html `
          ${repeat(this.children, (c) => c.getId(), (child) => child.getTemplate(frms))}
        `, id, this.attributes);
            if (this.protected && !this.parent.protected) {
                const removeButton = html `<textarena-remove node-id="${id}">
        <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
        viewBox="0 0 512.001 512.001" xml:space="preserve">
        <g>
          <path d="M294.111,256.001L504.109,46.003c10.523-10.524,10.523-27.586,0-38.109c-10.524-10.524-27.587-10.524-38.11,0L256,217.892
            L46.002,7.894c-10.524-10.524-27.586-10.524-38.109,0s-10.524,27.586,0,38.109l209.998,209.998L7.893,465.999
            c-10.524,10.524-10.524,27.586,0,38.109c10.524,10.524,27.586,10.523,38.109,0L256,294.11l209.997,209.998
            c10.524,10.524,27.587,10.523,38.11,0c10.523-10.524,10.523-27.586,0-38.109L294.111,256.001z" fill="currentColor"/>
        </g>
        </svg>
        </textarena-remove>`;
                this.cache = html `
          <textarena-node>${removeButton}${content}</textarena-node>
        `;
            }
            else {
                this.cache = content;
            }
        }
        return this.cache;
    }
    getPublicHtml(frms) {
        if (this.children.length === 0) {
            return '';
        }
        const content = [];
        for (let i = 0; i < this.children.length; i += 1) {
            content.push(this.children[i].getPublicHtml(frms));
        }
        return this.arena.getPublicHtml(content, this.attributes, this, frms);
    }
    getOutputHtml(frms, start, end) {
        const content = [];
        for (let i = start || 0; i < (end || this.children.length); i += 1) {
            content.push(this.children[i].getOutputHtml(frms));
        }
        return this.arena.getOutputTemplate(content.join('\n'), this.attributes);
    }
    getPlainText(start, end) {
        let content = '';
        for (let i = start || 0; i < (end || this.children.length); i += 1) {
            content += this.children[i].getPlainText();
            content += '\n';
        }
        return content;
    }
    getTextCursor(index) {
        if (!this.arena.arenaForText) {
            return undefined;
        }
        if (index === undefined) {
            for (let i = this.children.length - 1; i >= 0; i -= 1) {
                const { arena } = this.children[i];
                if (arena.hasText || (arena.hasChildren && arena.arenaForText)) {
                    return this.children[i].getTextCursor(index === -1 ? -1 : 0);
                }
            }
        }
        else {
            for (let i = 0; i < this.children.length; i += 1) {
                const { arena } = this.children[i];
                if (arena.hasText || (arena.hasChildren && arena.arenaForText)) {
                    return this.children[i].getTextCursor(index === -1 ? -1 : 0);
                }
            }
        }
        return undefined;
    }
    insertText() {
        throw new Error('Arena for text not found');
    }
    isAllowedNode(arena) {
        return this.arena.allowedArenas.includes(arena);
    }
    insertNode(node, offset) {
        if (this.arena.protected) {
            for (let i = 0; i < this.children.length; i += 1) {
                const child = this.children[i];
                if (child.arena === node.arena) {
                    return child;
                }
            }
        }
        else {
            const index = offset === undefined ? this.children.length : offset;
            node.setParent(this);
            this.children.splice(index, 0, node);
            return node;
        }
        return undefined;
    }
    getChild(index) {
        return this.children[index] || undefined;
    }
    removeChild(index) {
        if (this.arena.protected) {
            return {
                node: this,
                offset: index,
            };
        }
        if (index < 0
            || index >= this.children.length) {
            return {
                node: this,
                offset: 0,
            };
        }
        this.children.splice(index, 1);
        return this.mergeChildren(index);
    }
    getChildren() {
        return this.children;
    }
    insertChildren(nodes, offset) {
        let index = offset || 0;
        if (this.arena.protected) {
            let rest = [...nodes];
            for (let i = offset || 0; i < this.children.length; i += 1) {
                if (rest.length === 0) {
                    break;
                }
                const child = this.children[i];
                if (child.hasText && rest[0].hasText) {
                    const inserted = rest.shift();
                    if (inserted && inserted.hasText) {
                        child.insertText(inserted.getText(), 0);
                    }
                }
                else if (child.hasChildren) {
                    rest = child.insertChildren(rest, child.children.length);
                }
            }
            return rest;
        }
        const rest = [];
        nodes.forEach((node) => {
            if (this.arena.allowedArenas.includes(node.arena)) {
                node.setParent(this);
                this.children.splice(index, 0, node);
                index += 1;
            }
            else {
                rest.push(node);
            }
        });
        return rest;
    }
    mergeChildren(index) {
        if (this.hasParent && this.children.length === 0) {
            return this.remove();
        }
        let newIndex = index;
        for (let i = 0; i < this.children.length; i += 1) {
            const child = this.children[i];
            const prev = i > 0 ? this.children[i - 1] : undefined;
            if (child.hasChildren && child.automerge) {
                if (prev && prev.hasChildren && child.arena === prev.arena) {
                    prev.insertChildren(child.children, prev.children.length);
                }
                else if (child.children.length !== 0) {
                    // eslint-disable-next-line no-continue
                    continue;
                }
                this.children.splice(i, 1);
                if (i >= newIndex) {
                    newIndex -= 1;
                }
                i -= 1;
            }
        }
        return {
            node: this,
            offset: newIndex,
        };
    }
    cutChildren(start, length) {
        let result = [];
        if (!this.arena.protected) {
            if (length === undefined) {
                result = this.children.splice(start);
            }
            else {
                result = this.children.splice(start, length);
            }
            // this.mergeChildren(start);
        }
        return result;
    }
    removeChildren(start, length) {
        this.cutChildren(start, length);
        // this.mergeChildren(start);
    }
    resetCache() {
        this.cache = undefined;
    }
}
//# sourceMappingURL=AbstractParentNode.js.map