"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 axis_1 = require("./axis");
const common_1 = require("./common");
function rootAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncRootTraversalIterable(source, predicate);
}
exports.rootAsync = rootAsync;
function ancestorsAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncAncestorsTraversalIterable(source, predicate);
}
exports.ancestorsAsync = ancestorsAsync;
function ancestorsAndSelfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncAncestorsAndSelfTraversalIterable(source, predicate);
}
exports.ancestorsAndSelfAsync = ancestorsAndSelfAsync;
function descendantsAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncDescendantsTraversalIterable(source, predicate);
}
exports.descendantsAsync = descendantsAsync;
function descendantsAndSelfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncDescendantsAndSelfTraversalIterable(source, predicate);
}
exports.descendantsAndSelfAsync = descendantsAndSelfAsync;
function parentsAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncParentsTraversalIterable(source, predicate);
}
exports.parentsAsync = parentsAsync;
function selfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncSelfTraversalIterable(source, predicate);
}
exports.selfAsync = selfAsync;
function siblingsAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncSiblingsTraversalIterable(source, predicate);
}
exports.siblingsAsync = siblingsAsync;
function siblingsAndSelfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncSiblingsAndSelfTraversalIterable(source, predicate);
}
exports.siblingsAndSelfAsync = siblingsAndSelfAsync;
function siblingsBeforeSelfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncSiblingsBeforeSelfTraversalIterable(source, predicate);
}
exports.siblingsBeforeSelfAsync = siblingsBeforeSelfAsync;
function siblingsAfterSelfAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncSiblingsAfterSelfTraversalIterable(source, predicate);
}
exports.siblingsAfterSelfAsync = siblingsAfterSelfAsync;
function childrenAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AsyncChildrenTraversalIterable(source, predicate);
}
exports.childrenAsync = childrenAsync;
function CreateAsyncTraversalIterable(tag, axis) {
    let AsyncHierarchyTraversalIterable = class AsyncHierarchyTraversalIterable {
        constructor(source, predicate) {
            this._source = source;
            this._predicate = predicate;
            this._axis = axis;
        }
        [Symbol.asyncIterator]() {
            return tslib_1.__asyncGenerator(this, arguments, function* _a() {
                var e_1, _a;
                const source = this._source;
                const hierarchy = internal_1.GetHierarchy(this._source);
                const predicate = this._predicate;
                const axis = this._axis;
                try {
                    for (var source_1 = tslib_1.__asyncValues(source), source_1_1; source_1_1 = yield tslib_1.__await(source_1.next()), !source_1_1.done;) {
                        const element = source_1_1.value;
                        for (const related of axis(hierarchy, element)) {
                            if (yield tslib_1.__await(predicate(related))) {
                                yield yield tslib_1.__await(related);
                            }
                        }
                    }
                }
                catch (e_1_1) { e_1 = { error: e_1_1 }; }
                finally {
                    try {
                        if (source_1_1 && !source_1_1.done && (_a = source_1.return)) yield tslib_1.__await(_a.call(source_1));
                    }
                    finally { if (e_1) throw e_1.error; }
                }
            });
        }
        [types_1.Hierarchical.hierarchy]() {
            return internal_1.GetHierarchy(this._source);
        }
    };
    AsyncHierarchyTraversalIterable = tslib_1.__decorate([
        internal_1.ClassName(tag),
        internal_1.ToStringTag(tag)
    ], AsyncHierarchyTraversalIterable);
    return AsyncHierarchyTraversalIterable;
}
const AsyncRootTraversalIterable = CreateAsyncTraversalIterable("AsyncRootTraversalIterable", axis_1.Axis.root);
const AsyncAncestorsTraversalIterable = CreateAsyncTraversalIterable("AsyncAncestorsTraversalIterable", axis_1.Axis.ancestors);
const AsyncAncestorsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncAncestorsAndSelfTraversalIterable", axis_1.Axis.ancestorsAndSelf);
const AsyncDescendantsTraversalIterable = CreateAsyncTraversalIterable("AsyncDescendantsTraversalIterable", axis_1.Axis.descendants);
const AsyncDescendantsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncDescendantsAndSelfTraversalIterable", axis_1.Axis.descendantsAndSelf);
const AsyncParentsTraversalIterable = CreateAsyncTraversalIterable("AsyncParentsTraversalIterable", axis_1.Axis.parents);
const AsyncSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSelfTraversalIterable", axis_1.Axis.self);
const AsyncSiblingsTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsTraversalIterable", axis_1.Axis.siblings);
const AsyncSiblingsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsAndSelfTraversalIterable", axis_1.Axis.siblingsAndSelf);
const AsyncSiblingsBeforeSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsBeforeSelfTraversalIterable", axis_1.Axis.siblingsBeforeSelf);
const AsyncSiblingsAfterSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsAfterSelfTraversalIterable", axis_1.Axis.siblingsAfterSelf);
const AsyncChildrenTraversalIterable = CreateAsyncTraversalIterable("AsyncChildrenTraversalIterable", axis_1.Axis.children);
//# sourceMappingURL=traversalAsync.js.map