"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");
/**
 * Creates an [[AsyncIterable]] that, when iterated, consumes the provided [[AsyncIterator]].
 *
 * @param iterator An [[AsyncIterator]] object.
 * @category Query
 */
function consumeAsync(iterator, _a) {
    var _b = _a === void 0 ? {} : _a, _c = _b.cacheElements, cacheElements = _c === void 0 ? false : _c, _d = _b.leaveOpen, leaveOpen = _d === void 0 ? false : _d;
    internal_1.assert.mustBeIterator(iterator, "iterator");
    internal_1.assert.mustBeBoolean(cacheElements, "cacheElements");
    return new AsyncConsumeIterable(iterator, cacheElements, leaveOpen);
}
exports.consumeAsync = consumeAsync;
var AsyncConsumeIterable = /** @class */ (function () {
    function AsyncConsumeIterable(iterator, cacheElements, leaveOpen) {
        this._iterator = iterator;
        this._cache = cacheElements ? [] : undefined;
        this._leaveOpen = leaveOpen;
    }
    AsyncConsumeIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var offset, _a, done, value;
            return tslib_1.__generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        offset = 0;
                        _b.label = 1;
                    case 1:
                        _b.trys.push([1, , 14, 17]);
                        _b.label = 2;
                    case 2:
                        if (!(this._cache && offset < this._cache.length)) return [3 /*break*/, 5];
                        return [4 /*yield*/, tslib_1.__await(this._cache[offset++])];
                    case 3: return [4 /*yield*/, _b.sent()];
                    case 4:
                        _b.sent();
                        return [3 /*break*/, 12];
                    case 5:
                        if (!this._iterator) return [3 /*break*/, 10];
                        return [4 /*yield*/, tslib_1.__await(this._iterator.next())];
                    case 6:
                        _a = _b.sent(), done = _a.done, value = _a.value;
                        if (!!done) return [3 /*break*/, 9];
                        if (this._cache)
                            this._cache.push(value);
                        return [4 /*yield*/, tslib_1.__await(value)];
                    case 7: return [4 /*yield*/, _b.sent()];
                    case 8:
                        _b.sent();
                        offset++;
                        return [3 /*break*/, 12];
                    case 9:
                        this._iterator = undefined;
                        _b.label = 10;
                    case 10: return [4 /*yield*/, tslib_1.__await(void 0)];
                    case 11: return [2 /*return*/, _b.sent()];
                    case 12: return [3 /*break*/, 2];
                    case 13: return [3 /*break*/, 17];
                    case 14:
                        if (!!this._leaveOpen) return [3 /*break*/, 16];
                        return [4 /*yield*/, tslib_1.__await(internal_1.AsyncIteratorClose(this._iterator))];
                    case 15:
                        _b.sent();
                        _b.label = 16;
                    case 16: return [7 /*endfinally*/];
                    case 17: return [2 /*return*/];
                }
            });
        });
    };
    AsyncConsumeIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncConsumeIterable")
    ], AsyncConsumeIterable);
    return AsyncConsumeIterable;
}());
//# sourceMappingURL=consumeAsync.js.map