"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" */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var fn = require("./fn");
var internal_1 = require("./internal");
var types_1 = require("./types");
var query_1 = require("./query");
/**
 * Creates an `AsyncHierarchyQuery` from an [[AsyncQueryable]] source.
 *
 * @param source An [[AsyncQueryable]] object.
 * @param hierarchy A `HierarchyProvider` object.
 */
function fromAsync(source, hierarchy) {
    if (hierarchy)
        source = internal_1.MakeAsyncHierarchyIterable(source, hierarchy);
    return internal_1.IsPossiblyAsyncOrderedHierarchyIterable(source) ? new AsyncOrderedHierarchyQuery(source) :
        internal_1.IsPossiblyAsyncHierarchyIterable(source) ? new AsyncHierarchyQuery(source) :
            internal_1.IsPossiblyAsyncOrderedIterable(source) ? new AsyncOrderedQuery(source) :
                new AsyncQuery(source);
}
exports.fromAsync = fromAsync;
/**
 * An [[AsyncQuery]] represents a series of operations that act upon an [[AsyncIterable]], [[Iterable]]
 * or `ArrayLike`. Evaluation of these operations is deferred until the either a scalar value is
 * requested from the [[AsyncQuery]] or the [[AsyncQuery]] is iterated.
 */
var AsyncQuery = /** @class */ (function () {
    /**
     * Creates an [[AsyncQuery]] from an [[AsyncQueryable]] source.
     *
     * @param source An [[AsyncQueryable]] object.
     */
    function AsyncQuery(source) {
        internal_1.assert.mustBeAsyncQueryable(source, "source");
        this._source = source instanceof AsyncQuery
            ? source._source
            : source;
    }
    AsyncQuery.prototype[Symbol.asyncIterator] = function () {
        return internal_1.GetAsyncIterator(internal_1.ToPossiblyAsyncIterable(internal_1.GetAsyncSource(this)));
    };
    AsyncQuery.from = function (source, hierarchy) {
        return fromAsync(source, hierarchy);
    };
    AsyncQuery.of = function () {
        return fromAsync(arguments);
    };
    /**
     * Creates an [[AsyncQuery]] with no elements.
     * @category Query
     */
    AsyncQuery.empty = function () {
        // TODO: Consider contextual typing here?
        // return fromAsync(fn.emptyAsync());
        return fromAsync(fn.emptyAsync());
    };
    /**
     * Creates an [[AsyncQuery]] over a single element.
     *
     * @param value The only element for the `Query`.
     * @category Query
     */
    AsyncQuery.once = function (value) {
        return fromAsync(fn.onceAsync(value));
    };
    /**
     * Creates an [[AsyncQuery]] for a value repeated a provided number of times.
     *
     * @param value The value for each element of the `Query`.
     * @param count The number of times to repeat the value.
     * @category Query
     */
    AsyncQuery.repeat = function (value, count) {
        return fromAsync(fn.repeatAsync(value, count));
    };
    /**
     * Creates an [[AsyncQuery]] that repeats the provided value forever.
     *
     * @param value The value for each element of the `Query`.
     * @category Query
     */
    AsyncQuery.continuous = function (value) {
        return fromAsync(fn.continuousAsync(value));
    };
    /**
     * Creates an [[AsyncQuery]] whose values are provided by a callback executed a provided number of
     * times.
     *
     * @param count The number of times to execute the callback.
     * @param generator The callback to execute.
     * @category Query
     */
    AsyncQuery.generate = function (count, generator) {
        return fromAsync(fn.generateAsync(count, generator));
    };
    /**
     * Creates an `AsyncHierarchyQuery` from a root node and a `HierarchyProvider`.
     *
     * @param root The root node of the hierarchy.
     * @param hierarchy A `HierarchyProvider` object.
     * @category Query
     */
    AsyncQuery.hierarchy = function (root, hierarchy) {
        return fromAsync(fn.hierarchyAsync(root, hierarchy));
    };
    /**
     * Creates an [[AsyncQuery]] that, when iterated, consumes the provided `AsyncIterator`.
     *
     * @param iterator An `AsyncIterator` object.
     * @category Query
     */
    AsyncQuery.consume = function (iterator, options) {
        return fromAsync(fn.consumeAsync(iterator, options));
    };
    /**
     * Creates an [[AsyncQuery]] that iterates the elements from one of two sources based on the result of a
     * lazily evaluated condition.
     *
     * @param condition A callback used to choose a source.
     * @param thenQueryable The source to use when the callback evaluates to `true`.
     * @param elseQueryable The source to use when the callback evaluates to `false`.
     * @category Query
     */
    AsyncQuery.if = function (condition, thenQueryable, elseQueryable) {
        return fromAsync(fn.ifAsync(condition, thenQueryable, elseQueryable));
    };
    /**
     * Creates an [[AsyncQuery]] that iterates the elements from sources picked from a list based on the
     * result of a lazily evaluated choice.
     *
     * @param chooser A callback used to choose a source.
     * @param choices A list of sources
     * @param otherwise A default source to use when another choice could not be made.
     * @category Query
     */
    AsyncQuery.choose = function (chooser, choices, otherwise) {
        return fromAsync(fn.chooseAsync(chooser, choices, otherwise));
    };
    /**
     * Creates an [[AsyncQuery]] for the own property keys of an object.
     *
     * @param source An object.
     * @category Query
     */
    AsyncQuery.objectKeys = function (source) {
        return fromAsync(fn.objectKeysAsync(source));
    };
    /**
     * Creates an [[AsyncQuery]] for the own property values of an object.
     *
     * @param source An object.
     * @category Query
     */
    AsyncQuery.objectValues = function (source) {
        return fromAsync(fn.objectValuesAsync(source));
    };
    /**
     * Creates an [[AsyncQuery]] for the own property key-value pairs of an object.
     *
     * @param source An object.
     * @category Query
     */
    AsyncQuery.objectEntries = function (source) {
        return fromAsync(fn.objectEntriesAsync(source));
    };
    AsyncQuery.prototype.filter = function (predicate) {
        return fromAsync(fn.filterAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery where the selected key for each element matches the supplied predicate.
     *
     * @param keySelector A callback used to select the key for each element.
     * @param predicate A callback used to match each key.
     * @category Subquery
     */
    AsyncQuery.prototype.filterBy = function (keySelector, predicate) {
        return fromAsync(fn.filterByAsync(internal_1.GetAsyncSource(this), keySelector, predicate));
    };
    AsyncQuery.prototype.where = function (predicate) {
        return fromAsync(fn.whereAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery where the selected key for each element matches the supplied predicate.
     *
     * @param keySelector A callback used to select the key for each element.
     * @param predicate A callback used to match each key.
     * @category Subquery
     */
    AsyncQuery.prototype.whereBy = function (keySelector, predicate) {
        return fromAsync(fn.whereByAsync(internal_1.GetAsyncSource(this), keySelector, predicate));
    };
    AsyncQuery.prototype.filterDefined = function (keySelector) {
        return fromAsync(fn.filterDefinedAsync(internal_1.GetAsyncSource(this), keySelector));
    };
    AsyncQuery.prototype.whereDefined = function (keySelector) {
        return fromAsync(fn.whereDefinedAsync(internal_1.GetAsyncSource(this), keySelector));
    };
    /**
     * Creates a subquery by applying a callback to each element.
     *
     * @param selector A callback used to map each element.
     * @category Subquery
     */
    AsyncQuery.prototype.map = function (selector) {
        return fromAsync(fn.mapAsync(internal_1.GetAsyncSource(this), selector));
    };
    /**
     * Creates a subquery by applying a callback to each element.
     * This is an alias for `map`.
     *
     * @param selector A callback used to map each element.
     * @category Subquery
     */
    AsyncQuery.prototype.select = function (selector) {
        return fromAsync(fn.selectAsync(internal_1.GetAsyncSource(this), selector));
    };
    AsyncQuery.prototype.flatMap = function (projection, resultSelector) {
        return fromAsync(fn.flatMapAsync(internal_1.GetAsyncSource(this), projection, resultSelector));
    };
    AsyncQuery.prototype.selectMany = function (projection, resultSelector) {
        return fromAsync(fn.selectManyAsync(internal_1.GetAsyncSource(this), projection, resultSelector));
    };
    /**
     * Creates a subquery that iterates the results of recursively expanding the
     * elements of the source.
     *
     * @param projection A callback used to recusively expand each element.
     * @category Subquery
     */
    AsyncQuery.prototype.expand = function (projection) {
        return fromAsync(fn.expandAsync(internal_1.GetAsyncSource(this), projection));
    };
    AsyncQuery.prototype.do = function (callback) {
        return fromAsync(fn.doAsync(internal_1.GetAsyncSource(this), callback));
    };
    AsyncQuery.prototype.tap = function (callback) {
        return fromAsync(fn.tapAsync(internal_1.GetAsyncSource(this), callback));
    };
    /**
     * Pass the entire query to the provided callback, creating a new query from the result.
     *
     * @param callback A callback function.
     * @category Subquery
     */
    AsyncQuery.prototype.through = function (callback) {
        return fromAsync(fn.throughAsync(this, callback));
    };
    /**
     * Creates a subquery whose elements are in the reverse order.
     * @category Subquery
     */
    AsyncQuery.prototype.reverse = function () {
        return fromAsync(fn.reverseAsync(internal_1.GetAsyncSource(this)));
    };
    /**
     * Creates a subquery with every instance of the specified value removed.
     *
     * @param values The values to exclude.
     * @category Subquery
     */
    AsyncQuery.prototype.exclude = function () {
        var values = [];
        for (var _i = 0; _i < arguments.length; _i++) {
            values[_i] = arguments[_i];
        }
        return fromAsync(fn.excludeAsync.apply(fn, tslib_1.__spread([internal_1.GetAsyncSource(this)], values)));
    };
    /**
     * Creates a subquery containing all elements except the first elements up to the supplied
     * count.
     *
     * @param count The number of elements to skip.
     * @category Subquery
     */
    AsyncQuery.prototype.skip = function (count) {
        return fromAsync(fn.skipAsync(internal_1.GetAsyncSource(this), count));
    };
    /**
     * Creates a subquery containing all elements except the last elements up to the supplied
     * count.
     *
     * @param count The number of elements to skip.
     * @category Subquery
     */
    AsyncQuery.prototype.skipRight = function (count) {
        return fromAsync(fn.skipRightAsync(internal_1.GetAsyncSource(this), count));
    };
    /**
     * Creates a subquery containing all elements except the first elements that match
     * the supplied predicate.
     *
     * @param predicate A callback used to match each element.
     * @category Subquery
     */
    AsyncQuery.prototype.skipWhile = function (predicate) {
        return fromAsync(fn.skipWhileAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery containing all elements except the first elements that do not match
     * the supplied predicate.
     *
     * @param predicate A callback used to match each element.
     * @category Subquery
     */
    AsyncQuery.prototype.skipUntil = function (predicate) {
        return fromAsync(fn.skipUntilAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery containing the first elements up to the supplied
     * count.
     *
     * @param count The number of elements to take.
     * @category Subquery
     */
    AsyncQuery.prototype.take = function (count) {
        return fromAsync(fn.takeAsync(internal_1.GetAsyncSource(this), count));
    };
    /**
     * Creates a subquery containing the last elements up to the supplied
     * count.
     *
     * @param count The number of elements to take.
     * @category Subquery
     */
    AsyncQuery.prototype.takeRight = function (count) {
        return fromAsync(fn.takeRightAsync(internal_1.GetAsyncSource(this), count));
    };
    AsyncQuery.prototype.takeWhile = function (predicate) {
        return fromAsync(fn.takeWhileAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery containing the first elements that do not match the supplied predicate.
     *
     * @param predicate A callback used to match each element.
     * @category Subquery
     */
    AsyncQuery.prototype.takeUntil = function (predicate) {
        return fromAsync(fn.takeUntilAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncQuery.prototype.intersect = function (right, equaler) {
        return fromAsync(fn.intersectAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    AsyncQuery.prototype.intersectBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.intersectByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the set union of this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param right An [[AsyncQueryable]] object.
     * @param equaler An [[Equaler]] object used to compare equality.
     * @category Subquery
     */
    AsyncQuery.prototype.union = function (right, equaler) {
        return fromAsync(fn.unionAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    /**
     * Creates a subquery for the set union of this [[AsyncQuery]] and another [[AsyncQueryable]], where set identity is determined by the selected key.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param keySelector A callback used to select the key for each element.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.unionBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.unionByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the set difference (a.k.a. 'relative complement') between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param right An [[AsyncQueryable]] object.
     * @param equaler An [[Equaler]] object used to compare equality.
     * @category Subquery
     */
    AsyncQuery.prototype.except = function (right, equaler) {
        return fromAsync(fn.exceptAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    /**
     * Creates a subquery for the set difference between this and another [[AsyncQueryable]], where set identity is determined by the selected key.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param keySelector A callback used to select the key for each element.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.exceptBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.exceptByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the set difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * This is an alias for `except`.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param equaler An [[Equaler]] object used to compare equality.
     * @category Subquery
     */
    AsyncQuery.prototype.relativeComplement = function (right, equaler) {
        return fromAsync(fn.relativeComplementAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    /**
     * Creates a subquery for the set difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * This is an alias for `except`.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param keySelector A callback used to select the key for each element.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.relativeComplementBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.relativeComplementByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the symmetric difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param right An [[AsyncQueryable]] object.
     * @param equaler An [[Equaler]] object used to compare equality.
     * @category Subquery
     */
    AsyncQuery.prototype.symmetricDifference = function (right, equaler) {
        return fromAsync(fn.symmetricDifferenceAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    /**
     * Creates a subquery for the symmetric difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param right An [[AsyncQueryable]] object.
     * @param keySelector A callback used to select the key for each element.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.symmetricDifferenceBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.symmetricDifferenceByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the symmetric difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * This is an alias for `symmetricDifference`.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param equaler An [[Equaler]] object used to compare equality.
     * @category Subquery
     */
    AsyncQuery.prototype.xor = function (right, equaler) {
        return fromAsync(fn.xorAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler));
    };
    /**
     * Creates a subquery for the symmetric difference between this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * This is an alias for `symmetricDifference`.
     *
     * @param right An [[AsyncQueryable]] object.
     * @param keySelector A callback used to select the key for each element.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.xorBy = function (right, keySelector, keyEqualer) {
        return fromAsync(fn.xorByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery that concatenates this [[AsyncQuery]] with another [[AsyncQueryable]].
     *
     * @param right An [[AsyncQueryable]] object.
     * @category Subquery
     */
    AsyncQuery.prototype.concat = function (right) {
        return fromAsync(fn.concatAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right)));
    };
    /**
     * Creates a subquery for the distinct elements of this [[AsyncQuery]].
     *
     * @param equaler An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.distinct = function (equaler) {
        return fromAsync(fn.distinctAsync(internal_1.GetAsyncSource(this), equaler));
    };
    /**
     * Creates a subquery for the distinct elements of this [[AsyncQuery]].
     *
     * @param keySelector A callback used to select the key to determine uniqueness.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Subquery
     */
    AsyncQuery.prototype.distinctBy = function (keySelector, keyEqualer) {
        return fromAsync(fn.distinctByAsync(internal_1.GetAsyncSource(this), keySelector, keyEqualer));
    };
    /**
     * Creates a subquery for the elements of this [[AsyncQuery]] with the provided value appended to the end.
     *
     * @param value The value to append.
     * @category Subquery
     */
    AsyncQuery.prototype.append = function (value) {
        return fromAsync(fn.appendAsync(internal_1.GetAsyncSource(this), value));
    };
    /**
     * Creates a subquery for the elements of this [[AsyncQuery]] with the provided value prepended to the beginning.
     *
     * @param value The value to prepend.
     * @category Subquery
     */
    AsyncQuery.prototype.prepend = function (value) {
        return fromAsync(fn.prependAsync(internal_1.GetAsyncSource(this), value));
    };
    /**
     * Creates a subquery for the elements of this [[AsyncQuery]] with the provided range
     * patched into the results.
     *
     * @param start The offset at which to patch the range.
     * @param skipCount The number of elements to skip from start.
     * @param range The range to patch into the result.
     * @category Subquery
     */
    AsyncQuery.prototype.patch = function (start, skipCount, range) {
        return fromAsync(fn.patchAsync(internal_1.GetAsyncSource(this), start, skipCount, range && internal_1.GetAsyncSource(range)));
    };
    /**
     * Creates a subquery that contains the provided default value if this [[AsyncQuery]]
     * contains no elements.
     *
     * @param defaultValue The default value.
     * @category Subquery
     */
    AsyncQuery.prototype.defaultIfEmpty = function (defaultValue) {
        return fromAsync(fn.defaultIfEmptyAsync(internal_1.GetAsyncSource(this), defaultValue));
    };
    /**
     * Creates a subquery that splits this [[AsyncQuery]] into one or more pages.
     * While advancing from page to page is evaluated lazily, the elements of the page are
     * evaluated eagerly.
     *
     * @param pageSize The number of elements per page.
     * @category Subquery
     */
    AsyncQuery.prototype.pageBy = function (pageSize) {
        return fromAsync(fn.pageByAsync(internal_1.GetAsyncSource(this), pageSize));
    };
    AsyncQuery.prototype.spanMap = function (keySelector, elementSelector, spanSelector) {
        return fromAsync(fn.spanMapAsync(internal_1.GetAsyncSource(this), keySelector, elementSelector, wrapResultSelector(spanSelector)));
    };
    AsyncQuery.prototype.groupBy = function (keySelector, elementSelector, resultSelector, keyEqualer) {
        if (internal_1.IsEqualer(elementSelector)) {
            resultSelector = elementSelector;
            elementSelector = undefined;
        }
        if (internal_1.IsEqualer(resultSelector)) {
            keyEqualer = resultSelector;
            resultSelector = undefined;
        }
        return fromAsync(fn.groupByAsync(internal_1.GetAsyncSource(this), keySelector, elementSelector, wrapResultSelector(resultSelector), keyEqualer));
    };
    AsyncQuery.prototype.scan = function (accumulator, seed) {
        return fromAsync(arguments.length > 1
            ? fn.scanAsync(internal_1.GetAsyncSource(this), accumulator, seed)
            : fn.scanAsync(internal_1.GetAsyncSource(this), accumulator));
    };
    AsyncQuery.prototype.scanRight = function (accumulator, seed) {
        return fromAsync(arguments.length > 1
            ? fn.scanRightAsync(internal_1.GetAsyncSource(this), accumulator, seed)
            : fn.scanRightAsync(internal_1.GetAsyncSource(this), accumulator));
    };
    // #endregion Subquery
    // #region Join
    /**
     * Creates a grouped subquery for the correlated elements of this [[AsyncQuery]] and another [[AsyncQueryable]] object.
     *
     * @param inner A [[Queryable]] object.
     * @param outerKeySelector A callback used to select the key for an element in this [[AsyncQuery]].
     * @param innerKeySelector A callback used to select the key for an element in the other [[Queryable]] object.
     * @param resultSelector A callback used to select the result for the correlated elements.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Join
     */
    AsyncQuery.prototype.groupJoin = function (inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        return fromAsync(fn.groupJoinAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(inner), outerKeySelector, innerKeySelector, wrapResultSelector(resultSelector), keyEqualer));
    };
    /**
     * Creates a subquery for the correlated elements of this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param inner A [[Queryable]] object.
     * @param outerKeySelector A callback used to select the key for an element in this [[AsyncQuery]].
     * @param innerKeySelector A callback used to select the key for an element in the other Queryable.
     * @param resultSelector A callback used to select the result for the correlated elements.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Join
     */
    AsyncQuery.prototype.join = function (inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        return fromAsync(fn.joinAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer));
    };
    /**
     * Creates a subquery for the correlated elements of this [[AsyncQuery]] and another [[AsyncQueryable]].
     *
     * @param inner A [[Queryable]] object.
     * @param outerKeySelector A callback used to select the key for an element in this [[AsyncQuery]].
     * @param innerKeySelector A callback used to select the key for an element in the other Queryable.
     * @param resultSelector A callback used to select the result for the correlated elements.
     * @param keyEqualer An [[Equaler]] object used to compare key equality.
     * @category Join
     */
    AsyncQuery.prototype.fullJoin = function (inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        return fromAsync(fn.joinAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer));
    };
    AsyncQuery.prototype.zip = function (right, selector) {
        return fromAsync(fn.zipAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), selector));
    };
    // #endregion
    // #region Order
    /**
     * Creates an ordered subquery whose elements are sorted in ascending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparer An optional callback used to compare two keys.
     * @category Order
     */
    AsyncQuery.prototype.orderBy = function (keySelector, comparer) {
        return fromAsync(fn.orderByAsync(internal_1.GetAsyncSource(this), keySelector, comparer));
    };
    /**
     * Creates an ordered subquery whose elements are sorted in descending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparer An optional callback used to compare two keys.
     * @category Order
     */
    AsyncQuery.prototype.orderByDescending = function (keySelector, comparer) {
        return fromAsync(fn.orderByDescendingAsync(internal_1.GetAsyncSource(this), keySelector, comparer));
    };
    // #endregion Order
    // #region Hierarchy
    /**
     * Creates a HierarchyQuery using the provided HierarchyProvider.
     *
     * @param hierarchy A HierarchyProvider.
     * @category Hierarchy
     */
    AsyncQuery.prototype.toHierarchy = function (hierarchy) {
        return fromAsync(fn.toHierarchyAsync(internal_1.GetAsyncSource(this), hierarchy));
    };
    AsyncQuery.prototype.reduce = function (accumulator, seed, resultSelector) {
        return arguments.length > 1
            ? fn.reduceAsync(internal_1.GetAsyncSource(this), accumulator, seed, resultSelector)
            : fn.reduceAsync(internal_1.GetAsyncSource(this), accumulator);
    };
    AsyncQuery.prototype.reduceRight = function (accumulator, seed, resultSelector) {
        return arguments.length > 1
            ? fn.reduceAsync(internal_1.GetAsyncSource(this), accumulator, seed, resultSelector)
            : fn.reduceAsync(internal_1.GetAsyncSource(this), accumulator);
    };
    /**
     * Counts the number of elements in the Query, optionally filtering elements using the supplied
     * callback.
     *
     * @param predicate An optional callback used to match each element.
     * @category Scalar
     */
    AsyncQuery.prototype.count = function (predicate) {
        return fn.countAsync(internal_1.GetAsyncSource(this), predicate);
    };
    AsyncQuery.prototype.first = function (predicate) {
        return fn.firstAsync(internal_1.GetAsyncSource(this), predicate);
    };
    AsyncQuery.prototype.last = function (predicate) {
        return fn.lastAsync(internal_1.GetAsyncSource(this), predicate);
    };
    AsyncQuery.prototype.single = function (predicate) {
        return fn.singleAsync(internal_1.GetAsyncSource(this), predicate);
    };
    /**
     * Gets the minimum element in the query, optionally comparing elements using the supplied
     * callback.
     *
     * @param comparer An optional callback used to compare two elements.
     * @category Scalar
     */
    AsyncQuery.prototype.min = function (comparer) {
        return fn.minAsync(internal_1.GetAsyncSource(this), comparer);
    };
    /**
     * Gets the minimum element in the query, optionally comparing the keys of each element using the supplied callback.
     *
     * @param keySelector A callback used to choose the key to compare.
     * @param keyComparer An optional callback used to compare the keys.
     * @category Scalar
     */
    AsyncQuery.prototype.minBy = function (keySelector, keyComparer) {
        return fn.minByAsync(internal_1.GetAsyncSource(this), keySelector, keyComparer);
    };
    /**
     * Gets the maximum element in the query, optionally comparing elements using the supplied
     * callback.
     *
     * @param comparer An optional callback used to compare two elements.
     * @category Scalar
     */
    AsyncQuery.prototype.max = function (comparer) {
        return fn.maxAsync(internal_1.GetAsyncSource(this), comparer);
    };
    /**
     * Gets the maximum element in the query, optionally comparing the keys of each element using the supplied callback.
     *
     * @param keySelector A callback used to choose the key to compare.
     * @param keyComparer An optional callback used to compare the keys.
     * @category Scalar
     */
    AsyncQuery.prototype.maxBy = function (keySelector, keyComparer) {
        return fn.maxByAsync(internal_1.GetAsyncSource(this), keySelector, keyComparer);
    };
    AsyncQuery.prototype.sum = function (elementSelector) {
        return fn.sumAsync(internal_1.GetAsyncSource(this), elementSelector);
    };
    AsyncQuery.prototype.average = function (elementSelector) {
        return fn.averageAsync(internal_1.GetAsyncSource(this), elementSelector);
    };
    /**
     * Computes a scalar value indicating whether the Query contains any elements,
     * optionally filtering the elements using the supplied callback.
     *
     * @param predicate An optional callback used to match each element.
     * @category Scalar
     */
    AsyncQuery.prototype.some = function (predicate) {
        return fn.someAsync(internal_1.GetAsyncSource(this), predicate);
    };
    /**
     * Computes a scalar value indicating whether all elements of the Query
     * match the supplied callback.
     *
     * @param predicate A callback used to match each element.
     * @category Scalar
     */
    AsyncQuery.prototype.every = function (predicate) {
        return fn.everyAsync(internal_1.GetAsyncSource(this), predicate);
    };
    AsyncQuery.prototype.corresponds = function (right, equalityComparison) {
        return fn.correspondsAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equalityComparison);
    };
    AsyncQuery.prototype.correspondsBy = function (right, leftKeySelector, rightKeySelector, keyEqualer) {
        return fn.correspondsByAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), leftKeySelector, rightKeySelector, keyEqualer);
    };
    AsyncQuery.prototype.includes = function (value, equaler) {
        return fn.includesAsync(internal_1.GetAsyncSource(this), value, equaler);
    };
    AsyncQuery.prototype.includesSequence = function (right, equaler) {
        return fn.includesSequenceAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler);
    };
    AsyncQuery.prototype.startsWith = function (right, equaler) {
        return fn.startsWithAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler);
    };
    AsyncQuery.prototype.endsWith = function (right, equaler) {
        return fn.endsWithAsync(internal_1.GetAsyncSource(this), internal_1.GetAsyncSource(right), equaler);
    };
    /**
     * Finds the value in the Query at the provided offset. A negative offset starts from the
     * last element.
     *
     * @param offset An offset.
     * @category Scalar
     */
    AsyncQuery.prototype.elementAt = function (offset) {
        return fn.elementAtAsync(internal_1.GetAsyncSource(this), offset);
    };
    /**
     * Finds the value in the Query at the provided offset. A negative offset starts from the
     * last element.
     *
     * This is an alias for `elementAt`.
     *
     * @param offset An offset.
     * @category Scalar
     */
    AsyncQuery.prototype.nth = function (offset) {
        return fn.nthAsync(internal_1.GetAsyncSource(this), offset);
    };
    AsyncQuery.prototype.span = function (predicate) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a, first, rest;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, fn.spanAsync(internal_1.GetAsyncSource(this), predicate)];
                    case 1:
                        _a = tslib_1.__read.apply(void 0, [_b.sent(), 2]), first = _a[0], rest = _a[1];
                        return [2 /*return*/, [query_1.from(first), fromAsync(rest)]];
                }
            });
        });
    };
    /**
     * Creates a tuple whose first element is a subquery containing the first span of
     * elements that do not match the supplied predicate, and whose second element is a subquery
     * containing the remaining elements.
     *
     * The first subquery is eagerly evaluated, while the second subquery is lazily
     * evaluated.
     *
     * @param predicate The predicate used to match elements.
     * @category Scalar
     */
    AsyncQuery.prototype.break = function (predicate) {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a, first, rest;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0: return [4 /*yield*/, fn.breakAsync(internal_1.GetAsyncSource(this), predicate)];
                    case 1:
                        _a = tslib_1.__read.apply(void 0, [_b.sent(), 2]), first = _a[0], rest = _a[1];
                        return [2 /*return*/, [query_1.from(first), fromAsync(rest)]];
                }
            });
        });
    };
    AsyncQuery.prototype.forEach = function (callback) {
        return fn.forEachAsync(internal_1.GetAsyncSource(this), callback);
    };
    /**
     * Iterates over all of the elements in the query, ignoring the results.
     * @category Scalar
     */
    AsyncQuery.prototype.drain = function () {
        return fn.drainAsync(internal_1.GetAsyncSource(this));
    };
    /**
     * Eagerly evaluate the query, returning a new `Query`.
     * @category Scalar
     */
    AsyncQuery.prototype.eval = function () {
        return tslib_1.__awaiter(this, void 0, void 0, function () {
            var _a;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = query_1.from;
                        return [4 /*yield*/, fn.evalAsync(internal_1.GetAsyncSource(this))];
                    case 1: return [2 /*return*/, _a.apply(void 0, [_b.sent()])];
                }
            });
        });
    };
    AsyncQuery.prototype.unzip = function (partSelector) {
        return fn.unzipAsync(internal_1.GetAsyncSource(this), partSelector);
    };
    AsyncQuery.prototype.toArray = function (elementSelector) {
        return fn.toArrayAsync(internal_1.GetAsyncSource(this), elementSelector);
    };
    AsyncQuery.prototype.toSet = function (elementSelector, equaler) {
        return fn.toSetAsync(internal_1.GetAsyncSource(this), elementSelector, equaler);
    };
    AsyncQuery.prototype.toMap = function (keySelector, elementSelector, keyEqualer) {
        return fn.toMapAsync(internal_1.GetAsyncSource(this), keySelector, elementSelector, keyEqualer);
    };
    AsyncQuery.prototype.toLookup = function (keySelector, elementSelector, keyEqualer) {
        return fn.toLookupAsync(internal_1.GetAsyncSource(this), keySelector, elementSelector, keyEqualer);
    };
    AsyncQuery.prototype.toObject = function (prototype, keySelector, elementSelector, descriptorSelector) {
        return fn.toObjectAsync(internal_1.GetAsyncSource(this), prototype, keySelector, elementSelector, descriptorSelector);
    };
    /**
     * Writes each element to a destination array.
     *
     * @param dest The destination array.
     * @param start The offset into the array at which to start writing.
     * @param count The number of elements to write to the array.
     * @category Scalar
     */
    AsyncQuery.prototype.copyTo = function (dest, start, count) {
        return fn.copyToAsync(internal_1.GetAsyncSource(this), dest, start, count);
    };
    // #endregion Scalar
    /** @internal */ AsyncQuery.prototype[internal_1.AsyncQuerySource.source] = function () {
        return this._source;
    };
    return AsyncQuery;
}());
exports.AsyncQuery = AsyncQuery;
/**
 * Represents a sequence of hierarchically organized values.
 */
