"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");
function flatMap(source, projection, resultSelector) {
    internal_1.assert.mustBeQueryable(source, "source");
    internal_1.assert.mustBeFunction(projection, "projection");
    internal_1.assert.mustBeFunctionOrUndefined(resultSelector, "resultSelector");
    return new FlatMapIterable(internal_1.ToIterable(source), projection, resultSelector);
}
exports.flatMap = flatMap;
var FlatMapIterable = /** @class */ (function () {
    function FlatMapIterable(source, projection, resultSelector) {
        this._source = source;
        this._projection = projection;
        this._resultSelector = resultSelector;
    }
    FlatMapIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, e_2, _b, projection, resultSelector, _c, _d, element, inner, _e, _f, innerElement, e_2_1, e_1_1;
        return tslib_1.__generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    projection = this._projection;
                    resultSelector = this._resultSelector;
                    _g.label = 1;
                case 1:
                    _g.trys.push([1, 15, 16, 17]);
                    _c = tslib_1.__values(this._source), _d = _c.next();
                    _g.label = 2;
                case 2:
                    if (!!_d.done) return [3 /*break*/, 14];
                    element = _d.value;
                    inner = projection(element);
                    if (!resultSelector) return [3 /*break*/, 11];
                    _g.label = 3;
                case 3:
                    _g.trys.push([3, 8, 9, 10]);
                    _e = tslib_1.__values(internal_1.ToIterable(inner)), _f = _e.next();
                    _g.label = 4;
                case 4:
                    if (!!_f.done) return [3 /*break*/, 7];
                    innerElement = _f.value;
                    return [4 /*yield*/, resultSelector(element, innerElement)];
                case 5:
                    _g.sent();
                    _g.label = 6;
                case 6:
                    _f = _e.next();
                    return [3 /*break*/, 4];
                case 7: return [3 /*break*/, 10];
                case 8:
                    e_2_1 = _g.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 10];
                case 9:
                    try {
                        if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 10: return [3 /*break*/, 13];
                case 11: return [5 /*yield**/, tslib_1.__values(internal_1.ToIterable(inner))];
                case 12:
                    _g.sent();
                    _g.label = 13;
                case 13:
                    _d = _c.next();
                    return [3 /*break*/, 2];
                case 14: return [3 /*break*/, 17];
                case 15:
                    e_1_1 = _g.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 17];
                case 16:
                    try {
                        if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 17: return [2 /*return*/];
            }
        });
    };
    FlatMapIterable = tslib_1.__decorate([
        internal_1.ToStringTag("FlatMapIterable")
    ], FlatMapIterable);
    return FlatMapIterable;
}());
//# sourceMappingURL=flatMap.js.map