"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 common_1 = require("./common");
function toArrayAsync(source, elementSelector) {
    if (elementSelector === void 0) { elementSelector = common_1.identity; }
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var e_1, _a, result, _b, _c, item, _d, _e, _f, e_1_1;
        return tslib_1.__generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    internal_1.assert.mustBeAsyncQueryable(source, "source");
                    internal_1.assert.mustBeFunction(elementSelector, "elementSelector");
                    result = [];
                    _g.label = 1;
                case 1:
                    _g.trys.push([1, 9, 10, 15]);
                    _b = tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(source));
                    _g.label = 2;
                case 2: return [4 /*yield*/, _b.next()];
                case 3:
                    if (!(_c = _g.sent(), !_c.done)) return [3 /*break*/, 8];
                    item = _c.value;
                    _e = (_d = result).push;
                    if (!(elementSelector === common_1.identity)) return [3 /*break*/, 4];
                    _f = item;
                    return [3 /*break*/, 6];
                case 4: return [4 /*yield*/, elementSelector(item)];
                case 5:
                    _f = _g.sent();
                    _g.label = 6;
                case 6:
                    _e.apply(_d, [_f]);
                    _g.label = 7;
                case 7: return [3 /*break*/, 2];
                case 8: return [3 /*break*/, 15];
                case 9:
                    e_1_1 = _g.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 15];
                case 10:
                    _g.trys.push([10, , 13, 14]);
                    if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 12];
                    return [4 /*yield*/, _a.call(_b)];
                case 11:
                    _g.sent();
                    _g.label = 12;
                case 12: return [3 /*break*/, 14];
                case 13:
                    if (e_1) throw e_1.error;
                    return [7 /*endfinally*/];
                case 14: return [7 /*endfinally*/];
                case 15: return [2 /*return*/, result];
            }
        });
    });
}
exports.toArrayAsync = toArrayAsync;
//# sourceMappingURL=toArrayAsync.js.map