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