"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 union_1 = require("./union");
var map_1 = require("./map");
var lookup_1 = require("../lookup");
var defaultIfEmpty_1 = require("./defaultIfEmpty");
var common_1 = require("./common");
/**
 * Creates an [[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 fullJoin(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 FullJoinIterable(internal_1.ToIterable(outer), internal_1.ToIterable(inner), outerKeySelector, innerKeySelector, resultSelector, keyEqualer);
}
exports.fullJoin = fullJoin;
var FullJoinIterable = /** @class */ (function () {
    function FullJoinIterable(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    FullJoinIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, e_2, _b, e_3, _c, resultSelector, outerLookup, innerLookup, keys, keys_1, keys_1_1, key, outer, inner, outer_1, outer_1_1, outerElement, inner_1, inner_1_1, innerElement, e_3_1, e_2_1, e_1_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    resultSelector = this._resultSelector;
                    outerLookup = new lookup_1.Lookup(internal_1.CreateGroupings(this._outer, this._outerKeySelector, common_1.identity, this._keyEqualer));
                    innerLookup = new lookup_1.Lookup(internal_1.CreateGroupings(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer));
                    keys = union_1.union(map_1.map(outerLookup, internal_1.SelectGroupingKey), map_1.map(innerLookup, internal_1.SelectGroupingKey), this._keyEqualer);
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 18, 19, 20]);
                    keys_1 = tslib_1.__values(keys), keys_1_1 = keys_1.next();
                    _d.label = 2;
                case 2:
                    if (!!keys_1_1.done) return [3 /*break*/, 17];
                    key = keys_1_1.value;
                    outer = defaultIfEmpty_1.defaultIfEmpty(outerLookup.get(key), undefined);
                    inner = defaultIfEmpty_1.defaultIfEmpty(innerLookup.get(key), undefined);
                    _d.label = 3;
                case 3:
                    _d.trys.push([3, 14, 15, 16]);
                    outer_1 = tslib_1.__values(outer), outer_1_1 = outer_1.next();
                    _d.label = 4;
                case 4:
                    if (!!outer_1_1.done) return [3 /*break*/, 13];
                    outerElement = outer_1_1.value;
                    _d.label = 5;
                case 5:
                    _d.trys.push([5, 10, 11, 12]);
                    inner_1 = tslib_1.__values(inner), inner_1_1 = inner_1.next();
                    _d.label = 6;
                case 6:
                    if (!!inner_1_1.done) return [3 /*break*/, 9];
                    innerElement = inner_1_1.value;
                    return [4 /*yield*/, resultSelector(outerElement, innerElement)];
                case 7:
                    _d.sent();
                    _d.label = 8;
                case 8:
                    inner_1_1 = inner_1.next();
                    return [3 /*break*/, 6];
                case 9: return [3 /*break*/, 12];
                case 10:
                    e_3_1 = _d.sent();
                    e_3 = { error: e_3_1 };
                    return [3 /*break*/, 12];
                case 11:
                    try {
                        if (inner_1_1 && !inner_1_1.done && (_c = inner_1.return)) _c.call(inner_1);
                    }
                    finally { if (e_3) throw e_3.error; }
                    return [7 /*endfinally*/];
                case 12:
                    outer_1_1 = outer_1.next();
                    return [3 /*break*/, 4];
                case 13: return [3 /*break*/, 16];
                case 14:
                    e_2_1 = _d.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 16];
                case 15:
                    try {
                        if (outer_1_1 && !outer_1_1.done && (_b = outer_1.return)) _b.call(outer_1);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 16:
                    keys_1_1 = keys_1.next();
                    return [3 /*break*/, 2];
                case 17: return [3 /*break*/, 20];
                case 18:
                    e_1_1 = _d.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 20];
                case 19:
                    try {
                        if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 20: return [2 /*return*/];
            }
        });
    };
    FullJoinIterable = tslib_1.__decorate([
        internal_1.ToStringTag("FullJoinIterable")
    ], FullJoinIterable);
    return FullJoinIterable;
}());
//# sourceMappingURL=fullJoin.js.map