"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 types_1 = require("../types");
var axis_1 = require("./axis");
var elementAt_1 = require("./elementAt");
/**
 * Creates an [[AsyncHierarchyIterable]] for the child of each element at the specified offset. A negative offset
 * starts from the last child.
 *
 * @param source A [[HierarchyIterable]] or [[AsyncHierarchyIterable]] object.
 * @param offset The offset for the child.
 * @category Hierarchy
 */
function nthChildAsync(source, offset) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeInteger(offset, "offset");
    return new AsyncNthChildIterable(source, offset);
}
exports.nthChildAsync = nthChildAsync;
function nthChildOf(element, provider, offset) {
    if (element !== undefined) {
        var children = axis_1.Axis.children(provider, element);
        return elementAt_1.elementAt(children, offset);
    }
    return undefined;
}
var AsyncNthChildIterable = /** @class */ (function () {
    function AsyncNthChildIterable(source, offset) {
        this._source = source;
        this._offset = offset;
    }
    AsyncNthChildIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            var e_1, _a, offset, hierarchy, _b, _c, element, child, e_1_1;
            return tslib_1.__generator(this, function (_d) {
                switch (_d.label) {
                    case 0:
                        offset = this._offset;
                        hierarchy = internal_1.GetHierarchy(this._source);
                        _d.label = 1;
                    case 1:
                        _d.trys.push([1, 8, 9, 14]);
                        _b = tslib_1.__asyncValues(this._source);
                        _d.label = 2;
                    case 2: return [4 /*yield*/, tslib_1.__await(_b.next())];
                    case 3:
                        if (!(_c = _d.sent(), !_c.done)) return [3 /*break*/, 7];
                        element = _c.value;
                        child = nthChildOf(element, hierarchy, offset);
                        if (!(child !== undefined)) return [3 /*break*/, 6];
                        return [4 /*yield*/, tslib_1.__await(child)];
                    case 4: return [4 /*yield*/, _d.sent()];
                    case 5:
                        _d.sent();
                        _d.label = 6;
                    case 6: return [3 /*break*/, 2];
                    case 7: return [3 /*break*/, 14];
                    case 8:
                        e_1_1 = _d.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 14];
                    case 9:
                        _d.trys.push([9, , 12, 13]);
                        if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 11];
                        return [4 /*yield*/, tslib_1.__await(_a.call(_b))];
                    case 10:
                        _d.sent();
                        _d.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*/];
                }
            });
        });
    };
    AsyncNthChildIterable.prototype[types_1.Hierarchical.hierarchy] = function () {
        return internal_1.GetHierarchy(this._source);
    };
    AsyncNthChildIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncNthChildIterable")
    ], AsyncNthChildIterable);
    return AsyncNthChildIterable;
}());
//# sourceMappingURL=nthChildAsync.js.map