var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
import { ClassicRenderer } from './ClassicRenderer';
import EventStream from '../event/EventStream';
import { Overlay } from '../renderable/overlay';
import { Block } from '../renderable/block';
import { Container } from '../renderable/container';
import { EventType } from "../event/EventType";
import { Suggestion } from "../renderable/suggestion";
/**
 * The content centric renderer tries to maximize the time a content is visible by updating
 * the content if possible or displaying interrupting actions like intent cascading by overlaying the content.
 */
var ContentCentricRenderer = /** @class */ (function (_super) {
    __extends(ContentCentricRenderer, _super);
    function ContentCentricRenderer(content, suggest) {
        var _this = _super.call(this, content, suggest) || this;
        _this.qualifier = null;
        _this.behaviour = function (r, t) { return _this.defaultBehaviour(r, t); };
        return _this;
    }
    ContentCentricRenderer.prototype.init = function (channelId) {
        var _this = this;
        _super.prototype.init.call(this, channelId);
        EventStream.addListener(EventType.withChannelId(EventType.PUBLISH, channelId), function (e) {
            if (e[0].type === Suggestion.TYPE) {
                _this.behaviour = _this.suggestionBehaviour(_this.qualifier || "");
            }
        });
    };
    /**
     * @inheritDoc
     */
    ContentCentricRenderer.prototype.renderElement = function (renderable, containerType) {
        return this.behaviour(renderable, containerType);
    };
    ContentCentricRenderer.getQualifier = function (renderable) {
        return (renderable["spec"] !== undefined) ? renderable["spec"].qualifier : null;
    };
    ContentCentricRenderer.prototype.defaultBehaviour = function (renderable, containerType) {
        if (!containerType) {
            this.qualifier = ContentCentricRenderer.getQualifier(renderable) || this.qualifier;
        }
        return _super.prototype.renderElement.call(this, renderable, containerType);
    };
    ContentCentricRenderer.prototype.suggestionBehaviour = function (qualifier) {
        var _this = this;
        return function (renderable, containerType) {
            if (ContentCentricRenderer.getQualifier(renderable) === qualifier) {
                var containers = document.getElementsByClassName("lto-container");
                var containerZ = containers[containers.length - 1];
                if (containerZ.parentElement) {
                    containerZ.parentElement.removeChild(containerZ);
                }
                _this.behaviour = function (r, t) { return _this.defaultBehaviour(r, t); };
                return _super.prototype.renderElement.call(_this, renderable, containerType);
            }
            if (renderable instanceof Container) {
                return _super.prototype.renderElement.call(_this, new Overlay(renderable), new Block({ type: "block" }));
            }
            return _super.prototype.renderElement.call(_this, renderable, new Block({ type: "block" }));
        };
    };
    return ContentCentricRenderer;
}(ClassicRenderer));
export { ContentCentricRenderer };
//# sourceMappingURL=ContentCentricRenderer.js.map