"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 symmetricDifferenceBy(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 SymmetricDifferenceByIterable(internal_1.ToIterable(left), internal_1.ToIterable(right), keySelector, keyEqualer), left, right);
}
exports.symmetricDifferenceBy = symmetricDifferenceBy;
var SymmetricDifferenceByIterable = /** @class */ (function () {
    function SymmetricDifferenceByIterable(left, right, keySelector, keyEqualer) {
        this._left = left;
        this._right = right;
        this._keySelector = keySelector;
        this._keyEqualer = keyEqualer;
    }
    SymmetricDifferenceByIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, e_2, _b, e_3, _c, keySelector, rightKeys, right, _d, _e, element, key, set, _f, _g, element, key, e_2_1, right_1, right_1_1, _h, key, element, e_3_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();
                    try {
                        for (_d = tslib_1.__values(this._right), _e = _d.next(); !_e.done; _e = _d.next()) {
                            element = _e.value;
                            key = keySelector(element);
                            if (internal_1.TryAdd(rightKeys, key)) {
                                right.set(key, element);
                            }
                        }
                    }
                    catch (e_1_1) { e_1 = { error: e_1_1 }; }
                    finally {
                        try {
                            if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
                        }
                        finally { if (e_1) throw e_1.error; }
                    }
                    set = this._keyEqualer ? new collections_2.HashSet(this._keyEqualer) : new collections_1.Set();
                    _j.label = 1;
                case 1:
                    _j.trys.push([1, 6, 7, 8]);
                    _f = tslib_1.__values(this._left), _g = _f.next();
                    _j.label = 2;
                case 2:
                    if (!!_g.done) return [3 /*break*/, 5];
                    element = _g.value;
                    key = keySelector(element);
                    if (!(internal_1.TryAdd(set, key) && !right.has(key))) return [3 /*break*/, 4];
                    return [4 /*yield*/, element];
                case 3:
                    _j.sent();
                    _j.label = 4;
                case 4:
                    _g = _f.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_2_1 = _j.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 8:
                    _j.trys.push([8, 13, 14, 15]);
                    right_1 = tslib_1.__values(right), right_1_1 = right_1.next();
                    _j.label = 9;
                case 9:
                    if (!!right_1_1.done) return [3 /*break*/, 12];
                    _h = tslib_1.__read(right_1_1.value, 2), key = _h[0], element = _h[1];
                    if (!internal_1.TryAdd(set, key)) return [3 /*break*/, 11];
                    return [4 /*yield*/, element];
                case 10:
                    _j.sent();
                    _j.label = 11;
                case 11:
                    right_1_1 = right_1.next();
                    return [3 /*break*/, 9];
                case 12: return [3 /*break*/, 15];
                case 13:
                    e_3_1 = _j.sent();
                    e_3 = { error: e_3_1 };
                    return [3 /*break*/, 15];
                case 14:
                    try {
                        if (right_1_1 && !right_1_1.done && (_c = right_1.return)) _c.call(right_1);
                    }
                    finally { if (e_3) throw e_3.error; }
                    return [7 /*endfinally*/];
                case 15: return [2 /*return*/];
            }
        });
    };
    SymmetricDifferenceByIterable = tslib_1.__decorate([
        internal_1.ToStringTag("SymmetricDifferenceByIterable")
    ], SymmetricDifferenceByIterable);
    return SymmetricDifferenceByIterable;
}());
//# sourceMappingURL=symmetricDifferenceBy.js.map