"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");
const internal_1 = require("../internal");
const common_1 = require("./common");
/**
 * Creates an [[AsyncIterable]] for the correlated elements of two [[AsyncQueryable]] objects.
 *
 * @param outer An [[AsyncQueryable]].
 * @param inner An [[AsyncQueryable]].
 * @param outerKeySelector A callback used to select the key for an element in `outer`.
 * @param innerKeySelector A callback used to select the key for an element in `inner`.
 * @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
 */
function joinAsync(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
    internal_1.assert.mustBeAsyncQueryable(outer, "outer");
    internal_1.assert.mustBeAsyncQueryable(inner, "inner");
    internal_1.assert.mustBeFunction(outerKeySelector, "outerKeySelector");
    internal_1.assert.mustBeFunction(innerKeySelector, "innerKeySelector");
    internal_1.assert.mustBeFunction(resultSelector, "resultSelector");
    internal_1.assert.mustBeEqualerOrUndefined(keyEqualer, "keyEqualer");
    return new AsyncJoinIterable(internal_1.ToPossiblyAsyncIterable(outer), internal_1.ToPossiblyAsyncIterable(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer);
}
exports.joinAsync = joinAsync;
let AsyncJoinIterable = class AsyncJoinIterable {
    constructor(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    [Symbol.asyncIterator]() {
        return tslib_1.__asyncGenerator(this, arguments, function* _a() {
            var e_1, _a;
            const outerKeySelector = this._outerKeySelector;
            const resultSelector = this._resultSelector;
            const map = yield tslib_1.__await(internal_1.CreateGroupingsAsync(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer));
            try {
                for (var _b = tslib_1.__asyncValues(this._outer), _c; _c = yield tslib_1.__await(_b.next()), !_c.done;) {
                    const outerElement = _c.value;
                    const outerKey = outerKeySelector(outerElement);
                    const innerElements = map.get(outerKey);
                    if (innerElements != undefined) {
                        for (const innerElement of innerElements) {
                            yield yield tslib_1.__await(resultSelector(outerElement, innerElement));
                        }
                    }
                }
            }
            catch (e_1_1) { e_1 = { error: e_1_1 }; }
            finally {
                try {
                    if (_c && !_c.done && (_a = _b.return)) yield tslib_1.__await(_a.call(_b));
                }
                finally { if (e_1) throw e_1.error; }
            }
        });
    }
};
AsyncJoinIterable = tslib_1.__decorate([
    internal_1.ToStringTag("AsyncJoinIterable")
], AsyncJoinIterable);
//# sourceMappingURL=joinAsync.js.map