"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 [[Iterable]] that, when iterated, consumes the provided [[Iterator]].
 *
 * @param iterator An [[Iterator]] object.
 * @category Query
 */
function consume(iterator, options) {
    if (options === void 0) { options = {}; }
    var _a = options.cacheElements, cacheElements = _a === void 0 ? false : _a, _b = options.leaveOpen, leaveOpen = _b === void 0 ? false : _b;
    internal_1.assert.mustBeIterator(iterator, "iterator");
    internal_1.assert.mustBeBoolean(cacheElements, "cacheElements");
    return new ConsumeIterable(iterator, cacheElements, leaveOpen);
}
exports.consume = consume;
var ConsumeIterable = /** @class */ (function () {
    function ConsumeIterable(iterator, cacheElements, leaveOpen) {
        this._iterator = iterator;
        this._cache = cacheElements ? [] : undefined;
        this._leaveOpen = leaveOpen;
    }
    ConsumeIterable.prototype[Symbol.iterator] = function () {
        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, , 10, 11]);
                    _b.label = 2;
                case 2:
                    if (!(this._cache && offset < this._cache.length)) return [3 /*break*/, 4];
                    return [4 /*yield*/, this._cache[offset++]];
                case 3:
                    _b.sent();
                    return [3 /*break*/, 8];
                case 4:
                    if (!this._iterator) return [3 /*break*/, 7];
                    _a = this._iterator.next(), done = _a.done, value = _a.value;
                    if (!!done) return [3 /*break*/, 6];
                    if (this._cache)
                        this._cache.push(value);
                    return [4 /*yield*/, value];
                case 5:
                    _b.sent();
                    offset++;
                    return [3 /*break*/, 8];
                case 6:
                    this._iterator = undefined;
                    _b.label = 7;
                case 7: return [2 /*return*/];
                case 8: return [3 /*break*/, 2];
                case 9: return [3 /*break*/, 11];
                case 10:
                    if (!this._leaveOpen) {
                        internal_1.IteratorClose(this._iterator);
                    }
                    return [7 /*endfinally*/];
                case 11: return [2 /*return*/];
            }
        });
    };
    ConsumeIterable = tslib_1.__decorate([
        internal_1.ToStringTag("ConsumeIterable")
    ], ConsumeIterable);
    return ConsumeIterable;
}());
//# sourceMappingURL=consume.js.map