/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
import { TextSegment } from "./textSegment";
export class MergeTreeTextHelper {
    constructor(mergeTree) {
        this.mergeTree = mergeTree;
    }
    getText(refSeq, clientId, placeholder = "", start, end) {
        const range = this.getValidRange(start, end, refSeq, clientId);
        const accum = { textSegment: new TextSegment(""), placeholder };
        this.mergeTree.mapRange(gatherText, refSeq, clientId, accum, range.start, range.end);
        return accum.textSegment.text;
    }
    getValidRange(start, end, refSeq, clientId) {
        const range = {
            end: end !== null && end !== void 0 ? end : this.mergeTree.getLength(refSeq, clientId),
            start: start !== null && start !== void 0 ? 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.substring(_start, _end);
        }
    }
    else if (placeholder && placeholder.length > 0) {
        const placeholderText = placeholder === "*" ?
            `\n${segment}` : placeholder.repeat(segment.cachedLength);
        textSegment.text += placeholderText;
    }
    return true;
}
//# sourceMappingURL=MergeTreeTextHelper.js.map