"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 });
const tslib_1 = require("tslib");
const internal_1 = require("../internal");
const types_1 = require("../types");
const elementAt_1 = require("./elementAt");
const axis_1 = require("./axis");
/**
 * Creates a [[HierarchyIterable]] for the child of each element at the specified offset. A negative offset
 * starts from the last child.
 *
 * @param source A [[HierarchyIterable]] object.
 * @param offset The offset for the child.
 * @category Hierarchy
 */
function nthChild(source, offset) {
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeInteger(offset, "offset");
    return new NthChildIterable(source, offset);
}
exports.nthChild = nthChild;
function nthChildOf(element, provider, offset) {
    if (element !== undefined) {
        const children = axis_1.Axis.children(provider, element);
        return elementAt_1.elementAt(children, offset);
    }
    return undefined;
}
let NthChildIterable = class NthChildIterable {
    constructor(source, offset) {
        this._source = source;
        this._offset = offset;
    }
    *[Symbol.iterator]() {
        const source = this._source;
        const hierarchy = internal_1.GetHierarchy(source);
        const offset = this._offset;
        for (const element of source) {
            const child = nthChildOf(element, hierarchy, offset);
            if (child !== undefined) {
                yield child;
            }
        }
    }
    [types_1.Hierarchical.hierarchy]() {
        return internal_1.GetHierarchy(this._source);
    }
};
NthChildIterable = tslib_1.__decorate([
    internal_1.ToStringTag("NthChildIterable")
], NthChildIterable);
//# sourceMappingURL=nthChild.js.map