var __extends = (this && this.__extends) || (function () {
    var 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 function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
define(["require", "exports", "../../carousel/carousel.abstract.controller", "../../ScreenSizeDetection"], function (require, exports, carousel_abstract_controller_1, ScreenSizeDetection_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var WidgetCategoryProductsController = /** @class */ (function (_super) {
        __extends(WidgetCategoryProductsController, _super);
        function WidgetCategoryProductsController(WidgetCategoryProductsService, $timeout, $element) {
            var _this = _super.call(this, $timeout) || this;
            _this.WidgetCategoryProductsService = WidgetCategoryProductsService;
            _this.$timeout = $timeout;
            _this.$element = $element;
            _this.productsRaw = [];
            return _this;
        }
        WidgetCategoryProductsController.prototype.subscribeScreenSize = function () {
            var _this = this;
            ScreenSizeDetection_1.default.get().subscribe(function (screensize) {
                _this.$timeout(function () {
                    _this.screensize = screensize;
                    _this.setMode();
                    _this.calculateHeight();
                });
            });
        };
        Object.defineProperty(WidgetCategoryProductsController.prototype, "categories", {
            get: function () {
                if (!this.staticCategories) {
                    return this.$element[0].parentNode.getAttribute('categories');
                }
                else {
                    return this.staticCategories;
                }
            },
            set: function (categories) {
                var staticCategoriesSetBefore = this.staticCategories;
                this.staticCategories = categories;
                if (staticCategoriesSetBefore) {
                    this.getData();
                }
                ;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(WidgetCategoryProductsController.prototype, "attribute", {
            get: function () {
                if (!this.staticAttribute) {
                    return this.$element[0].parentNode.getAttribute('attribute');
                }
                else {
                    return this.staticAttribute;
                }
            },
            set: function (attribute) {
                this.staticAttribute = attribute;
            },
            enumerable: true,
            configurable: true
        });
        Object.defineProperty(WidgetCategoryProductsController.prototype, "headline", {
            get: function () {
                if (!this.staticHeadline) {
                    return this.$element[0].parentNode.getAttribute('headline');
                }
                else {
                    return this.staticHeadline;
                }
            },
            set: function (attribute) {
                this.staticAttribute = attribute;
            },
            enumerable: true,
            configurable: true
        });
        WidgetCategoryProductsController.prototype.calculateHeight = function () {
            var rowsVisible = 2;
            var firstItem = this.listElement[0].querySelector('li');
            if (this.mode === 'carousel') {
                this.sliceList = 0;
            }
            else if (firstItem) {
                var singleWidth = parseInt(window.getComputedStyle(firstItem, null).getPropertyValue('max-width'), 10);
                var elementsPerRow = 100 / singleWidth;
                this.stepLength = elementsPerRow * rowsVisible;
                this.sliceList = this.stepLength;
            }
        };
        WidgetCategoryProductsController.prototype.setMode = function () {
            if (this.screensize === 'xs' || this.screensize === 'sm') {
                this.mode = 'carousel';
                this.$timeout(this.initCarousel.bind(this));
            }
            else {
                this.mode = 'tabs';
            }
        };
        WidgetCategoryProductsController.prototype.getData = function () {
            var _this = this;
            this.WidgetCategoryProductsService.get(this.categories).then(function (data) {
                data.attributeValues.unshift('all');
                _this.attributeValues = data.attributeValues;
                _this.selectedAttributeValue = _this.attributeValues[0];
                _this.productsRaw = data.products;
                _this.$timeout(function () {
                    _this.calculateHeight();
                });
            });
        };
        Object.defineProperty(WidgetCategoryProductsController.prototype, "products", {
            get: function () {
                var _this = this;
                var list;
                if (!this.selectedAttributeValue || this.selectedAttributeValue === 'all') {
                    list = this.productsRaw;
                }
                else {
                    list = this.productsRaw.filter(function (product) {
                        var result = product.custom_attributes.filter(function (attribute) {
                            if (attribute.attribute_code !== _this.attribute) {
                                return false;
                            }
                            if (attribute.value instanceof Array) {
                                return attribute.value.indexOf(_this.selectedAttributeValue) !== -1;
                            }
                            else {
                                return attribute.value === _this.selectedAttributeValue;
                            }
                        });
                        return result.length;
                    });
                }
                return (this.sliceList) ? list.slice(0, this.sliceList) : list;
            },
            enumerable: true,
            configurable: true
        });
        WidgetCategoryProductsController.prototype.toggleList = function () {
            if (this.sliceList) {
                this.sliceList = 0;
            }
            else {
                this.scrollContainer.scrollTo(0, this.$element[0].offsetTop);
                this.sliceList = this.stepLength;
            }
        };
        WidgetCategoryProductsController.prototype.getToggleButtonText = function () {
            if (this.sliceList) {
                return 'Alle Anzeigen';
            }
            else {
                return 'Weniger Anzeigen';
            }
        };
        WidgetCategoryProductsController.prototype.getToggleButtonIcon = function () {
            if (this.sliceList) {
                return 'plus';
            }
            else {
                return 'minus';
            }
        };
        WidgetCategoryProductsController.prototype.getAttribute = function (product, attribute_code) {
            var result = product.custom_attributes.find(function (item) {
                return item.attribute_code === attribute_code;
            });
            if (result) {
                return result.value;
            }
            else {
                return '';
            }
        };
        WidgetCategoryProductsController.prototype.setActive = function (attributeValue) {
            this.selectedAttributeValue = attributeValue;
        };
        WidgetCategoryProductsController.prototype.$onInit = function () {
            this.scrollContainer = document.querySelector('.styleguide-content');
            if (!this.scrollContainer) {
                this.scrollContainer = document.body;
            }
            if (this.url) {
                this.WidgetCategoryProductsService.setUrl(this.url);
            }
            this.WidgetCategoryProductsService.setAttribute(this.attribute);
            this.listElement = this.$element.find('.product-items');
            this.getData();
            this.subscribeScreenSize();
        };
        return WidgetCategoryProductsController;
    }(carousel_abstract_controller_1.CarouselAbstractController));
    exports.WidgetCategoryProductsController = WidgetCategoryProductsController;
});
//# sourceMappingURL=widget.category.products.controller.js.map