"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 common_1 = require("./common");
function rootAsync(source, predicate) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    if (predicate === void 0) { 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) {
    var AsyncHierarchyTraversalIterable = /** @class */ (function () {
        function AsyncHierarchyTraversalIterable(source, predicate) {
            this._source = source;
            this._predicate = predicate;
            this._axis = axis;
        }
        AsyncHierarchyTraversalIterable.prototype[Symbol.asyncIterator] = function () {
            return tslib_1.__asyncGenerator(this, arguments, function _a() {
                var e_1, _a, e_2, _b, source, hierarchy, predicate, axis, source_1, source_1_1, element, _c, _d, related, e_1_1, e_2_1;
                return tslib_1.__generator(this, function (_e) {
                    switch (_e.label) {
                        case 0:
                            source = this._source;
                            hierarchy = internal_1.GetHierarchy(this._source);
                            predicate = this._predicate;
                            axis = this._axis;
                            _e.label = 1;
                        case 1:
                            _e.trys.push([1, 15, 16, 21]);
                            source_1 = tslib_1.__asyncValues(source);
                            _e.label = 2;
                        case 2: return [4 /*yield*/, tslib_1.__await(source_1.next())];
                        case 3:
                            if (!(source_1_1 = _e.sent(), !source_1_1.done)) return [3 /*break*/, 14];
                            element = source_1_1.value;
                            _e.label = 4;
                        case 4:
                            _e.trys.push([4, 11, 12, 13]);
                            _c = tslib_1.__values(axis(hierarchy, element)), _d = _c.next();
                            _e.label = 5;
                        case 5:
                            if (!!_d.done) return [3 /*break*/, 10];
                            related = _d.value;
                            return [4 /*yield*/, tslib_1.__await(predicate(related))];
                        case 6:
                            if (!_e.sent()) return [3 /*break*/, 9];
                            return [4 /*yield*/, tslib_1.__await(related)];
                        case 7: return [4 /*yield*/, _e.sent()];
                        case 8:
                            _e.sent();
                            _e.label = 9;
                        case 9:
                            _d = _c.next();
                            return [3 /*break*/, 5];
                        case 10: return [3 /*break*/, 13];
                        case 11:
                            e_1_1 = _e.sent();
                            e_1 = { error: e_1_1 };
                            return [3 /*break*/, 13];
                        case 12:
                            try {
                                if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
                            }
                            finally { if (e_1) throw e_1.error; }
                            return [7 /*endfinally*/];
                        case 13: return [3 /*break*/, 2];
                        case 14: return [3 /*break*/, 21];
                        case 15:
                            e_2_1 = _e.sent();
                            e_2 = { error: e_2_1 };
                            return [3 /*break*/, 21];
                        case 16:
                            _e.trys.push([16, , 19, 20]);
                            if (!(source_1_1 && !source_1_1.done && (_b = source_1.return))) return [3 /*break*/, 18];
                            return [4 /*yield*/, tslib_1.__await(_b.call(source_1))];
                        case 17:
                            _e.sent();
                            _e.label = 18;
                        case 18: return [3 /*break*/, 20];
                        case 19:
                            if (e_2) throw e_2.error;
                            return [7 /*endfinally*/];
                        case 20: return [7 /*endfinally*/];
                        case 21: return [2 /*return*/];
                    }
                });
            });
        };
        AsyncHierarchyTraversalIterable.prototype[types_1.Hierarchical.hierarchy] = function () {
            return internal_1.GetHierarchy(this._source);
        };
        AsyncHierarchyTraversalIterable = tslib_1.__decorate([
            internal_1.ClassName(tag),
            internal_1.ToStringTag(tag)
        ], AsyncHierarchyTraversalIterable);
        return AsyncHierarchyTraversalIterable;
    }());
    return AsyncHierarchyTraversalIterable;
}
var AsyncRootTraversalIterable = CreateAsyncTraversalIterable("AsyncRootTraversalIterable", axis_1.Axis.root);
var AsyncAncestorsTraversalIterable = CreateAsyncTraversalIterable("AsyncAncestorsTraversalIterable", axis_1.Axis.ancestors);
var AsyncAncestorsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncAncestorsAndSelfTraversalIterable", axis_1.Axis.ancestorsAndSelf);
var AsyncDescendantsTraversalIterable = CreateAsyncTraversalIterable("AsyncDescendantsTraversalIterable", axis_1.Axis.descendants);
var AsyncDescendantsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncDescendantsAndSelfTraversalIterable", axis_1.Axis.descendantsAndSelf);
var AsyncParentsTraversalIterable = CreateAsyncTraversalIterable("AsyncParentsTraversalIterable", axis_1.Axis.parents);
var AsyncSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSelfTraversalIterable", axis_1.Axis.self);
var AsyncSiblingsTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsTraversalIterable", axis_1.Axis.siblings);
var AsyncSiblingsAndSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsAndSelfTraversalIterable", axis_1.Axis.siblingsAndSelf);
var AsyncSiblingsBeforeSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsBeforeSelfTraversalIterable", axis_1.Axis.siblingsBeforeSelf);
var AsyncSiblingsAfterSelfTraversalIterable = CreateAsyncTraversalIterable("AsyncSiblingsAfterSelfTraversalIterable", axis_1.Axis.siblingsAfterSelf);
var AsyncChildrenTraversalIterable = CreateAsyncTraversalIterable("AsyncChildrenTraversalIterable", axis_1.Axis.children);
//# sourceMappingURL=traversalAsync.js.map