import { TemplateResult, defaultTemplateProcessor } from 'lit-html';
export default class AbstractArena {
    constructor(options) {
        this.allowedAttributes = [];
        this.name = options.name;
        this.tag = options.tag;
        this.attributes = options.attributes;
        if (options.allowedAttributes) {
            this.allowedAttributes = options.allowedAttributes;
        }
        this.getPublicProcessor = options.getPublic;
    }
    getTemplate(children, id, attributes) {
        if (!this.tag) {
            return children;
        }
        const openTag = this.getOpenTag(Object.assign(Object.assign({}, attributes), { 'arena-id': id }));
        const closeTag = this.getCloseTag();
        const stringArray = [
            openTag,
            closeTag,
        ];
        // FIXME
        return new TemplateResult(stringArray, [children], 'html', defaultTemplateProcessor);
    }
    getOutputTemplate(children, attributes, sigle = false) {
        if (!this.tag) {
            return children || '';
        }
        const openTag = this.getOpenTag(attributes);
        const closeTag = this.getCloseTag();
        let content = '';
        if (children) {
            content = sigle ? children : `\n${children}\n`;
        }
        return `${openTag}${content}${closeTag}`;
    }
    getPublicHtml(children, attributes, node, frms) {
        if (this.getPublicProcessor) {
            return this.getPublicProcessor(node, frms, this.attributes);
        }
        const openTag = this.getOpenTag(attributes);
        const closeTag = this.getCloseTag();
        let content = '';
        if (children) {
            content = Array.isArray(children) ? children.join('\n') : `${children}`;
        }
        return `${openTag}${content}${closeTag}`;
    }
    getOpenTag(attributes) {
        if (!this.tag) {
            return '';
        }
        const attrs = this.getAttributesString('', attributes);
        const tag = this.tag.toLowerCase();
        return `<${tag.toLowerCase()}${attrs}>`;
    }
    getCloseTag() {
        if (!this.tag) {
            return '';
        }
        const tag = this.tag.toLowerCase();
        return `</${tag.toLowerCase()}>`;
    }
    getAttributesString(id, attributes) {
        let str = '';
        if (id) {
            str += ` arena-id="${id}"`;
        }
        this.attributes.forEach((attr) => {
            str += ` ${attr}`;
        });
        Object.entries(attributes).forEach(([name, value]) => {
            if (typeof value === 'boolean') {
                if (value) {
                    str += ` ${name}`;
                }
            }
            else {
                const escapedValue = value.toString()
                    .replace(/&/g, '&amp;')
                    .replace(/'/g, '&apos;')
                    .replace(/"/g, '&quot;')
                    .replace(/</g, '&lt;')
                    .replace(/>/g, '&gt;');
                str += ` ${name}="${escapedValue}"`;
            }
        });
        return str;
    }
}
//# sourceMappingURL=AbstractArena.js.map