"use strict";
/*!
  Copyright 2018 Ron Buckton (rbuckton@chronicles.org)

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */
/** @module "iterable-query/fn" */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var internal_1 = require("../internal");
var types_1 = require("../types");
var toArray_1 = require("./toArray");
var equatable_1 = require("equatable");
function orderBy(source, keySelector, keyComparer) {
    if (keyComparer === void 0) { keyComparer = equatable_1.Comparer.defaultComparer; }
    if (typeof keyComparer === "function")
        keyComparer = equatable_1.Comparer.create(keyComparer);
    internal_1.assert.mustBeQueryable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(new OrderByIterable(internal_1.ToIterable(source), keySelector, keyComparer, /*descending*/ false), source);
}
exports.orderBy = orderBy;
function orderByDescending(source, keySelector, keyComparer) {
    if (keyComparer === void 0) { keyComparer = equatable_1.Comparer.defaultComparer; }
    if (typeof keyComparer === "function")
        keyComparer = equatable_1.Comparer.create(keyComparer);
    internal_1.assert.mustBeQueryable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(new OrderByIterable(internal_1.ToIterable(source), keySelector, keyComparer, /*descending*/ true), source);
}
exports.orderByDescending = orderByDescending;
function thenBy(source, keySelector, keyComparer) {
    if (keyComparer === void 0) { keyComparer = equatable_1.Comparer.defaultComparer; }
    if (typeof keyComparer === "function")
        keyComparer = equatable_1.Comparer.create(keyComparer);
    internal_1.assert.mustBeOrderedIterable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(internal_1.ThenBy(source, keySelector, keyComparer, /*descending*/ false), source);
}
exports.thenBy = thenBy;
function thenByDescending(source, keySelector, keyComparer) {
    if (keyComparer === void 0) { keyComparer = equatable_1.Comparer.defaultComparer; }
    if (typeof keyComparer === "function")
        keyComparer = equatable_1.Comparer.create(keyComparer);
    internal_1.assert.mustBeOrderedIterable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(internal_1.ThenBy(source, keySelector, keyComparer, /*descending*/ true), source);
}
exports.thenByDescending = thenByDescending;
var OrderByIterable = /** @class */ (function () {
    function OrderByIterable(source, keySelector, keyComparer, descending, parent) {
        this._source = source;
        this._keySelector = keySelector;
        this._keyComparer = keyComparer;
        this._descending = descending;
        this._parent = parent;
    }
    OrderByIterable_1 = OrderByIterable;
    OrderByIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, source, array, sorter, len, indices, i, indices_1, indices_1_1, index, e_1_1;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    source = this._source;
                    array = toArray_1.toArray(source);
                    sorter = this._getSorter(array);
                    len = array.length;
                    indices = new Array(len);
                    for (i = 0; i < len; ++i) {
                        indices[i] = i;
                    }
                    indices.sort(sorter);
                    _b.label = 1;
                case 1:
                    _b.trys.push([1, 6, 7, 8]);
                    indices_1 = tslib_1.__values(indices), indices_1_1 = indices_1.next();
                    _b.label = 2;
                case 2:
                    if (!!indices_1_1.done) return [3 /*break*/, 5];
                    index = indices_1_1.value;
                    return [4 /*yield*/, array[index]];
                case 3:
                    _b.sent();
                    _b.label = 4;
                case 4:
                    indices_1_1 = indices_1.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _b.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (indices_1_1 && !indices_1_1.done && (_a = indices_1.return)) _a.call(indices_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    };
    OrderByIterable.prototype[types_1.OrderedIterable.thenBy] = function (keySelector, keyComparer, descending) {
        if (typeof keyComparer === "function")
            keyComparer = equatable_1.Comparer.create(keyComparer);
        internal_1.assert.mustBeFunction(keySelector, "keySelector");
        internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
        internal_1.assert.mustBeBoolean(descending, "descending");
        return new OrderByIterable_1(this._source, keySelector, keyComparer, descending, this);
    };
    OrderByIterable.prototype._getSorter = function (elements, next) {
        var keySelector = this._keySelector;
        var comparer = this._keyComparer;
        var descending = this._descending;
        var parent = this._parent;
        var keys = elements.map(keySelector);
        var sorter = function (x, y) {
            var result = comparer.compare(keys[x], keys[y]);
            if (result === 0) {
                return next ? next(x, y) : x - y;
            }
            return descending ? -result : result;
        };
        return parent ? parent._getSorter(elements, sorter) : sorter;
    };
    var OrderByIterable_1;
    OrderByIterable = OrderByIterable_1 = tslib_1.__decorate([
        internal_1.ToStringTag("OrderByIterable")
    ], OrderByIterable);
    return OrderByIterable;
}());
//# sourceMappingURL=orderBy.js.map