"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 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;
var AsyncJoinIterable = /** @class */ (function () {
    function AsyncJoinIterable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    AsyncJoinIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var e_1, _a, e_2, _b, outerKeySelector, resultSelector, map, _c, _d, outerElement, outerKey, innerElements, innerElements_1, innerElements_1_1, innerElement, e_1_1, e_2_1;
            return tslib_1.__generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        outerKeySelector = this._outerKeySelector;
                        resultSelector = this._resultSelector;
                        return [4 /*yield*/, tslib_1.__await(internal_1.CreateGroupingsAsync(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer))];
                    case 1:
                        map = _e.sent();
                        _e.label = 2;
                    case 2:
                        _e.trys.push([2, 15, 16, 21]);
                        _c = tslib_1.__asyncValues(this._outer);
                        _e.label = 3;
                    case 3: return [4 /*yield*/, tslib_1.__await(_c.next())];
                    case 4:
                        if (!(_d = _e.sent(), !_d.done)) return [3 /*break*/, 14];
                        outerElement = _d.value;
                        outerKey = outerKeySelector(outerElement);
                        innerElements = map.get(outerKey);
                        if (!(innerElements != undefined)) return [3 /*break*/, 13];
                        _e.label = 5;
                    case 5:
                        _e.trys.push([5, 11, 12, 13]);
                        innerElements_1 = tslib_1.__values(innerElements), innerElements_1_1 = innerElements_1.next();
                        _e.label = 6;
                    case 6:
                        if (!!innerElements_1_1.done) return [3 /*break*/, 10];
                        innerElement = innerElements_1_1.value;
                        return [4 /*yield*/, tslib_1.__await(resultSelector(outerElement, innerElement))];
                    case 7: return [4 /*yield*/, _e.sent()];
                    case 8:
                        _e.sent();
                        _e.label = 9;
                    case 9:
                        innerElements_1_1 = innerElements_1.next();
                        return [3 /*break*/, 6];
                    case 10: return [3 /*break*/, 13];
                    case 11:
                        e_1_1 = _e.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 13];
                    case 12:
                        try {
                            if (innerElements_1_1 && !innerElements_1_1.done && (_a = innerElements_1.return)) _a.call(innerElements_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7 /*endfinally*/];
                    case 13: return [3 /*break*/, 3];
                    case 14: return [3 /*break*/, 21];
                    case 15:
                        e_2_1 = _e.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 21];
                    case 16:
                        _e.trys.push([16, , 19, 20]);
                        if (!(_d && !_d.done && (_b = _c.return))) return [3 /*break*/, 18];
                        return [4 /*yield*/, tslib_1.__await(_b.call(_c))];
                    case 17:
                        _e.sent();
                        _e.label = 18;
                    case 18: return [3 /*break*/, 20];
                    case 19:
                        if (e_2) throw e_2.error;
                        return [7 /*endfinally*/];
                    case 20: return [7 /*endfinally*/];
                    case 21: return [2 /*return*/];
                }
            });
        });
    };
    AsyncJoinIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncJoinIterable")
    ], AsyncJoinIterable);
    return AsyncJoinIterable;
}());
//# sourceMappingURL=joinAsync.js.map