/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { SortedSegmentSet } from "./sortedSegmentSet";
export class TrackingGroup {
    constructor() {
        this.trackedSet = new SortedSegmentSet();
    }
    /**
     * @deprecated - use tracked instead.
     * For references positions this will return the underlying segment,
     * which may not match the intention
     */
    get segments() {
        // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
        return this.trackedSet.items.map((v) => (v.isLeaf() ? v : v.getSegment()));
    }
    get tracked() {
        return this.trackedSet.items;
    }
    get size() {
        return this.trackedSet.size;
    }
    has(trackable) {
        return this.trackedSet.has(trackable);
    }
    link(trackable) {
        if (!this.trackedSet.has(trackable)) {
            this.trackedSet.addOrUpdate(trackable);
            trackable.trackingCollection.link(this);
        }
    }
    unlink(trackable) {
        if (this.trackedSet.remove(trackable)) {
            trackable.trackingCollection.unlink(this);
            return true;
        }
        return false;
    }
}
export class TrackingGroupCollection {
    constructor(trackable) {
        this.trackable = trackable;
        this.trackingGroups = new Set();
    }
    link(trackingGroup) {
        if (trackingGroup) {
            if (!this.trackingGroups.has(trackingGroup)) {
                this.trackingGroups.add(trackingGroup);
            }
            if (!trackingGroup.has(this.trackable)) {
                trackingGroup.link(this.trackable);
            }
        }
    }
    unlink(trackingGroup) {
        if (this.trackingGroups.has(trackingGroup)) {
            if (trackingGroup.has(this.trackable)) {
                trackingGroup.unlink(this.trackable);
            }
            this.trackingGroups.delete(trackingGroup);
            return true;
        }
        return false;
    }
    copyTo(trackable) {
        this.trackingGroups.forEach((sg) => {
            trackable.trackingCollection.link(sg);
        });
    }
    get empty() {
        return this.trackingGroups.size === 0;
    }
    matches(trackingCollection) {
        if (!trackingCollection ||
            this.trackingGroups.size !== trackingCollection.trackingGroups.size) {
            return false;
        }
        for (const tg of this.trackingGroups.values()) {
            if (!trackingCollection.trackingGroups.has(tg)) {
                return false;
            }
        }
        return true;
    }
}
//# sourceMappingURL=mergeTreeTracking.js.map