define(["require", "exports", "../Utils"], function (require, exports, Utils_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MINIMUM_QUERY_LENGTH = 2;
    var INITIAL_PAGE_COUNTER = 1;
    var TAB_LABEL_IDENTIFIER_PREFIX = 'search.component.tab.label.';
    var SearchController = /** @class */ (function () {
        function SearchController(SearchService, $element, $timeout) {
            this.SearchService = SearchService;
            this.$element = $element;
            this.$timeout = $timeout;
            this.searchActive = false;
            this.records = [];
            this.searching = false;
            this.numPages = 0;
            this.currentPageCounter = 0;
            this.attributes = [];
            this.selectedAttribute = {
                title: 'all',
                value: ''
            };
            this.filterObject = {
                type: this.selectedAttribute.value
            };
            this.data = [];
            this.subscribeObservables();
        }
        SearchController.prototype.$onInit = function () {
            var _this = this;
            this.inputElement = this.$element.find('#search')[0];
            this.inputElement.addEventListener('keyup', function (e) {
                if (e.keyCode === 13) {
                    _this.inputElement.blur();
                }
                else if (e.keyCode === 27) {
                    _this.closeActiveState();
                }
            });
        };
        SearchController.prototype.subscribeObservables = function () {
            var _this = this;
            this.SearchService.get().subscribe(function (data) {
                if (data) {
                    _this.data = data;
                    if (_this.data['info'].page.current_page === 1) {
                        _this.resetRecords();
                        if (_this.data['info'].page.num_pages > 1) {
                            _this.viewObserver();
                        }
                    }
                    if (_this.query.length > MINIMUM_QUERY_LENGTH) {
                        _this.records = _this.records.concat(_this.data['records'].page);
                        _this.attributes = _this.getAttributes(_this.records);
                        _this.numPages = _this.data['info'].page.num_pages;
                        _this.currentPageCounter = _this.data['info'].page.current_page;
                        _this.searching = false;
                    }
                }
            });
        };
        SearchController.prototype.resetRecords = function () {
            this.records = [];
            this.currentPageCounter = INITIAL_PAGE_COUNTER;
        };
        SearchController.prototype.queryChangeHandler = function () {
            if (this.query.length > MINIMUM_QUERY_LENGTH) {
                this.searching = true;
                this.SearchService.updateQuery(this.query);
            }
            else {
                this.resetRecords();
            }
        };
        SearchController.prototype.getAttributes = function (list) {
            var attributes = this.attributes.map(function (item) { return item.value; });
            for (var _i = 0, list_1 = list; _i < list_1.length; _i++) {
                var item = list_1[_i];
                if (attributes.indexOf(item.type) === -1) {
                    attributes.push(item.type);
                }
            }
            var filterObjects = attributes.map(function (item) {
                var title = (item === '') ? 'all' : item;
                return {
                    title: TAB_LABEL_IDENTIFIER_PREFIX + title,
                    value: item
                };
            });
            return filterObjects;
        };
        SearchController.prototype.setSelectedAttribute = function (attribute) {
            this.selectedAttribute = attribute;
            this.filterObject.type = this.selectedAttribute.value;
        };
        SearchController.prototype.toggleActiveState = function () {
            this.inputElement.focus();
            this.searchActive = !this.searchActive;
            Utils_1.Utils.toggleNoScrollBodyState();
        };
        SearchController.prototype.closeActiveState = function () {
            var _this = this;
            this.inputElement.blur();
            this.$timeout(function () {
                _this.searchActive = false;
            });
            Utils_1.Utils.toggleNoScrollBodyState();
        };
        SearchController.prototype.viewObserver = function () {
            var _this = this;
            var updateView = document.querySelector('search-component .backdrop');
            var updateViewTrigger = document.querySelector('search-component .js-update-view-trigger');
            var io;
            var onChange = function (changes) {
                changes.forEach(function (change) {
                    if (change.isIntersecting) {
                        _this.updateViewHandler();
                        if (_this.currentPageCounter === _this.numPages) {
                            io.unobserve(change.target);
                        }
                    }
                }, _this);
            };
            var observerOptions = {
                root: updateView,
                rootMargin: '0px 0px 250px 0px',
                threshold: [0.0]
            };
            io = new IntersectionObserver(onChange, observerOptions);
            io.observe(updateViewTrigger);
        };
        SearchController.prototype.updateViewHandler = function () {
            if (this.currentPageCounter !== this.numPages) {
                this.currentPageCounter++;
            }
            if (this.currentPageCounter < this.numPages) {
                this.SearchService.getData(this.query, this.currentPageCounter);
            }
            else {
                this.currentPageCounter = this.numPages;
            }
        };
        return SearchController;
    }());
    exports.SearchController = SearchController;
});
//# sourceMappingURL=search.controller.js.map