var AsyncHierarchyQuery = /** @class */ (function (_super) {
    tslib_1.__extends(AsyncHierarchyQuery, _super);
    function AsyncHierarchyQuery(source, hierarchy) {
        var _this = this;
        if (hierarchy) {
            internal_1.assert.mustBeAsyncQueryable(source, "source");
            internal_1.assert.mustBeHierarchyProvider(hierarchy, "hierarchy");
            source = internal_1.MakeAsyncHierarchyIterable(source, hierarchy);
        }
        else {
            internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
        }
        _this = _super.call(this, source) || this;
        return _this;
    }
    AsyncHierarchyQuery.prototype.root = function (predicate) {
        return fromAsync(fn.rootAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.ancestors = function (predicate) {
        return fromAsync(fn.ancestorsAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.ancestorsAndSelf = function (predicate) {
        return fromAsync(fn.ancestorsAndSelfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.descendants = function (predicate) {
        return fromAsync(fn.descendantsAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.descendantsAndSelf = function (predicate) {
        return fromAsync(fn.descendantsAndSelfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.self = function (predicate) {
        return fromAsync(fn.selfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.parents = function (predicate) {
        return fromAsync(fn.parentsAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.children = function (predicate) {
        return fromAsync(fn.childrenAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.siblings = function (predicate) {
        return fromAsync(fn.siblingsAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.siblingsAndSelf = function (predicate) {
        return fromAsync(fn.siblingsAndSelfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.siblingsBeforeSelf = function (predicate) {
        return fromAsync(fn.siblingsBeforeSelfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.siblingsAfterSelf = function (predicate) {
        return fromAsync(fn.siblingsAfterSelfAsync(internal_1.GetAsyncSource(this), predicate));
    };
    /**
     * Creates a subquery for the child of each element at the specified offset. A negative offset
     * starts from the last child.
     *
     * @param offset The offset for the child.
     * @category Hierarchy
     */
    AsyncHierarchyQuery.prototype.nthChild = function (offset) {
        return fromAsync(fn.nthChildAsync(internal_1.GetAsyncSource(this), offset));
    };
    AsyncHierarchyQuery.prototype.topMost = function (predicate) {
        return fromAsync(fn.topMostAsync(internal_1.GetAsyncSource(this), predicate));
    };
    AsyncHierarchyQuery.prototype.bottomMost = function (predicate) {
        return fromAsync(fn.bottomMostAsync(internal_1.GetAsyncSource(this), predicate));
    };
    // #endregion Hierarchy
    AsyncHierarchyQuery.prototype[types_1.Hierarchical.hierarchy] = function () {
        return internal_1.GetHierarchy(internal_1.GetAsyncSource(this));
    };
    return AsyncHierarchyQuery;
}(AsyncQuery));
exports.AsyncHierarchyQuery = AsyncHierarchyQuery;
/**
 * Represents an ordered sequence of elements.
 */
var AsyncOrderedQuery = /** @class */ (function (_super) {
    tslib_1.__extends(AsyncOrderedQuery, _super);
    function AsyncOrderedQuery(source) {
        var _this = this;
        internal_1.assert.mustBePossiblyAsyncOrderedIterable(source, "source");
        _this = _super.call(this, internal_1.ToAsyncOrderedIterable(source)) || this;
        return _this;
    }
    // #region Order
    /**
     * Creates a subsequent ordered subquery whose elements are sorted in ascending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparer An optional callback used to compare two keys.
     * @category Order
     */
    AsyncOrderedQuery.prototype.thenBy = function (keySelector, comparer) {
        return fromAsync(fn.thenByAsync(internal_1.GetAsyncSource(this), keySelector, comparer));
    };
    /**
     * Creates a subsequent ordered subquery whose elements are sorted in descending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparer An optional callback used to compare two keys.
     * @category Order
     */
    AsyncOrderedQuery.prototype.thenByDescending = function (keySelector, comparer) {
        return fromAsync(fn.thenByDescendingAsync(internal_1.GetAsyncSource(this), keySelector, comparer));
    };
    // #endregion Order
    AsyncOrderedQuery.prototype[types_1.AsyncOrderedIterable.thenByAsync] = function (keySelector, comparer, descending) {
        return internal_1.ThenByAsync(internal_1.GetAsyncSource(this), keySelector, comparer, descending);
    };
    return AsyncOrderedQuery;
}(AsyncQuery));
exports.AsyncOrderedQuery = AsyncOrderedQuery;
/**
 * Represents an ordered sequence of hierarchically organized values.
 */
var AsyncOrderedHierarchyQuery = /** @class */ (function (_super) {
    tslib_1.__extends(AsyncOrderedHierarchyQuery, _super);
    function AsyncOrderedHierarchyQuery(source, hierarchy) {
        var _this = this;
        internal_1.assert.mustBePossiblyAsyncOrderedIterable(source, "source");
        if (hierarchy === undefined && internal_1.IsPossiblyAsyncHierarchyIterable(source)) {
            _this = _super.call(this, internal_1.ToAsyncOrderedHierarchyIterable(source)) || this;
        }
        else {
            _this = _super.call(this, internal_1.ToAsyncOrderedIterable(source), hierarchy) || this;
        }
        return _this;
    }
    // #region Order
    /**
     * Creates a subsequent ordered subquery whose elements are sorted in ascending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparison An optional callback used to compare two keys.
     * @category Order
     */
    AsyncOrderedHierarchyQuery.prototype.thenBy = function (keySelector, comparison) {
        return fromAsync(fn.thenByAsync(internal_1.GetAsyncSource(this), keySelector, comparison));
    };
    /**
     * Creates a subsequent ordered subquery whose elements are sorted in descending order by the provided key.
     *
     * @param keySelector A callback used to select the key for an element.
     * @param comparison An optional callback used to compare two keys.
     * @category Order
     */
    AsyncOrderedHierarchyQuery.prototype.thenByDescending = function (keySelector, comparison) {
        return fromAsync(fn.thenByDescendingAsync(internal_1.GetAsyncSource(this), keySelector, comparison));
    };
    // #endregion Order
    AsyncOrderedHierarchyQuery.prototype[types_1.AsyncOrderedIterable.thenByAsync] = function (keySelector, comparison, descending) {
        return internal_1.ThenByAsync(internal_1.GetAsyncSource(this), keySelector, comparison, descending);
    };
    return AsyncOrderedHierarchyQuery;
}(AsyncHierarchyQuery));
exports.AsyncOrderedHierarchyQuery = AsyncOrderedHierarchyQuery;
function wrapResultSelector(selector) {
    if (typeof selector === "function") {
        return function (inner, outer) { return selector(inner, query_1.from(outer)); };
    }
    return selector;
}
//# sourceMappingURL=asyncQuery.js.map