"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.SegmentGroupCollection = void 0;
const internal_1 = require("@fluidframework/core-utils/internal");
class SegmentGroupCollection {
    constructor(segment) {
        this.segment = segment;
        this.segmentGroups = new internal_1.DoublyLinkedList();
    }
    get size() {
        return this.segmentGroups.length;
    }
    get empty() {
        return this.segmentGroups.empty;
    }
    enqueue(segmentGroup) {
        this.segmentGroups.push(segmentGroup);
        segmentGroup.segments.push(this.segment);
    }
    dequeue() {
        return this.segmentGroups.shift()?.data;
    }
    remove(segmentGroup) {
        const found = this.segmentGroups.find((v) => v.data === segmentGroup);
        if (found === undefined) {
            return false;
        }
        this.segmentGroups.remove(found);
        return true;
    }
    pop() {
        return this.segmentGroups.pop ? this.segmentGroups.pop()?.data : undefined;
    }
    copyTo(segmentGroups) {
        (0, internal_1.walkList)(this.segmentGroups, (sg) => segmentGroups.enqueueOnCopy(sg.data, this.segment));
    }
    enqueueOnCopy(segmentGroup, sourceSegment) {
        this.enqueue(segmentGroup);
        if (segmentGroup.previousProps) {
            // duplicate the previousProps for this segment
            const index = segmentGroup.segments.indexOf(sourceSegment);
            if (index !== -1) {
                segmentGroup.previousProps.push(segmentGroup.previousProps[index]);
            }
        }
    }
}
exports.SegmentGroupCollection = SegmentGroupCollection;
//# sourceMappingURL=segmentGroupCollection.js.map