"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 lastAsync_1 = require("./lastAsync");
/**
 * Finds the value at the provided offset. A negative offset starts from the
 * last element.
 *
 * @param source An [[AsyncQueryable]] object.
 * @param offset An offset.
 * @category Scalar
 */
function elementAtAsync(source, offset) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var e_1, _a, e_2, _b, array, _c, _d, element, e_1_1, _e, _f, element, e_2_1;
        return tslib_1.__generator(this, function (_g) {
            switch (_g.label) {
                case 0:
                    internal_1.assert.mustBeAsyncQueryable(source, "source");
                    internal_1.assert.mustBeInteger(offset, "offset");
                    if (!(offset === -1)) return [3 /*break*/, 2];
                    return [4 /*yield*/, lastAsync_1.lastAsync(source)];
                case 1: return [2 /*return*/, _g.sent()];
                case 2:
                    if (!(offset < 0)) return [3 /*break*/, 15];
                    offset = Math.abs(offset);
                    array = [];
                    _g.label = 3;
                case 3:
                    _g.trys.push([3, 8, 9, 14]);
                    _c = tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(source));
                    _g.label = 4;
                case 4: return [4 /*yield*/, _c.next()];
                case 5:
                    if (!(_d = _g.sent(), !_d.done)) return [3 /*break*/, 7];
                    element = _d.value;
                    if (array.length >= offset) {
                        array.shift();
                    }
                    array.push(element);
                    _g.label = 6;
                case 6: return [3 /*break*/, 4];
                case 7: return [3 /*break*/, 14];
                case 8:
                    e_1_1 = _g.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 14];
                case 9:
                    _g.trys.push([9, , 12, 13]);
                    if (!(_d && !_d.done && (_a = _c.return))) return [3 /*break*/, 11];
                    return [4 /*yield*/, _a.call(_c)];
                case 10:
                    _g.sent();
                    _g.label = 11;
                case 11: return [3 /*break*/, 13];
                case 12:
                    if (e_1) throw e_1.error;
                    return [7 /*endfinally*/];
                case 13: return [7 /*endfinally*/];
                case 14: return [2 /*return*/, array.length - offset >= 0 ? array[array.length - offset] : undefined];
                case 15:
                    _g.trys.push([15, 20, 21, 26]);
                    _e = tslib_1.__asyncValues(internal_1.ToPossiblyAsyncIterable(source));
                    _g.label = 16;
                case 16: return [4 /*yield*/, _e.next()];
                case 17:
                    if (!(_f = _g.sent(), !_f.done)) return [3 /*break*/, 19];
                    element = _f.value;
                    if (offset === 0) {
                        return [2 /*return*/, element];
                    }
                    offset--;
                    _g.label = 18;
                case 18: return [3 /*break*/, 16];
                case 19: return [3 /*break*/, 26];
                case 20:
                    e_2_1 = _g.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 26];
                case 21:
                    _g.trys.push([21, , 24, 25]);
                    if (!(_f && !_f.done && (_b = _e.return))) return [3 /*break*/, 23];
                    return [4 /*yield*/, _b.call(_e)];
                case 22:
                    _g.sent();
                    _g.label = 23;
                case 23: return [3 /*break*/, 25];
                case 24:
                    if (e_2) throw e_2.error;
                    return [7 /*endfinally*/];
                case 25: return [7 /*endfinally*/];
                case 26: return [2 /*return*/, undefined];
            }
        });
    });
}
exports.elementAtAsync = elementAtAsync;
//# sourceMappingURL=elementAtAsync.js.map