"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 axis_1 = require("./axis");
const collections_1 = require("../collections");
const toArrayAsync_1 = require("./toArrayAsync");
const common_1 = require("./common");
function topMostAsync(source, predicate = common_1.T) {
    internal_1.assert.mustBePossiblyAsyncHierarchyIterable(source, "source");
    return new AsyncTopMostIterable(source, predicate);
}
exports.topMostAsync = topMostAsync;
let AsyncTopMostIterable = class AsyncTopMostIterable {
    constructor(source, predicate) {
        this._source = source;
        this._predicate = predicate;
    }
    [Symbol.asyncIterator]() {
        return tslib_1.__asyncGenerator(this, arguments, function* _a() {
            const source = this._source;
            const predicate = this._predicate;
            const hierarchy = internal_1.GetHierarchy(source);
            const topMostNodes = yield tslib_1.__await(toArrayAsync_1.toArrayAsync(source));
            const ancestors = new collections_1.Map();
            for (let i = topMostNodes.length - 1; i >= 1; i--) {
                const node = topMostNodes[i];
                for (let j = i - 1; j >= 0; j--) {
                    const other = topMostNodes[j];
                    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)) {
                        topMostNodes.splice(i, 1);
                        break;
                    }
                    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)) {
                        topMostNodes.splice(j, 1);
                        i--;
                    }
                }
            }
            if (predicate === common_1.T) {
                yield tslib_1.__await(yield* tslib_1.__asyncDelegator(tslib_1.__asyncValues(topMostNodes)));
            }
            else {
                for (const node of topMostNodes) {
                    if (yield tslib_1.__await(predicate(node)))
                        yield yield tslib_1.__await(node);
                }
            }
        });
    }
    [types_1.Hierarchical.hierarchy]() {
        return internal_1.GetHierarchy(this._source);
    }
};
AsyncTopMostIterable = tslib_1.__decorate([
    internal_1.ToStringTag("AsyncTopMostIterable")
], AsyncTopMostIterable);
//# sourceMappingURL=topMostAsync.js.map