"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 _ = require("lodash");
var typescript_angular_utilities_1 = require("typescript-angular-utilities");
var __transform = typescript_angular_utilities_1.services.transform;
var sortDirection_1 = require("../sortDirection");
var mergeSort_service_1 = require("../mergeSort/mergeSort.service");
var Sorter = (function () {
    function Sorter(mergeSort, transformService) {
        this.mergeSort = mergeSort;
        this.transformService = transformService;
    }
    Sorter.prototype.sort = function (data, sort) {
        var _this = this;
        if (!sort || !data) {
            return data;
        }
        if (_.isArray(sort)) {
            var reverseSorts = _.clone(sort);
            reverseSorts.reverse();
            return _.reduce(reverseSorts, function (sortedData, nextSort) {
                return _this.singleSort(sortedData, nextSort);
            }, data);
        }
        return this.singleSort(data, sort);
    };
    Sorter.prototype.singleSort = function (data, sort) {
        var compareFunction = this.buildSortFunction(sort);
        return this.mergeSort.sort(data, compareFunction);
    };
    Sorter.prototype.buildSortFunction = function (sort) {
        var _this = this;
        return function (a, b) {
            if (sort.direction === sortDirection_1.SortDirection.none) {
                return typescript_angular_utilities_1.types.CompareResult.equal;
            }
            var valueOfA = _this.transformService.getValue(a, sort.column.getValue);
            var valueOfB = _this.transformService.getValue(b, sort.column.getValue);
            var greaterResult = typescript_angular_utilities_1.types.CompareResult.greater;
            var lessResult = typescript_angular_utilities_1.types.CompareResult.less;
            var descendingSort = (sort.direction === sortDirection_1.SortDirection.descending);
            var flip = sort.column.flipSort;
            // Exclusive OR... if flipping a descending sort, you get an ascending sort
            if ((descendingSort || flip) && !(descendingSort && flip)) {
                greaterResult = typescript_angular_utilities_1.types.CompareResult.less;
                lessResult = typescript_angular_utilities_1.types.CompareResult.greater;
            }
            return valueOfA > valueOfB
                ? greaterResult
                : (valueOfA < valueOfB ? lessResult : typescript_angular_utilities_1.types.CompareResult.equal);
        };
    };
    return Sorter;
}());
Sorter = __decorate([
    core_1.Injectable(),
    __metadata("design:paramtypes", [mergeSort_service_1.MergeSort, __transform.TransformService])
], Sorter);
exports.Sorter = Sorter;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic29ydGVyLnNlcnZpY2UuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJzb3J0ZXIuc2VydmljZS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7Ozs7Ozs7O0FBQUEsc0NBQTJDO0FBQzNDLDBCQUE0QjtBQUU1Qiw2RUFBK0Q7QUFDL0QsSUFBTyxXQUFXLEdBQUcsdUNBQVEsQ0FBQyxTQUFTLENBQUM7QUFHeEMsa0RBQWlEO0FBQ2pELG9FQUF1RTtBQU92RSxJQUFhLE1BQU07SUFLbEIsZ0JBQVksU0FBb0IsRUFDNUIsZ0JBQThDO1FBQ2pELElBQUksQ0FBQyxTQUFTLEdBQUcsU0FBUyxDQUFDO1FBQzNCLElBQUksQ0FBQyxnQkFBZ0IsR0FBRyxnQkFBZ0IsQ0FBQztJQUMxQyxDQUFDO0lBRUQscUJBQUksR0FBSixVQUFnQixJQUFpQixFQUFFLElBQXFCO1FBQXhELGlCQWVDO1FBZEEsRUFBRSxDQUFDLENBQUMsQ0FBQyxJQUFJLElBQUksQ0FBQyxJQUFJLENBQUMsQ0FBQyxDQUFDO1lBQ3BCLE1BQU0sQ0FBQyxJQUFJLENBQUM7UUFDYixDQUFDO1FBRUQsRUFBRSxDQUFDLENBQUMsQ0FBQyxDQ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