"use strict";
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.linesWithBackticksRemoved = linesWithBackticksRemoved;
exports.constructMessages = constructMessages;
exports.charCount = charCount;
exports.findMergeConflictMarkersRange = findMergeConflictMarkersRange;
const prompt_tsx_1 = require("@vscode/prompt-tsx");
const globalStringUtils_1 = require("../../../platform/chat/common/globalStringUtils");
const offsetRange_1 = require("../../../util/vs/editor/common/core/ranges/offsetRange");
/**
 * Remove backticks on the first and last lines.
 */
async function* linesWithBackticksRemoved(linesStream) {
    let lineN = -1;
    let bufferedBacktickLine;
    for await (const line of linesStream) {
        ++lineN;
        if (bufferedBacktickLine) {
            yield bufferedBacktickLine;
            bufferedBacktickLine = undefined;
        }
        if (line.match(/^```[a-z]*$/)) {
            if (lineN === 0) {
                continue;
            }
            else {
                // maybe middle of stream or last line
                // we set it to buffer; if it's midle of stream, it will be emitted
                // if last line, it will be omitted
                bufferedBacktickLine = line;
            }
        }
        else {
            yield line;
        }
    }
    // ignore bufferedLine
}
function constructMessages({ systemMsg, userMsg }) {
    return [
        {
            role: prompt_tsx_1.Raw.ChatRole.System,
            content: (0, globalStringUtils_1.toTextParts)(systemMsg)
        },
        {
            role: prompt_tsx_1.Raw.ChatRole.User,
            content: (0, globalStringUtils_1.toTextParts)(userMsg)
        }
    ];
}
function charCount(messages) {
    const promptCharCount = messages.reduce((total, msg) => total + msg.content.reduce((subtotal, part) => subtotal + (part.type === prompt_tsx_1.Raw.ChatCompletionContentPartKind.Text ? part.text.length : 0), 0), 0);
    return promptCharCount;
}
/**
 * Finds the range of lines containing merge conflict markers within a specified edit window.
 *
 * @param lines - Array of strings representing the lines of text to search through
 * @param editWindowRange - The range within which to search for merge conflict markers
 * @param maxMergeConflictLines - Maximum number of lines to search for conflict markers
 * @returns An OffsetRange object representing the start and end of the conflict markers, or undefined if not found
 */
function findMergeConflictMarkersRange(lines, editWindowRange, maxMergeConflictLines) {
    for (let i = editWindowRange.start; i < Math.min(lines.length, editWindowRange.endExclusive); ++i) {
        if (!lines[i].startsWith('<<<<<<<')) {
            continue;
        }
        // found start of merge conflict markers -- now find the end
        for (let j = i + 1; j < lines.length && (j - i) < maxMergeConflictLines; ++j) {
            if (lines[j].startsWith('>>>>>>>')) {
                return new offsetRange_1.OffsetRange(i, j + 1 /* because endExclusive */);
            }
        }
    }
    return undefined;
}
//# sourceMappingURL=xtabUtils.js.map