"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.TrackingGroupCollection = exports.TrackingGroup = void 0;
const sortedSegmentSet_1 = require("./sortedSegmentSet");
class TrackingGroup {
    constructor() {
        this.segmentSet = new sortedSegmentSet_1.SortedSegmentSet();
    }
    get segments() {
        return this.segmentSet.items;
    }
    get size() {
        return this.segmentSet.size;
    }
    has(segment) {
        return this.segmentSet.has(segment);
    }
    link(segment) {
        if (!this.segmentSet.has(segment)) {
            this.segmentSet.addOrUpdate(segment);
            segment.trackingCollection.link(this);
        }
    }
    unlink(segment) {
        if (this.segmentSet.remove(segment)) {
            segment.trackingCollection.unlink(this);
        }
    }
}
exports.TrackingGroup = TrackingGroup;
class TrackingGroupCollection {
    constructor(segment) {
        this.segment = segment;
        this.trackingGroups = new Set();
    }
    link(trackingGroup) {
        if (trackingGroup) {
            if (!this.trackingGroups.has(trackingGroup)) {
                this.trackingGroups.add(trackingGroup);
            }
            if (!trackingGroup.has(this.segment)) {
                trackingGroup.link(this.segment);
            }
        }
    }
    unlink(trackingGroup) {
        if (trackingGroup.has(this.segment)) {
            trackingGroup.unlink(this.segment);
        }
        this.trackingGroups.delete(trackingGroup);
    }
    copyTo(segment) {
        this.trackingGroups.forEach((sg) => segment.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;
    }
}
exports.TrackingGroupCollection = TrackingGroupCollection;
//# sourceMappingURL=mergeTreeTracking.js.map