"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 symmetricDifferenceByAsync(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 AsyncSymmetricDifferenceByIterable(internal_1.ToPossiblyAsyncIterable(left), internal_1.ToPossiblyAsyncIterable(right), keySelector), left, right);
}
exports.symmetricDifferenceByAsync = symmetricDifferenceByAsync;
var AsyncSymmetricDifferenceByIterable = /** @class */ (function () {
    function AsyncSymmetricDifferenceByIterable(left, right, keySelector, keyEqualer) {
        this._left = left;
        this._right = right;
        this._keySelector = keySelector;
        this._keyEqualer = keyEqualer;
    }
    AsyncSymmetricDifferenceByIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var e_1, _a, e_2, _b, e_3, _c, keySelector, rightKeys, right, _d, _e, element, key, e_2_1, set, _f, _g, element, key, e_3_1, right_1, right_1_1, _h, key, element, e_1_1;
            return tslib_1.__generator(this, function (_j) {
                switch (_j.label) {
                    case 0:
                        keySelector = this._keySelector;
                        rightKeys = this._keyEqualer ? new collections_2.HashSet(this._keyEqualer) : new collections_1.Set();
                        right = this._keyEqualer ? new collections_2.HashMap(this._keyEqualer) : new Map();
                        _j.label = 1;
                    case 1:
                        _j.trys.push([1, 6, 7, 12]);
                        _d = tslib_1.__asyncValues(this._right);
                        _j.label = 2;
                    case 2: return [4 /*yield*/, tslib_1.__await(_d.next())];
                    case 3:
                        if (!(_e = _j.sent(), !_e.done)) return [3 /*break*/, 5];
                        element = _e.value;
                        key = keySelector(element);
                        if (internal_1.TryAdd(rightKeys, key)) {
                            right.set(key, element);
                        }
                        _j.label = 4;
                    case 4: return [3 /*break*/, 2];
                    case 5: return [3 /*break*/, 12];
                    case 6:
                        e_2_1 = _j.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 12];
                    case 7:
                        _j.trys.push([7, , 10, 11]);
                        if (!(_e && !_e.done && (_b = _d.return))) return [3 /*break*/, 9];
                        return [4 /*yield*/, tslib_1.__await(_b.call(_d))];
                    case 8:
                        _j.sent();
                        _j.label = 9;
                    case 9: return [3 /*break*/, 11];
                    case 10:
                        if (e_2) throw e_2.error;
                        return [7 /*endfinally*/];
                    case 11: return [7 /*endfinally*/];
                    case 12:
                        set = this._keyEqualer ? new collections_2.HashSet(this._keyEqualer) : new collections_1.Set();
                        _j.label = 13;
                    case 13:
                        _j.trys.push([13, 20, 21, 26]);
                        _f = tslib_1.__asyncValues(this._left);
                        _j.label = 14;
                    case 14: return [4 /*yield*/, tslib_1.__await(_f.next())];
                    case 15:
                        if (!(_g = _j.sent(), !_g.done)) return [3 /*break*/, 19];
                        element = _g.value;
                        key = keySelector(element);
                        if (!(internal_1.TryAdd(set, key) && !right.has(key))) return [3 /*break*/, 18];
                        return [4 /*yield*/, tslib_1.__await(element)];
                    case 16: return [4 /*yield*/, _j.sent()];
                    case 17:
                        _j.sent();
                        _j.label = 18;
                    case 18: return [3 /*break*/, 14];
                    case 19: return [3 /*break*/, 26];
                    case 20:
                        e_3_1 = _j.sent();
                        e_3 = { error: e_3_1 };
                        return [3 /*break*/, 26];
                    case 21:
                        _j.trys.push([21, , 24, 25]);
                        if (!(_g && !_g.done && (_c = _f.return))) return [3 /*break*/, 23];
                        return [4 /*yield*/, tslib_1.__await(_c.call(_f))];
                    case 22:
                        _j.sent();
                        _j.label = 23;
                    case 23: return [3 /*break*/, 25];
                    case 24:
                        if (e_3) throw e_3.error;
                        return [7 /*endfinally*/];
                    case 25: return [7 /*endfinally*/];
                    case 26:
                        _j.trys.push([26, 32, 33, 34]);
                        right_1 = tslib_1.__values(right), right_1_1 = right_1.next();
                        _j.label = 27;
                    case 27:
                        if (!!right_1_1.done) return [3 /*break*/, 31];
                        _h = tslib_1.__read(right_1_1.value, 2), key = _h[0], element = _h[1];
                        if (!internal_1.TryAdd(set, key)) return [3 /*break*/, 30];
                        return [4 /*yield*/, tslib_1.__await(element)];
                    case 28: return [4 /*yield*/, _j.sent()];
                    case 29:
                        _j.sent();
                        _j.label = 30;
                    case 30:
                        right_1_1 = right_1.next();
                        return [3 /*break*/, 27];
                    case 31: return [3 /*break*/, 34];
                    case 32:
                        e_1_1 = _j.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 34];
                    case 33:
                        try {
                            if (right_1_1 && !right_1_1.done && (_a = right_1.return)) _a.call(right_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7 /*endfinally*/];
                    case 34: return [2 /*return*/];
                }
            });
        });
    };
    AsyncSymmetricDifferenceByIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncSymmetricDifferenceByIterable")
    ], AsyncSymmetricDifferenceByIterable);
    return AsyncSymmetricDifferenceByIterable;
}());
//# sourceMappingURL=symmetricDifferenceByAsync.js.map