"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Renderables_1 = require("../renderable/Renderables");
/**
 * Abstract renderer class.
 */
var AbstractRenderer = /** @class */ (function () {
    function AbstractRenderer(content, suggest) {
        var _this = this;
        /**
         * @inheritDoc
         */
        this.appendContent = function (element) {
            _this.content.appendChild(element);
        };
        /**
         * @inheritDoc
         */
        this.appendSuggest = function (element) {
            _this.suggest.appendChild(element);
        };
        this.content = content;
        this.suggest = suggest;
    }
    /**
     * @inheritDoc
     */
    AbstractRenderer.prototype.render = function (message, containerType) {
        var spec = Object.assign(message, { channelId: this.channelId, interactionContentClassName: this.content.className });
        if (message["render"] !== undefined) {
            return this.renderElement(spec, containerType);
        }
        var renderable = this.getRenderable(spec);
        return this.renderElement(renderable, containerType);
    };
    // noinspection JSMethodCanBeStatic
    /**
     * Returns the html decoded message
     *
     * @param text to be decoded message
     */
    AbstractRenderer.prototype.decodeEntities = function (text) {
        var handler = document.createElement('div');
        handler.innerHTML = text
            .replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '')
            .replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
        return handler.textContent || "";
    };
    /**
     * Returns the element by evaluating the message type.
     *
     * @param message the message
     */
    // noinspection JSMethodCanBeStatic
    AbstractRenderer.prototype.getRenderable = function (message) {
        if (message.text) {
            message.text = this.decodeEntities(message.text);
        }
        var renderableClass = Renderables_1.default.resolve(message.type.toUpperCase());
        if (renderableClass === undefined) {
            console.error("unable to render element of type \"" + message.type + "\"");
        }
        return new renderableClass(message);
    };
    AbstractRenderer.prototype.init = function (channelId) {
        this.channelId = channelId;
    };
    return AbstractRenderer;
}());
exports.AbstractRenderer = AbstractRenderer;
//# sourceMappingURL=AbstractRenderer.js.map