"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 toArrayAsync_1 = require("./toArrayAsync");
var common_1 = require("./common");
function bottomMostAsync(source, predicate) {
    if (predicate === void 0) { predicate = common_1.T; }
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    return new AsyncBottomMostIterable(source, predicate);
}
exports.bottomMostAsync = bottomMostAsync;
var AsyncBottomMostIterable = /** @class */ (function () {
    function AsyncBottomMostIterable(source, predicate) {
        this._source = source;
        this._predicate = predicate;
    }
    AsyncBottomMostIterable.prototype[Symbol.asyncIterator] = function () {
        return tslib_1.__asyncGenerator(this, arguments, function _a() {
            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);
                        return [4 /*yield*/, tslib_1.__await(toArrayAsync_1.toArrayAsync(source))];
                    case 1:
                        bottomMostNodes = _b.sent();
                        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--;
                                }
                            }
                        }
                        if (!(predicate === common_1.T)) return [3 /*break*/, 5];
                        return [5 /*yield**/, tslib_1.__values(tslib_1.__asyncDelegator(tslib_1.__asyncValues(bottomMostNodes)))];
                    case 2: return [4 /*yield*/, tslib_1.__await.apply(void 0, [_b.sent()])];
                    case 3:
                        _b.sent();
                        return [4 /*yield*/, tslib_1.__await(void 0)];
                    case 4: return [2 /*return*/, _b.sent()];
                    case 5:
                        _b.trys.push([5, 12, 13, 14]);
                        bottomMostNodes_1 = tslib_1.__values(bottomMostNodes), bottomMostNodes_1_1 = bottomMostNodes_1.next();
                        _b.label = 6;
                    case 6:
                        if (!!bottomMostNodes_1_1.done) return [3 /*break*/, 11];
                        node = bottomMostNodes_1_1.value;
                        return [4 /*yield*/, tslib_1.__await(predicate(node))];
                    case 7:
                        if (!_b.sent()) return [3 /*break*/, 10];
                        return [4 /*yield*/, tslib_1.__await(node)];
                    case 8: return [4 /*yield*/, _b.sent()];
                    case 9:
                        _b.sent();
                        _b.label = 10;
                    case 10:
                        bottomMostNodes_1_1 = bottomMostNodes_1.next();
                        return [3 /*break*/, 6];
                    case 11: return [3 /*break*/, 14];
                    case 12:
                        e_1_1 = _b.sent();
                        e_1 = { error: e_1_1 };
                        return [3 /*break*/, 14];
                    case 13:
                        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 14: return [2 /*return*/];
                }
            });
        });
    };
    AsyncBottomMostIterable.prototype[types_1.Hierarchical.hierarchy] = function () {
        return internal_1.GetHierarchy(this._source);
    };
    AsyncBottomMostIterable = tslib_1.__decorate([
        internal_1.ToStringTag("AsyncBottomMostIterable")
    ], AsyncBottomMostIterable);
    return AsyncBottomMostIterable;
}());
//# sourceMappingURL=bottomMostAsync.js.map