"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 collections_1 = require("../collections");
const axis_1 = require("./axis");
const toArray_1 = require("./toArray");
const common_1 = require("./common");
function bottomMost(source, predicate = common_1.T) {
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    return new BottomMostIterable(source, predicate);
}
exports.bottomMost = bottomMost;
let BottomMostIterable = class BottomMostIterable {
    constructor(source, predicate) {
        this._source = source;
        this._predicate = predicate;
    }
    *[Symbol.iterator]() {
        const source = this._source;
        const predicate = this._predicate;
        const hierarchy = internal_1.GetHierarchy(source);
        const bottomMostNodes = toArray_1.toArray(source);
        const ancestors = new collections_1.Map();
        for (let i = bottomMostNodes.length - 1; i >= 1; i--) {
            const node = bottomMostNodes[i];
            for (let j = i - 1; j >= 0; j--) {
                const other = bottomMostNodes[j];
                let ancestorsOfOther = ancestors.get(other);
                if (!ancestorsOfOther) {
                    ancestorsOfOther = new collections_1.Set(axis_1.Axis.ancestors(hierarchy, other));
                    ancestors.set(other, ancestorsOfOther);
                }
                if (ancestorsOfOther.has(node)) {
                    bottomMostNodes.splice(i, 1);
                    break;
                }
                let ancestorsOfNode = ancestors.get(node);
                if (!ancestorsOfNode) {
                    ancestorsOfNode = new collections_1.Set(axis_1.Axis.ancestors(hierarchy, node));
                    ancestors.set(node, ancestorsOfNode);
                }
                if (ancestorsOfNode.has(other)) {
                    bottomMostNodes.splice(j, 1);
                    i--;
                }
            }
        }
        for (const node of bottomMostNodes) {
            if (predicate(node))
                yield node;
        }
    }
    [types_1.Hierarchical.hierarchy]() {
        return internal_1.GetHierarchy(this._source);
    }
};
BottomMostIterable = tslib_1.__decorate([
    internal_1.ToStringTag("BottomMostIterable")
], BottomMostIterable);
//# sourceMappingURL=bottomMost.js.map