"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 unionByAsync(left, right, keySelector, keyEqualer) {
    internal_1.assert.mustBeAsyncQueryable(left, "left");
    internal_1.assert.mustBeAsyncQueryable(right, "right");
    internal_1.assert.mustBeFunction(keySelector, "keySelector");
    internal_1.assert.mustBeEqualerOrUndefined(keyEqualer, "keyEqualer");
    return internal_1.FlowHierarchy(new AsyncUnionByIterable(internal_1.ToPossiblyAsyncIterable(left), internal_1.ToPossiblyAsyncIterable(right), keySelector, keyEqualer), left, right);
}
exports.unionByAsync = unionByAsync;
var AsyncUnionByIterable = /** @class */ (function () {
    function AsyncUnionByIterable(left, right, keySelector, keyEqualer) {
        this._left = left;
        this._right = right;
        this._keySelector = keySelector;
        this._keyEqualer = keyEqualer;
    }
    AsyncUnionByIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            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(this._keyEqualer) : new collections_1.Set();
                        _g.label = 1;
                    case 1:
                        _g.trys.push([1, 8, 9, 14]);
                        _c = tslib_1.__asyncValues(this._left);
                        _g.label = 2;
                    case 2: return [4 /*yield*/, tslib_1.__await(_c.next())];
                    case 3:
                        if (!(_d = _g.sent(), !_d.done)) return [3 /*break*/, 7];
                        element = _d.value;
                        if (!internal_1.TryAdd(set, keySelector(element))) return [3 /*break*/, 6];
                        return [4 /*yield*/, tslib_1.__await(element)];
                    case 4: return [4 /*yield*/, _g.sent()];
                    case 5:
                        _g.sent();
                        _g.label = 6;
                    case 6: return [3 /*break*/, 2];
                    case 7: return [3 /*break*/, 14];
                    case 8:
                        e_1_1 = _g.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 14];
                    case 9:
                        _g.trys.push([9, , 12, 13]);
                        if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 11];
                        return [4 /*yield*/, tslib_1.__await(_a.call(_c))];
                    case 10:
                        _g.sent();
                        _g.label = 11;
                    case 11: return [3 /*break*/, 13];
                    case 12:
                        if (e_1) throw e_1.error;
                        return [7 /*endfinally*/];
                    case 13: return [7 /*endfinally*/];
                    case 14:
                        _g.trys.push([14, 21, 22, 27]);
                        _e = tslib_1.__asyncValues(this._right);
                        _g.label = 15;
                    case 15: return [4 /*yield*/, tslib_1.__await(_e.next())];
                    case 16:
                        if (!(_f = _g.sent(), !_f.done)) return [3 /*break*/, 20];
                        element = _f.value;
                        if (!internal_1.TryAdd(set, keySelector(element))) return [3 /*break*/, 19];
                        return [4 /*yield*/, tslib_1.__await(element)];
                    case 17: return [4 /*yield*/, _g.sent()];
                    case 18:
                        _g.sent();
                        _g.label = 19;
                    case 19: return [3 /*break*/, 15];
                    case 20: return [3 /*break*/, 27];
                    case 21:
                        e_2_1 = _g.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 27];
                    case 22:
                        _g.trys.push([22, , 25, 26]);
                        if (!(_f && !_f.done && (_b = _e.return))) return [3 /*break*/, 24];
                        return [4 /*yield*/, tslib_1.__await(_b.call(_e))];
                    case 23:
                        _g.sent();
                        _g.label = 24;
                    case 24: return [3 /*break*/, 26];
                    case 25:
                        if (e_2) throw e_2.error;
                        return [7 /*endfinally*/];
                    case 26: return [7 /*endfinally*/];
                    case 27: return [2 /*return*/];
                }
            });
        });
    };
    AsyncUnionByIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncUnionByIterable")
    ], AsyncUnionByIterable);
    return AsyncUnionByIterable;
}());
//# sourceMappingURL=unionByAsync.js.map