"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 });
const tslib_1 = require("tslib");
var OrderByIterable_1;
"use strict";
const internal_1 = require("../internal");
const types_1 = require("../types");
const toArray_1 = require("./toArray");
const equatable_1 = require("equatable");
function orderBy(source, keySelector, 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 = 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 = 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 = 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;
let OrderByIterable = OrderByIterable_1 = class OrderByIterable {
    constructor(source, keySelector, keyComparer, descending, parent) {
        this._source = source;
        this._keySelector = keySelector;
        this._keyComparer = keyComparer;
        this._descending = descending;
        this._parent = parent;
    }
    *[Symbol.iterator]() {
        const source = this._source;
        const array = toArray_1.toArray(source);
        const sorter = this._getSorter(array);
        const len = array.length;
        const indices = new Array(len);
        for (let i = 0; i < len; ++i) {
            indices[i] = i;
        }
        indices.sort(sorter);
        for (const index of indices) {
            yield array[index];
        }
    }
    [types_1.OrderedIterable.thenBy](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);
    }
    _getSorter(elements, next) {
        const keySelector = this._keySelector;
        const comparer = this._keyComparer;
        const descending = this._descending;
        const parent = this._parent;
        const keys = elements.map(keySelector);
        const sorter = (x, y) => {
            const 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;
    }
};
OrderByIterable = OrderByIterable_1 = tslib_1.__decorate([
    internal_1.ToStringTag("OrderByIterable")
], OrderByIterable);
//# sourceMappingURL=orderBy.js.map