"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 });
const internal_1 = require("../internal");
/** @internal */
var Axis;
(function (Axis) {
    function* root(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        let hasRoot = false;
        let root;
        for (const ancestor of ancestorsAndSelf(provider, element)) {
            hasRoot = true;
            root = ancestor;
        }
        if (hasRoot) {
            yield root;
        }
    }
    Axis.root = root;
    function* ancestors(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* ancestorsWorker(provider, element, /*self*/ false);
    }
    Axis.ancestors = ancestors;
    function* ancestorsAndSelf(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* ancestorsWorker(provider, element, /*self*/ true);
    }
    Axis.ancestorsAndSelf = ancestorsAndSelf;
    function* ancestorsWorker(provider, element, self) {
        if (element === undefined)
            return;
        let ancestor = self ? element : provider.parent(element);
        while (ancestor !== undefined) {
            yield ancestor;
            ancestor = provider.parent(ancestor);
        }
    }
    function* descendants(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* descendantsWorker(provider, element, /*self*/ false);
    }
    Axis.descendants = descendants;
    function* descendantsAndSelf(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* descendantsWorker(provider, element, /*self*/ true);
    }
    Axis.descendantsAndSelf = descendantsAndSelf;
    function* descendantsWorker(provider, element, self) {
        if (element === undefined)
            return;
        if (self) {
            yield element;
        }
        for (const child of childrenWorker(provider, element)) {
            yield* descendantsWorker(provider, child, /*self*/ true);
        }
    }
    function* parents(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        const parent = provider.parent(element);
        if (parent !== undefined) {
            yield parent;
        }
    }
    Axis.parents = parents;
    function* self(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield element;
    }
    Axis.self = self;
    function* siblings(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* siblingsWorker(provider, element, /*self*/ false);
    }
    Axis.siblings = siblings;
    function* siblingsAndSelf(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* siblingsWorker(provider, element, /*self*/ true);
    }
    Axis.siblingsAndSelf = siblingsAndSelf;
    function* siblingsWorker(provider, element, self) {
        if (element === undefined)
            return;
        const parent = provider.parent(element);
        if (parent !== undefined) {
            for (const child of childrenWorker(provider, parent)) {
                if (self || !internal_1.SameValue(child, element)) {
                    yield child;
                }
            }
        }
    }
    function* siblingsBeforeSelf(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        for (const sibling of siblingsWorker(provider, element, /*self*/ true)) {
            if (internal_1.SameValue(sibling, element)) {
                return;
            }
            yield sibling;
        }
    }
    Axis.siblingsBeforeSelf = siblingsBeforeSelf;
    function* siblingsAfterSelf(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        let hasSeenSelf = false;
        for (const sibling of siblingsWorker(provider, element, /*self*/ true)) {
            if (hasSeenSelf) {
                yield sibling;
            }
            else {
                hasSeenSelf = internal_1.SameValue(sibling, element);
            }
        }
    }
    Axis.siblingsAfterSelf = siblingsAfterSelf;
    function* children(provider, element) {
        if (element === undefined || !owns(provider, element))
            return;
        yield* childrenWorker(provider, element);
    }
    Axis.children = children;
    function* childrenWorker(provider, element) {
        if (element === undefined)
            return;
        const children = provider.children(element);
        if (children === undefined)
            return;
        for (const child of internal_1.ToIterable(children)) {
            if (child !== undefined)
                yield child;
        }
    }
    function owns(hierarchy, value) {
        return hierarchy.owns === undefined || hierarchy.owns(value);
    }
})(Axis = exports.Axis || (exports.Axis = {}));
//# sourceMappingURL=axis.js.map