"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 collections_1 = require("../collections");
var axis_1 = require("./axis");
var toArray_1 = require("./toArray");
var common_1 = require("./common");
function bottomMost(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBeHierarchyIterable(source, "source");
    return new BottomMostIterable(source, predicate);
}
exports.bottomMost = bottomMost;
var BottomMostIterable = /** @class */ (function () {
    function BottomMostIterable(source, predicate) {
        this._source = source;
        this._predicate = predicate;
    }
    BottomMostIterable.prototype[Symbol.iterator] = function () {
        var e_1, _a, source, predicate, hierarchy, bottomMostNodes, ancestors, i, node, j, other, ancestorsOfOther, ancestorsOfNode, bottomMostNodes_1, bottomMostNodes_1_1, node, e_1_1;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    source = this._source;
                    predicate = this._predicate;
                    hierarchy = internal_1.GetHierarchy(source);
                    bottomMostNodes = toArray_1.toArray(source);
                    ancestors = new collections_1.Map();
                    for (i = bottomMostNodes.length - 1; i >= 1; i--) {
                        node = bottomMostNodes[i];
                        for (j = i - 1; j >= 0; j--) {
                            other = bottomMostNodes[j];
                            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;
                            }
                            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--;
                            }
                        }
                    }
                    _b.label = 1;
                case 1:
                    _b.trys.push([1, 6, 7, 8]);
                    bottomMostNodes_1 = tslib_1.__values(bottomMostNodes), bottomMostNodes_1_1 = bottomMostNodes_1.next();
                    _b.label = 2;
                case 2:
                    if (!!bottomMostNodes_1_1.done) return [3 /*break*/, 5];
                    node = bottomMostNodes_1_1.value;
                    if (!predicate(node)) return [3 /*break*/, 4];
                    return [4 /*yield*/, node];
                case 3:
                    _b.sent();
                    _b.label = 4;
                case 4:
                    bottomMostNodes_1_1 = bottomMostNodes_1.next();
                    return [3 /*break*/, 2];
                case 5: return [3 /*break*/, 8];
                case 6:
                    e_1_1 = _b.sent();
                    e_1 = { error: e_1_1 };
                    return [3 /*break*/, 8];
                case 7:
                    try {
                        if (bottomMostNodes_1_1 && !bottomMostNodes_1_1.done && (_a = bottomMostNodes_1.return)) _a.call(bottomMostNodes_1);
                    }
                    finally { if (e_1) throw e_1.error; }
                    return [7 /*endfinally*/];
                case 8: return [2 /*return*/];
            }
        });
    };
    BottomMostIterable.prototype[types_1.Hierarchical.hierarchy] = function () {
        return internal_1.GetHierarchy(this._source);
    };
    BottomMostIterable = tslib_1.__decorate([
        internal_1.ToStringTag("BottomMostIterable")
    ], BottomMostIterable);
    return BottomMostIterable;
}());
//# sourceMappingURL=bottomMost.js.map