"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 empty_1 = require("../fn/empty");
var common_1 = require("./common");
/**
 * Creates a grouped [[AsyncIterable]] for the correlated elements between an outer [[AsyncQueryable]] object and an inner [[AsyncQueryable]] object.
 *
 * @param outer An [[AsyncQueryable]] object.
 * @param inner An [[AsyncQueryable]] object.
 * @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 groupJoinAsync(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 AsyncGroupJoinIterable(internal_1.ToPossiblyAsyncIterable(outer), internal_1.ToPossiblyAsyncIterable(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer);
}
exports.groupJoinAsync = groupJoinAsync;
var AsyncGroupJoinIterable = /** @class */ (function () {
    function AsyncGroupJoinIterable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    AsyncGroupJoinIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var e_1, _a, outerKeySelector, resultSelector, map, _b, _c, outerElement, outerKey, innerElements, e_1_1;
            return tslib_1.__generator(this, function (_d) {
                switch (_d.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 = _d.sent();
                        _d.label = 2;
                    case 2:
                        _d.trys.push([2, 9, 10, 15]);
                        _b = tslib_1.__asyncValues(this._outer);
                        _d.label = 3;
                    case 3: return [4 /*yield*/, tslib_1.__await(_b.next())];
                    case 4:
                        if (!(_c = _d.sent(), !_c.done)) return [3 /*break*/, 8];
                        outerElement = _c.value;
                        outerKey = outerKeySelector(outerElement);
                        innerElements = map.get(outerKey) || empty_1.empty();
                        return [4 /*yield*/, tslib_1.__await(resultSelector(outerElement, innerElements))];
                    case 5: return [4 /*yield*/, _d.sent()];
                    case 6:
                        _d.sent();
                        _d.label = 7;
                    case 7: return [3 /*break*/, 3];
                    case 8: return [3 /*break*/, 15];
                    case 9:
                        e_1_1 = _d.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 15];
                    case 10:
                        _d.trys.push([10, , 13, 14]);
                        if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 12];
                        return [4 /*yield*/, tslib_1.__await(_a.call(_b))];
                    case 11:
                        _d.sent();
                        _d.label = 12;
                    case 12: return [3 /*break*/, 14];
                    case 13:
                        if (e_1) throw e_1.error;
                        return [7 /*endfinally*/];
                    case 14: return [7 /*endfinally*/];
                    case 15: return [2 /*return*/];
                }
            });
        });
    };
    AsyncGroupJoinIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncGroupJoinIterable")
    ], AsyncGroupJoinIterable);
    return AsyncGroupJoinIterable;
}());
//# sourceMappingURL=groupJoinAsync.js.map