"use strict";
/*!
 * Copyright (c) Microsoft Corporation and contributors. All rights reserved.
 * Licensed under the MIT License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.MergeTreeTextHelper = void 0;
const textSegment_js_1 = require("./textSegment.js");
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_js_1.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 ?? this.mergeTree.getLength(refSeq, clientId),
            start: start ?? 0,
        };
        return range;
    }
}
exports.MergeTreeTextHelper = MergeTreeTextHelper;
function gatherText(segment, pos, refSeq, clientId, start, end, { textSegment, placeholder }) {
    if (textSegment_js_1.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