"use strict";
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 __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var AbstractRenderer_1 = require("./AbstractRenderer");
var button_1 = require("../renderable/button");
var link_1 = require("../renderable/link");
var Defaults_1 = require("../support/Defaults");
var suggestion_1 = require("../renderable/suggestion");
var EventStream_1 = require("../event/EventStream");
var EventType_1 = require("../event/EventType");
/**
 * The classic renderer renders the AIOS messages in a classic top-down manner.
 */
var ClassicRenderer = /** @class */ (function (_super) {
    __extends(ClassicRenderer, _super);
    function ClassicRenderer(content, suggest) {
        var _this = _super.call(this, content || Defaults_1.Defaults.content(), suggest || Defaults_1.Defaults.suggest()) || this;
        /**
         * The bottom scroll strategy autoscrolls to the bottom of the content element.
         *
         * @param e
         */
        _this.bottomScrollStrategy = function (e) { return e.scrollTop = e.scrollHeight; };
        /**
         * The container scroll strategy autoscrolls to the last renderable with a .lto-container class.
         *
         * @param e
         */
        _this.containerScrollStrategy = function (e) {
            var list = e.querySelectorAll(".lto-container");
            if (!list || list.length == 0) {
                return;
            }
            var item = list.item(list.length - 1);
            item.scrollIntoView({ behavior: "smooth" });
        };
        /**
         * The scroll strategy is used to define the manner of scroll automation
         * after the rendering of a renderable.
         */
        _this.scrollStrategy = _this.bottomScrollStrategy;
        return _this;
    }
    ClassicRenderer.prototype.init = function (channelId) {
        _super.prototype.init.call(this, channelId);
        EventStream_1.default.addListener(EventType_1.EventType.withChannelId(EventType_1.EventType.CAROUSEL, channelId), this.handleCarousel.bind(this));
    };
    /**
     * @inheritDoc
     */
    ClassicRenderer.prototype.renderElement = function (renderable, containerType) {
        var _this = this;
        var array = [];
        var element = renderable.render(this, containerType !== undefined);
        array.push(element);
        if (containerType === undefined) {
            if (this.needsSeparator(renderable)) {
                var div = document.createElement('div');
                div.classList.add('lto-separator');
                array.push(div);
            }
        }
        setTimeout(function () {
            if (_this.content != null) {
                if (_this.scrollStrategy === "bottom") {
                    _this.bottomScrollStrategy(_this.content);
                }
                else if (_this.scrollStrategy === "container") {
                    _this.containerScrollStrategy(_this.content);
                }
                else {
                    _this.scrollStrategy(_this.content);
                }
            }
        }, 1);
        return array;
    };
    // noinspection JSMethodCanBeStatic
    ClassicRenderer.prototype.needsSeparator = function (renderable) {
        switch (renderable.constructor) {
            case button_1.Button:
                return false;
            case suggestion_1.Suggestion:
                return false;
            case link_1.Link:
                return false;
            default:
                return true;
        }
    };
    ClassicRenderer.prototype.handleCarousel = function (args) {
        var suggestions = this.suggest.querySelectorAll(".lto-suggestion");
        suggestions.forEach(function (suggestion) {
            suggestion.classList.remove("lto-hide");
            var value = suggestion.getAttribute("data-counter");
            if (value && (args[0] !== parseInt(value, 10))) {
                suggestion.classList.add("lto-hide");
            }
        });
    };
    return ClassicRenderer;
}(AbstractRenderer_1.AbstractRenderer));
exports.ClassicRenderer = ClassicRenderer;
//# sourceMappingURL=ClassicRenderer.js.map