"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.
 */
/** @ignore */
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var internal_1 = require("../internal");
/** @internal */
var Axis;
(function (Axis) {
    function root(provider, element) {
        var e_1, _a, hasRoot, root, _b, _c, ancestor;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    hasRoot = false;
                    try {
                        for (_b = tslib_1.__values(ancestorsAndSelf(provider, element)), _c = _b.next(); !_c.done; _c = _b.next()) {
                            ancestor = _c.value;
                            hasRoot = true;
                            root = ancestor;
                        }
                    }
                    catch (e_1_1) { e_1 = { error: e_1_1 }; }
                    finally {
                        try {
                            if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                        }
                        finally { if (e_1) throw e_1.error; }
                    }
                    if (!hasRoot) return [3 /*break*/, 2];
                    return [4 /*yield*/, root];
                case 1:
                    _d.sent();
                    _d.label = 2;
                case 2: return [2 /*return*/];
            }
        });
    }
    Axis.root = root;
    function ancestors(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(ancestorsWorker(provider, element, /*self*/ false))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.ancestors = ancestors;
    function ancestorsAndSelf(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(ancestorsWorker(provider, element, /*self*/ true))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.ancestorsAndSelf = ancestorsAndSelf;
    function ancestorsWorker(provider, element, self) {
        var ancestor;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined)
                        return [2 /*return*/];
                    ancestor = self ? element : provider.parent(element);
                    _a.label = 1;
                case 1:
                    if (!(ancestor !== undefined)) return [3 /*break*/, 3];
                    return [4 /*yield*/, ancestor];
                case 2:
                    _a.sent();
                    ancestor = provider.parent(ancestor);
                    return [3 /*break*/, 1];
                case 3: return [2 /*return*/];
            }
        });
    }
    function descendants(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(descendantsWorker(provider, element, /*self*/ false))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.descendants = descendants;
    function descendantsAndSelf(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(descendantsWorker(provider, element, /*self*/ true))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.descendantsAndSelf = descendantsAndSelf;
    function descendantsWorker(provider, element, self) {
        var e_2, _a, _b, _c, child, e_2_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined)
                        return [2 /*return*/];
                    if (!self) return [3 /*break*/, 2];
                    return [4 /*yield*/, element];
                case 1:
                    _d.sent();
                    _d.label = 2;
                case 2:
                    _d.trys.push([2, 7, 8, 9]);
                    _b = tslib_1.__values(childrenWorker(provider, element)), _c = _b.next();
                    _d.label = 3;
                case 3:
                    if (!!_c.done) return [3 /*break*/, 6];
                    child = _c.value;
                    return [5 /*yield**/, tslib_1.__values(descendantsWorker(provider, child, /*self*/ true))];
                case 4:
                    _d.sent();
                    _d.label = 5;
                case 5:
                    _c = _b.next();
                    return [3 /*break*/, 3];
                case 6: return [3 /*break*/, 9];
                case 7:
                    e_2_1 = _d.sent();
                    e_2 = { error: e_2_1 };
                    return [3 /*break*/, 9];
                case 8:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_2) throw e_2.error; }
                    return [7 /*endfinally*/];
                case 9: return [2 /*return*/];
            }
        });
    }
    function parents(provider, element) {
        var parent;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    parent = provider.parent(element);
                    if (!(parent !== undefined)) return [3 /*break*/, 2];
                    return [4 /*yield*/, parent];
                case 1:
                    _a.sent();
                    _a.label = 2;
                case 2: return [2 /*return*/];
            }
        });
    }
    Axis.parents = parents;
    function self(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [4 /*yield*/, element];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.self = self;
    function siblings(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(siblingsWorker(provider, element, /*self*/ false))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.siblings = siblings;
    function siblingsAndSelf(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(siblingsWorker(provider, element, /*self*/ true))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.siblingsAndSelf = siblingsAndSelf;
    function siblingsWorker(provider, element, self) {
        var e_3, _a, parent, _b, _c, child, e_3_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined)
                        return [2 /*return*/];
                    parent = provider.parent(element);
                    if (!(parent !== undefined)) return [3 /*break*/, 8];
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 6, 7, 8]);
                    _b = tslib_1.__values(childrenWorker(provider, parent)), _c = _b.next();
                    _d.label = 2;
                case 2:
                    if (!!_c.done) return [3 /*break*/, 5];
                    child = _c.value;
                    if (!(self || !internal_1.SameValue(child, element))) return [3 /*break*/, 4];
                    return [4 /*yield*/, child];
                case 3:
                    _d.sent();
                    _d.label = 4;
                case 4:
                    _c = _b.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_3_1 = _d.sent();
                    e_3 = { error: e_3_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_3) throw e_3.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    }
    function siblingsBeforeSelf(provider, element) {
        var e_4, _a, _b, _c, sibling, e_4_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 6, 7, 8]);
                    _b = tslib_1.__values(siblingsWorker(provider, element, /*self*/ true)), _c = _b.next();
                    _d.label = 2;
                case 2:
                    if (!!_c.done) return [3 /*break*/, 5];
                    sibling = _c.value;
                    if (internal_1.SameValue(sibling, element)) {
                        return [2 /*return*/];
                    }
                    return [4 /*yield*/, sibling];
                case 3:
                    _d.sent();
                    _d.label = 4;
                case 4:
                    _c = _b.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_4_1 = _d.sent();
                    e_4 = { error: e_4_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_4) throw e_4.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    }
    Axis.siblingsBeforeSelf = siblingsBeforeSelf;
    function siblingsAfterSelf(provider, element) {
        var e_5, _a, hasSeenSelf, _b, _c, sibling, e_5_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    hasSeenSelf = false;
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 7, 8, 9]);
                    _b = tslib_1.__values(siblingsWorker(provider, element, /*self*/ true)), _c = _b.next();
                    _d.label = 2;
                case 2:
                    if (!!_c.done) return [3 /*break*/, 6];
                    sibling = _c.value;
                    if (!hasSeenSelf) return [3 /*break*/, 4];
                    return [4 /*yield*/, sibling];
                case 3:
                    _d.sent();
                    return [3 /*break*/, 5];
                case 4:
                    hasSeenSelf = internal_1.SameValue(sibling, element);
                    _d.label = 5;
                case 5:
                    _c = _b.next();
                    return [3 /*break*/, 2];
                case 6: return [3 /*break*/, 9];
                case 7:
                    e_5_1 = _d.sent();
                    e_5 = { error: e_5_1 };
                    return [3 /*break*/, 9];
                case 8:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_5) throw e_5.error; }
                    return [7 /*endfinally*/];
                case 9: return [2 /*return*/];
            }
        });
    }
    Axis.siblingsAfterSelf = siblingsAfterSelf;
    function children(provider, element) {
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    if (element === undefined || !owns(provider, element))
                        return [2 /*return*/];
                    return [5 /*yield**/, tslib_1.__values(childrenWorker(provider, element))];
                case 1:
                    _a.sent();
                    return [2 /*return*/];
            }
        });
    }
    Axis.children = children;
    function childrenWorker(provider, element) {
        var e_6, _a, children, _b, _c, child, e_6_1;
        return tslib_1.__generator(this, function (_d) {
            switch (_d.label) {
                case 0:
                    if (element === undefined)
                        return [2 /*return*/];
                    children = provider.children(element);
                    if (children === undefined)
                        return [2 /*return*/];
                    _d.label = 1;
                case 1:
                    _d.trys.push([1, 6, 7, 8]);
                    _b = tslib_1.__values(internal_1.ToIterable(children)), _c = _b.next();
                    _d.label = 2;
                case 2:
                    if (!!_c.done) return [3 /*break*/, 5];
                    child = _c.value;
                    if (!(child !== undefined)) return [3 /*break*/, 4];
                    return [4 /*yield*/, child];
                case 3:
                    _d.sent();
                    _d.label = 4;
                case 4:
                    _c = _b.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_6_1 = _d.sent();
                    e_6 = { error: e_6_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
                    }
                    finally { if (e_6) throw e_6.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    }
    function owns(hierarchy, value) {
        return hierarchy.owns === undefined || hierarchy.owns(value);
    }
})(Axis = exports.Axis || (exports.Axis = {}));
//# sourceMappingURL=axis.js.map