"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("./empty");
var common_1 = require("./common");
/**
 * Creates a grouped [[Iterable]] for the correlated elements between an outer [[Queryable]] object and an inner [[Queryable]] object.
 *
 * @param outer A [[Queryable]] object.
 * @param inner A [[Queryable]] 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 groupJoin(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
    internal_1.assert.mustBeQueryable(outer, "outer");
    internal_1.assert.mustBeQueryable(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 GroupJoinIterable(internal_1.ToIterable(outer), internal_1.ToIterable(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer);
}
exports.groupJoin = groupJoin;
var GroupJoinIterable = /** @class */ (function () {
    function GroupJoinIterable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    GroupJoinIterable.prototype[Symbol.iterator] = function () {
        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;
                    map = internal_1.CreateGroupings(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer);
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 6, 7, 8]);
                    _b = tslib_1.__values(this._outer), _c = _b.next();
                    _d.label = 2;
                case 2:
                    if (!!_c.done) return [3 /*break*/, 5];
                    outerElement = _c.value;
                    outerKey = outerKeySelector(outerElement);
                    innerElements = map.get(outerKey) || empty_1.empty();
                    return [4 /*yield*/, resultSelector(outerElement, innerElements)];
                case 3:
                    _d.sent();
                    _d.label = 4;
                case 4:
                    _c = _b.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _d.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    };
    GroupJoinIterable = tslib_1.__decorate([
        internal_1.ToStringTag("GroupJoinIterable")
    ], GroupJoinIterable);
    return GroupJoinIterable;
}());
//# sourceMappingURL=groupJoin.js.map