/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { TextSegment } from "./textSegment.js";
export class MergeTreeTextHelper {
    constructor(mergeTree) {
        this.mergeTree = mergeTree;
    }
    getText(perspective, placeholder = "", start, end) {
        const range = this.getValidRange(start, end, perspective);
        const accum = { textSegment: new TextSegment(""), placeholder };
        this.mergeTree.mapRange(gatherText, perspective, accum, range.start, range.end);
        return accum.textSegment.text;
    }
    getValidRange(start, end, perspective) {
        const range = {
            end: end ?? this.mergeTree.getLength(perspective),
            start: start ?? 0,
        };
        return range;
    }
}
function gatherText(segment, pos, refSeq, clientId, start, end, { textSegment, placeholder }) {
    if (TextSegment.is(segment)) {
        if (start <= 0 && end >= segment.text.length) {
            textSegment.text += segment.text;
        }
        else {
            const seglen = segment.text.length;
            const _start = start < 0 ? 0 : start;
            const _end = end >= seglen ? undefined : end;
            textSegment.text += segment.text.slice(_start, _end);
        }
    }
    else if (placeholder && placeholder.length > 0) {
        const placeholderText = 
        // eslint-disable-next-line @typescript-eslint/no-base-to-string
        placeholder === "*" ? `\n${segment}` : placeholder.repeat(segment.cachedLength);
        textSegment.text += placeholderText;
    }
    return true;
}
//# sourceMappingURL=MergeTreeTextHelper.js.map