"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var core_1 = require("@angular/core");
var rxjs_1 = require("rxjs");
var lodash_1 = require("lodash");
var sort_1 = require("../sort");
exports.SortDirection = sort_1.SortDirection;
var sorter_service_1 = require("../sorter/sorter.service");
var SortManagerService = (function () {
    function SortManagerService(sorter) {
        this.sorter = sorter;
        this._sortList = new rxjs_1.BehaviorSubject(null);
    }
    SortManagerService.prototype.setup = function (sortList, getColumn, maxColumnSorts) {
        this.getColumn = getColumn;
        this.maxColumnSorts = maxColumnSorts;
        this._sortList.next(sortList);
    };
    Object.defineProperty(SortManagerService.prototype, "sortList$", {
        get: function () {
            return this._sortList.asObservable();
        },
        enumerable: true,
        configurable: true
    });
    Object.defineProperty(SortManagerService.prototype, "visualColumnSorting$", {
        get: function () {
            var _this = this;
            return this.sortList$.map(function (sorts) { return lodash_1.map(sorts, function (sort, index) {
                // Only first sort should have visible direction
                if (index === 0) {
                    _this.updateVisualSortIndicator(sort);
                }
                else {
                    _this.clearVisualSortIndicator(sort);
                }
                return sort;
            }); });
        },
        enumerable: true,
        configurable: true
    });
    SortManagerService.prototype.sort = function (data$) {
        var _this = this;
        return data$.combineLatest(this.sortList$).map(function (_a) {
            var data = _a[0], sorts = _a[1];
            return _this.sorter.sort(data, sorts);
        });
    };
    SortManagerService.prototype.updateSorts = function (column) {
        var sortList = this._sortList.getValue();
        var updatedSortList;
        var firstSort = sortList[0];
        // If column is already the primary sort, change the direction
        if (firstSort != null && firstSort.column === column) {
            updatedSortList = this.toggleFirstSort(firstSort, column.secondarySorts != null, sortList);
        }
        else {
            updatedSortList = this.setFirstSort(column, sortList);
        }
        firstSort = updatedSortList[0];
        this.updateVisualColumnSorting(updatedSortList);
        // If column has secondary sorts, wipe the sort order and just apply the secondary sorts
        if (firstSort != null && column.secondarySorts != null) {
            var secondarySorts = this.buildSecondarySorts(firstSort.direction, column.secondarySorts);
            updatedSortList = [firstSort].concat(secondarySorts);
        }
        else {
            // If not using column secondary sorts, limit the maximum number
            //  of sorts applied to the maximum number of sorts
            updatedSortList = lodash_1.take(updatedSortList, this.maxColumnSorts);
        }
        this._sortList.next(updatedSortList);
    };
    SortManagerService.prototype.toggleFirstSort = function (firstSort, hasSecondarySorts, sortList) {
        firstSort.direction = sort_1.SortDirection.toggle(firstSort.direction);
        // Clear sort
        if (firstSort.direction === sort_1.SortDirection.none) {
            this.clearVisualSortIndicator(firstSort);
            firstSort = null;
            // If the column has secondary sorts don't fall back to a
            //  secondary sort, instead just clear all sorts
            if (hasSecondarySorts) {
                return [];
            }
            else {
                return lodash_1.drop(sortList, 1);
            }
        }
        else {
            return sortList;
        }
    };
    SortManagerService.prototype.setFirstSort = function (column, sortList) {
        // Make column primary ascending sort
        // Remove any existing non-primary sorts on column
        var listWithSortRemoved = lodash_1.reject(sortList, function (sort) {
            return column === sort.column;
        });
        // Build ascending sort for column
        var newSort = {
            column: column,
            direction: sort_1.SortDirection.ascending,
        };
        return [newSort].concat(listWithSortRemoved);
    };
    SortManagerService.prototype.buildSecondarySorts = function (direction, secondarySorts) {
        var _this = this;
        var sortList = secondarySorts[sort_1.SortDirection.getFullName(direction)];
        return lodash_1.map(sortList, function (sort) {
            return {
                direction: sort.direction,
                column: _this.getColumn(sort.column),
            };
        });
    };
    SortManagerService.prototype.updateVisualColumnSorting = function (sorts) {
        var _this = this;
        lodash_1.each(sorts, function (sort, index) {
            // Only first sort should have visible direction
            if (index === 0) {
                _this.updateVisualSortIndicator(sort);
            }
            else {
                _this.clearVisualSortIndicator(sort);
            }
        });
    };
    SortManagerService.prototype.updateVisualSortIndicator = function (sort) {
        sort.column.sortDirection = sort.direction;
    };
    SortManagerService.prototype.clearVisualSortIndicator = function (sort) {
        sort.column.sortDirection = null;
    };
    return SortManagerService;
}());
SortManagerService = __decorate([
    core_1.Injectable(),
    __metadata("design:paramtypes", [sorter_service_1.Sorter])
], SortManagerService);
exports.SortManagerService = SortManagerService;
//# sourceMappingURL=data:application/json;base64, 