"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 toArrayAsync_1 = require("./toArrayAsync");
var equatable_1 = require("equatable");
function orderByAsync(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.mustBeAsyncQueryable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(new AsyncOrderByIterable(internal_1.ToPossiblyAsyncIterable(source), keySelector, keyComparer, /*descending*/ false), source);
}
exports.orderByAsync = orderByAsync;
function orderByDescendingAsync(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.mustBeAsyncQueryable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(new AsyncOrderByIterable(internal_1.ToPossiblyAsyncIterable(source), keySelector, keyComparer, /*descending*/ true), source);
}
exports.orderByDescendingAsync = orderByDescendingAsync;
function thenByAsync(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.mustBePossiblyAsyncOrderedIterable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(internal_1.ThenByAsync(internal_1.ToAsyncOrderedIterable(source), keySelector, keyComparer, /*descending*/ false), source);
}
exports.thenByAsync = thenByAsync;
function thenByDescendingAsync(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.mustBePossiblyAsyncOrderedIterable(source, "source");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeComparer(keyComparer, "keyComparer");
    return internal_1.FlowHierarchy(internal_1.ThenByAsync(internal_1.ToAsyncOrderedIterable(source), keySelector, keyComparer, /*descending*/ true), source);
}
exports.thenByDescendingAsync = thenByDescendingAsync;
var AsyncOrderByIterable = /** @class */ (function () {
    function AsyncOrderByIterable(source, keySelector, keyComparer, descending, parent) {
        this._source = source;
        this._keySelector = keySelector;
        this._keyComparer = keyComparer;
        this._descending = descending;
        this._parent = parent;
    }
    AsyncOrderByIterable_1 = AsyncOrderByIterable;
    AsyncOrderByIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            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;
                        return [4 /*yield*/, tslib_1.__await(toArrayAsync_1.toArrayAsync(source))];
                    case 1:
                        array = _b.sent();
                        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 = 2;
                    case 2:
                        _b.trys.push([2, 8, 9, 10]);
                        indices_1 = tslib_1.__values(indices), indices_1_1 = indices_1.next();
                        _b.label = 3;
                    case 3:
                        if (!!indices_1_1.done) return [3 /*break*/, 7];
                        index = indices_1_1.value;
                        return [4 /*yield*/, tslib_1.__await(array[index])];
                    case 4: return [4 /*yield*/, _b.sent()];
                    case 5:
                        _b.sent();
                        _b.label = 6;
                    case 6:
                        indices_1_1 = indices_1.next();
                        return [3 /*break*/, 3];
                    case 7: return [3 /*break*/, 10];
                    case 8:
                        e_1_1 = _b.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 10];
                    case 9:
                        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 10: return [2 /*return*/];
                }
            });
        });
    };
    AsyncOrderByIterable.prototype[types_1.AsyncOrderedIterable.thenByAsync] = 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 AsyncOrderByIterable_1(this._source, keySelector, keyComparer, descending, this);
    };
    AsyncOrderByIterable.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 AsyncOrderByIterable_1;
    AsyncOrderByIterable = AsyncOrderByIterable_1 = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncOrderByIterable")
    ], AsyncOrderByIterable);
    return AsyncOrderByIterable;
}());
//# sourceMappingURL=orderByAsync.js.map