"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 });
const tslib_1 = require("tslib");
const internal_1 = require("../internal");
const union_1 = require("./union");
const map_1 = require("./map");
const lookup_1 = require("../lookup");
const defaultIfEmpty_1 = require("./defaultIfEmpty");
const 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;
let FullJoinIterable = class FullJoinIterable {
    constructor(outer, inner, outerKeySelector, innerKeySelector, resultSelector, keyEqualer) {
        this._outer = outer;
        this._inner = inner;
        this._outerKeySelector = outerKeySelector;
        this._innerKeySelector = innerKeySelector;
        this._resultSelector = resultSelector;
        this._keyEqualer = keyEqualer;
    }
    *[Symbol.iterator]() {
        const resultSelector = this._resultSelector;
        const outerLookup = new lookup_1.Lookup(internal_1.CreateGroupings(this._outer, this._outerKeySelector, common_1.identity, this._keyEqualer));
        const innerLookup = new lookup_1.Lookup(internal_1.CreateGroupings(this._inner, this._innerKeySelector, common_1.identity, this._keyEqualer));
        const keys = union_1.union(map_1.map(outerLookup, internal_1.SelectGroupingKey), map_1.map(innerLookup, internal_1.SelectGroupingKey), this._keyEqualer);
        for (const key of keys) {
            const outer = defaultIfEmpty_1.defaultIfEmpty(outerLookup.get(key), undefined);
            const inner = defaultIfEmpty_1.defaultIfEmpty(innerLookup.get(key), undefined);
            for (const outerElement of outer) {
                for (const innerElement of inner) {
                    yield resultSelector(outerElement, innerElement);
                }
            }
        }
    }
};
FullJoinIterable = tslib_1.__decorate([
    internal_1.ToStringTag("FullJoinIterable")
], FullJoinIterable);
//# sourceMappingURL=fullJoin.js.map