"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 flatMapAsync(source, projection, resultSelector) {
    internal_1.assert.mustBeAsyncQueryable(source, "source");
    internal_1.assert.mustBeFunction(projection, "projection");
    return new AsyncFlatMapIterable(internal_1.ToPossiblyAsyncIterable(source), projection, resultSelector);
}
exports.flatMapAsync = flatMapAsync;
var AsyncFlatMapIterable = /** @class */ (function () {
    function AsyncFlatMapIterable(source, projection, resultSelector) {
        this._source = source;
        this._projection = projection;
        this._resultSelector = resultSelector;
    }
    AsyncFlatMapIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            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, 23, 24, 29]);
                        _c = tslib_1.__asyncValues(this._source);
                        _g.label = 2;
                    case 2: return [4 /*yield*/, tslib_1.__await(_c.next())];
                    case 3:
                        if (!(_d = _g.sent(), !_d.done)) return [3 /*break*/, 22];
                        element = _d.value;
                        inner = projection(element);
                        if (!resultSelector) return [3 /*break*/, 18];
                        _g.label = 4;
                    case 4:
                        _g.trys.push([4, 11, 12, 17]);
                        _e = tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(inner));
                        _g.label = 5;
                    case 5: return [4 /*yield*/, tslib_1.__await(_e.next())];
                    case 6:
                        if (!(_f = _g.sent(), !_f.done)) return [3 /*break*/, 10];
                        innerElement = _f.value;
                        return [4 /*yield*/, tslib_1.__await(resultSelector(element, innerElement))];
                    case 7: return [4 /*yield*/, _g.sent()];
                    case 8:
                        _g.sent();
                        _g.label = 9;
                    case 9: return [3 /*break*/, 5];
                    case 10: return [3 /*break*/, 17];
                    case 11:
                        e_2_1 = _g.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 17];
                    case 12:
                        _g.trys.push([12, , 15, 16]);
                        if (!(_f && !_f.done && (_b = _e.return))) return [3 /*break*/, 14];
                        return [4 /*yield*/, tslib_1.__await(_b.call(_e))];
                    case 13:
                        _g.sent();
                        _g.label = 14;
                    case 14: return [3 /*break*/, 16];
                    case 15:
                        if (e_2) throw e_2.error;
                        return [7 /*endfinally*/];
                    case 16: return [7 /*endfinally*/];
                    case 17: return [3 /*break*/, 21];
                    case 18: return [5 /*yield**/, tslib_1.__values(tslib_1.__asyncDelegator(tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(inner))))];
                    case 19: return [4 /*yield*/, tslib_1.__await.apply(void 0, [_g.sent()])];
                    case 20:
                        _g.sent();
                        _g.label = 21;
                    case 21: return [3 /*break*/, 2];
                    case 22: return [3 /*break*/, 29];
                    case 23:
                        e_1_1 = _g.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 29];
                    case 24:
                        _g.trys.push([24, , 27, 28]);
                        if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 26];
                        return [4 /*yield*/, tslib_1.__await(_a.call(_c))];
                    case 25:
                        _g.sent();
                        _g.label = 26;
                    case 26: return [3 /*break*/, 28];
                    case 27:
                        if (e_1) throw e_1.error;
                        return [7 /*endfinally*/];
                    case 28: return [7 /*endfinally*/];
                    case 29: return [2 /*return*/];
                }
            });
        });
    };
    AsyncFlatMapIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncFlatMapIterable")
    ], AsyncFlatMapIterable);
    return AsyncFlatMapIterable;
}());
//# sourceMappingURL=flatMapAsync.js.map