/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { RedBlackTree } from "./rbTree";
/**
 * @deprecated  for internal use only. public export will be removed.
 * @internal
 */
export const integerRangeToString = (range) => `[${range.start},${range.end})`;
const intervalComparer = (a, b) => a.compare(b);
/**
 * @deprecated  for internal use only. public export will be removed.
 * @internal
 */
export class IntervalTree {
    constructor() {
        this.intervals = new RedBlackTree(intervalComparer, this);
    }
    remove(x) {
        this.intervals.remove(x);
    }
    removeExisting(x) {
        this.intervals.removeExisting(x);
    }
    put(x, conflict) {
        let rbConflict;
        if (conflict) {
            rbConflict = (key, currentKey) => {
                const ival = conflict(key, currentKey);
                return {
                    key: ival,
                };
            };
        }
        this.intervals.put(x, { minmax: x.clone() }, rbConflict);
    }
    map(fn) {
        const actions = {
            infix: (node) => {
                fn(node.key);
                return true;
            },
            showStructure: true,
        };
        this.intervals.walk(actions);
    }
    mapUntil(fn) {
        const actions = {
            infix: (node) => {
                return fn(node.key);
            },
            showStructure: true,
        };
        this.intervals.walk(actions);
    }
    mapBackward(fn) {
        const actions = {
            infix: (node) => {
                fn(node.key);
                return true;
            },
            showStructure: true,
        };
        this.intervals.walkBackward(actions);
    }
    // TODO: toString()
    match(x) {
        return this.intervals.gather(x, this);
    }
    matchNode(node, key) {
        return !!node && node.key.overlaps(key);
    }
    continueSubtree(node, key) {
        return !!node && node.data.minmax.overlaps(key);
    }
    update(node) {
        if (node.left && node.right) {
            node.data.minmax = node.key.union(node.left.data.minmax.union(node.right.data.minmax));
        }
        else {
            if (node.left) {
                node.data.minmax = node.key.union(node.left.data.minmax);
            }
            else if (node.right) {
                node.data.minmax = node.key.union(node.right.data.minmax);
            }
            else {
                node.data.minmax = node.key.clone();
            }
        }
    }
}
//# sourceMappingURL=intervalTree.js.map