"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 [[Iterable]] for the correlated elements of two [[Queryable]] objects.
 *
 * @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 join(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 JoinIterable(internal_1.ToIterable(outer), internal_1.ToIterable(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer);
}
exports.join = join;
var JoinIterable = /** @class */ (function () {
    function JoinIterable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    JoinIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, e_2, _b, outerKeySelector, resultSelector, map, _c, _d, outerElement, outerKey, innerElements, innerElements_1, innerElements_1_1, innerElement, e_2_1, e_1_1;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    outerKeySelector = this._outerKeySelector;
                    resultSelector = this._resultSelector;
                    map = internal_1.CreateGroupings(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer);
                    _e.label = 1;
                case 1:
                    _e.trys.push([1, 12, 13, 14]);
                    _c = tslib_1.__values(this._outer), _d = _c.next();
                    _e.label = 2;
                case 2:
                    if (!!_d.done) return [3 /*break*/, 11];
                    outerElement = _d.value;
                    outerKey = outerKeySelector(outerElement);
                    innerElements = map.get(outerKey);
                    if (!(innerElements != undefined)) return [3 /*break*/, 10];
                    _e.label = 3;
                case 3:
                    _e.trys.push([3, 8, 9, 10]);
                    innerElements_1 = tslib_1.__values(innerElements), innerElements_1_1 = innerElements_1.next();
                    _e.label = 4;
                case 4:
                    if (!!innerElements_1_1.done) return [3 /*break*/, 7];
                    innerElement = innerElements_1_1.value;
                    return [4 /*yield*/, resultSelector(outerElement, innerElement)];
                case 5:
                    _e.sent();
                    _e.label = 6;
                case 6:
                    innerElements_1_1 = innerElements_1.next();
                    return [3 /*break*/, 4];
                case 7: return [3 /*break*/, 10];
                case 8:
                    e_2_1 = _e.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 10];
                case 9:
                    try {
                        if (innerElements_1_1 && !innerElements_1_1.done && (_b = innerElements_1.return)) _b.call(innerElements_1);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 10:
                    _d = _c.next();
                    return [3 /*break*/, 2];
                case 11: return [3 /*break*/, 14];
                case 12:
                    e_1_1 = _e.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 14];
                case 13:
                    try {
                        if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 14: return [2 /*return*/];
            }
        });
    };
    JoinIterable = tslib_1.__decorate([
        internal_1.ToStringTag("JoinIterable")
    ], JoinIterable);
    return JoinIterable;
}());
//# sourceMappingURL=join.js.map