"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.
 */
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const guards_1 = require("./guards");
const conversion_1 = require("./conversion");
const utils_1 = require("./utils");
const decorators_1 = require("./decorators");
const collections_1 = require("../collections");
/** @internal */ function FlowHierarchy(result, flowLeft, flowRight) {
    if (guards_1.IsPossiblyAsyncHierarchyIterable(result))
        return result;
    const leftHierarchy = guards_1.IsPossiblyAsyncHierarchyIterable(flowLeft) ? utils_1.GetHierarchy(flowLeft) : undefined;
    const rightHierarchy = guards_1.IsPossiblyAsyncHierarchyIterable(flowRight) ? utils_1.GetHierarchy(flowRight) : undefined;
    const hierarchy = CombineHierarchies(leftHierarchy, rightHierarchy);
    if (hierarchy) {
        if (guards_1.IsAsyncIterable(result)) {
            return conversion_1.MakeAsyncHierarchyIterable(result, hierarchy);
        }
    }
    return hierarchy ? guards_1.IsAsyncIterable(result) ? conversion_1.MakeAsyncHierarchyIterable(result, hierarchy) : conversion_1.MakeHierarchyIterable(result, hierarchy) : result;
}
exports.FlowHierarchy = FlowHierarchy;
/** @internal */ function CombineHierarchies(left, right) {
    if (left === right || right === undefined)
        return left;
    if (left === undefined)
        return right;
    const leftHierarchies = left instanceof CompositeHierarchyProvider ? left["_hierarchies"] : [left];
    const rightHierarchies = right instanceof CompositeHierarchyProvider ? right["_hierarchies"] : [right];
    const set = new collections_1.Set(leftHierarchies);
    for (const hierarchy of rightHierarchies)
        set.add(hierarchy);
    if (set.size === leftHierarchies.length)
        return left;
    const hierarchies = [...set];
    return hierarchies.length === 1 ? hierarchies[0] : new CompositeHierarchyProvider(hierarchies);
}
exports.CombineHierarchies = CombineHierarchies;
let CompositeHierarchyProvider = class CompositeHierarchyProvider {
    constructor(hierarchies) {
        this._hierarchies = hierarchies;
    }
    owns(value) {
        return findOwner(this._hierarchies, value) !== undefined;
    }
    parent(value) {
        const hierarchy = findOwner(this._hierarchies, value);
        return hierarchy && hierarchy.parent(value);
    }
    children(value) {
        const hierarchy = findOwner(this._hierarchies, value);
        return hierarchy ? hierarchy.children(value) : [];
    }
};
CompositeHierarchyProvider = tslib_1.__decorate([
    decorators_1.ToStringTag("CompositeHierarchyProvider")
], CompositeHierarchyProvider);
function findOwner(hierarchies, value) {
    let bestMatch;
    for (const hierarchy of hierarchies) {
        if (hierarchy.owns) {
            if (hierarchy.owns(value))
                return hierarchy;
        }
        else if (!bestMatch) {
            bestMatch = hierarchy;
        }
    }
    return bestMatch;
}
//# sourceMappingURL=flow.js.map