"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 root(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new RootTraversalIterable(source, predicate);
}
exports.root = root;
function ancestors(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AncestorsTraversalIterable(source, predicate);
}
exports.ancestors = ancestors;
function ancestorsAndSelf(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new AncestorsAndSelfTraversalIterable(source, predicate);
}
exports.ancestorsAndSelf = ancestorsAndSelf;
function descendants(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new DescendantsTraversalIterable(source, predicate);
}
exports.descendants = descendants;
function descendantsAndSelf(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new DescendantsAndSelfTraversalIterable(source, predicate);
}
exports.descendantsAndSelf = descendantsAndSelf;
function parents(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new ParentsTraversalIterable(source, predicate);
}
exports.parents = parents;
function self(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new SelfTraversalIterable(source, predicate);
}
exports.self = self;
function siblings(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new SiblingsTraversalIterable(source, predicate);
}
exports.siblings = siblings;
function siblingsAndSelf(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new SiblingsAndSelfTraversalIterable(source, predicate);
}
exports.siblingsAndSelf = siblingsAndSelf;
function siblingsBeforeSelf(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new SiblingsBeforeSelfTraversalIterable(source, predicate);
}
exports.siblingsBeforeSelf = siblingsBeforeSelf;
function siblingsAfterSelf(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new SiblingsAfterSelfTraversalIterable(source, predicate);
}
exports.siblingsAfterSelf = siblingsAfterSelf;
function children(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    internal_1.assert.mustBeFunctionOrUndefined(predicate, "predicate");
    return new ChildrenTraversalIterable(source, predicate);
}
exports.children = children;
function CreateTraversalIterable(tag, axis) {
    var HierarchyTraversalIterable = /** @class */ (function () {
        function HierarchyTraversalIterable(source, predicate) {
            this._source = source;
            this._predicate = predicate;
            this._axis = axis;
        }
        HierarchyTraversalIterable.prototype[Symbol.iterator] = function () {
            var e_1, _a, e_2, _b, source, hierarchy, predicate, axis, source_1, source_1_1, element, _c, _d, related, e_2_1, e_1_1;
            return tslib_1.__generator(this, function (_e) {
                switch (_e.label) {
                    case 0:
                        source = this._source;
                        hierarchy = internal_1.GetHierarchy(source);
                        predicate = this._predicate;
                        axis = this._axis;
                        _e.label = 1;
                    case 1:
                        _e.trys.push([1, 12, 13, 14]);
                        source_1 = tslib_1.__values(source), source_1_1 = source_1.next();
                        _e.label = 2;
                    case 2:
                        if (!!source_1_1.done) return [3 /*break*/, 11];
                        element = source_1_1.value;
                        _e.label = 3;
                    case 3:
                        _e.trys.push([3, 8, 9, 10]);
                        _c = tslib_1.__values(axis(hierarchy, element)), _d = _c.next();
                        _e.label = 4;
                    case 4:
                        if (!!_d.done) return [3 /*break*/, 7];
                        related = _d.value;
                        if (!predicate(related)) return [3 /*break*/, 6];
                        return [4 /*yield*/, related];
                    case 5:
                        _e.sent();
                        _e.label = 6;
                    case 6:
                        _d = _c.next();
                        return [3 /*break*/, 4];
                    case 7: return [3 /*break*/, 10];
                    case 8:
                        e_2_1 = _e.sent();
                        e_2 = { error: e_2_1 };
                        return [3 /*break*/, 10];
                    case 9:
                        try {
                            if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
                        }
                        finally { if (e_2) throw e_2.error; }
                        return [7 /*endfinally*/];
                    case 10:
                        source_1_1 = source_1.next();
                        return [3 /*break*/, 2];
                    case 11: return [3 /*break*/, 14];
                    case 12:
                        e_1_1 = _e.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 14];
                    case 13:
                        try {
                            if (source_1_1 && !source_1_1.done && (_a = source_1.return)) _a.call(source_1);
                        }
                        finally { if (e_1) throw e_1.error; }
                        return [7 /*endfinally*/];
                    case 14: return [2 /*return*/];
                }
            });
        };
        HierarchyTraversalIterable.prototype[types_1.Hierarchical.hierarchy] = function () {
            return internal_1.GetHierarchy(this._source);
        };
        HierarchyTraversalIterable = tslib_1.__decorate([
            internal_1.ToStringTag(tag)
        ], HierarchyTraversalIterable);
        return HierarchyTraversalIterable;
    }());
    return HierarchyTraversalIterable;
}
var RootTraversalIterable = CreateTraversalIterable("RootTraversalIterable", axis_1.Axis.root);
var AncestorsTraversalIterable = CreateTraversalIterable("AncestorsTraversalIterable", axis_1.Axis.ancestors);
var AncestorsAndSelfTraversalIterable = CreateTraversalIterable("AncestorsAndSelfTraversalIterable", axis_1.Axis.ancestorsAndSelf);
var DescendantsTraversalIterable = CreateTraversalIterable("DescendantsTraversalIterable", axis_1.Axis.descendants);
var DescendantsAndSelfTraversalIterable = CreateTraversalIterable("DescendantsAndSelfTraversalIterable", axis_1.Axis.descendantsAndSelf);
var ParentsTraversalIterable = CreateTraversalIterable("ParentsTraversalIterable", axis_1.Axis.parents);
var SelfTraversalIterable = CreateTraversalIterable("SelfTraversalIterable", axis_1.Axis.self);
var SiblingsTraversalIterable = CreateTraversalIterable("SiblingsTraversalIterable", axis_1.Axis.siblings);
var SiblingsAndSelfTraversalIterable = CreateTraversalIterable("SiblingsAndSelfTraversalIterable", axis_1.Axis.siblingsAndSelf);
var SiblingsBeforeSelfTraversalIterable = CreateTraversalIterable("SiblingsBeforeSelfTraversalIterable", axis_1.Axis.siblingsBeforeSelf);
var SiblingsAfterSelfTraversalIterable = CreateTraversalIterable("SiblingsAfterSelfTraversalIterable", axis_1.Axis.siblingsAfterSelf);
var ChildrenTraversalIterable = CreateTraversalIterable("ChildrenTraversalIterable", axis_1.Axis.children);
//# sourceMappingURL=traversal.js.map