"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 collections_1 = require("../collections");
var collections_2 = require("equatable/collections");
function unionBy(left, right, keySelector, keyEqualer) {
    internal_1.assert.mustBeQueryable(left, "left");
    internal_1.assert.mustBeQueryable(right, "right");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeEqualerOrUndefined(keyEqualer, "keyEqualer");
    return internal_1.FlowHierarchy(new UnionByIterable(internal_1.ToIterable(left), internal_1.ToIterable(right), keySelector, keyEqualer), left, right);
}
exports.unionBy = unionBy;
var UnionByIterable = /** @class */ (function () {
    function UnionByIterable(left, right, keySelector, keyEqualer) {
        this._left = left;
        this._right = right;
        this._keySelector = keySelector;
        this._keyEqualer = keyEqualer;
    }
    UnionByIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, e_2, _b, keySelector, set, _c, _d, element, e_1_1, _e, _f, element, e_2_1;
        return tslib_1.__generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    keySelector = this._keySelector;
                    set = this._keyEqualer ? new collections_2.HashSet() : new collections_1.Set();
                    _g.label = 1;
                case 1:
                    _g.trys.push([1, 6, 7, 8]);
                    _c = tslib_1.__values(this._left), _d = _c.next();
                    _g.label = 2;
                case 2:
                    if (!!_d.done) return [3 /*break*/, 5];
                    element = _d.value;
                    if (!internal_1.TryAdd(set, keySelector(element))) return [3 /*break*/, 4];
                    return [4 /*yield*/, element];
                case 3:
                    _g.sent();
                    _g.label = 4;
                case 4:
                    _d = _c.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _g.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8:
                    _g.trys.push([8, 13, 14, 15]);
                    _e = tslib_1.__values(this._right), _f = _e.next();
                    _g.label = 9;
                case 9:
                    if (!!_f.done) return [3 /*break*/, 12];
                    element = _f.value;
                    if (!internal_1.TryAdd(set, keySelector(element))) return [3 /*break*/, 11];
                    return [4 /*yield*/, element];
                case 10:
                    _g.sent();
                    _g.label = 11;
                case 11:
                    _f = _e.next();
                    return [3 /*break*/, 9];
                case 12: return [3 /*break*/, 15];
                case 13:
                    e_2_1 = _g.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 15];
                case 14:
                    try {
                        if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 15: return [2 /*return*/];
            }
        });
    };
    UnionByIterable = tslib_1.__decorate([
        internal_1.ToStringTag("UnionByIterable")
    ], UnionByIterable);
    return UnionByIterable;
}());
//# sourceMappingURL=unionBy.js.map