"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applySnippetPatch = applySnippetPatch;
const parser_1 = require("./parser");
const patcher_1 = require("./patcher");
const parsing_constants_1 = require("../../src/llm-orchestration/parser/parsing.constants");
const tree_sitter_tokenizer_1 = require("./strategies/tree-sitter-tokenizer");
const tiktoken_tokenizer_1 = require("./strategies/tiktoken-tokenizer");
async function applySnippetPatch(sourceCode, patchCode, optionsOrGrammarPath) {
    let strategy;
    let options;
    if (typeof optionsOrGrammarPath === 'string') {
        options = { grammarPath: optionsOrGrammarPath };
    }
    else {
        options = optionsOrGrammarPath;
    }
    if (options.useTiktoken) {
        strategy = new tiktoken_tokenizer_1.TiktokenTokenizer();
    }
    else if (options.grammarPath) {
        const parser = await (0, parser_1.initializeParser)(options.grammarPath);
        strategy = new tree_sitter_tokenizer_1.TreeSitterTokenizer(parser);
    }
    else {
        strategy = new tiktoken_tokenizer_1.TiktokenTokenizer();
    }
    const sourceTokens = strategy.tokenize(sourceCode);
    let patchResult;
    let processedPatchCode = patchCode;
    const beginOfFileRegex = new RegExp(`//\\s*${parsing_constants_1.SPECIAL_PATCH_BEGIN_FILE_MARKER}.*`);
    const endOfFileRegex = new RegExp(`//\\s*${parsing_constants_1.SPECIAL_PATCH_END_FILE_MARKER}.*`);
    const hasBeginOfFile = beginOfFileRegex.test(patchCode);
    const hasEndOfFile = endOfFileRegex.test(patchCode);
    if (hasBeginOfFile) {
        processedPatchCode = patchCode.replace(beginOfFileRegex, '');
        const patchTokens = strategy
            .tokenize(processedPatchCode)
            .filter((t) => t.text !== '' && t.text !== '\n');
        patchResult = (0, patcher_1.handleBeginOfFilePatch)(sourceTokens, patchTokens);
    }
    else if (hasEndOfFile) {
        processedPatchCode = patchCode.replace(endOfFileRegex, '');
        const patchTokens = strategy
            .tokenize(processedPatchCode)
            .filter((t) => t.text !== '' && t.text !== '\n');
        patchResult = (0, patcher_1.handleEndOfFilePatch)(sourceTokens, patchTokens, sourceCode);
    }
    else {
        processedPatchCode = patchCode.trim();
        const patchTokens = strategy
            .tokenize(processedPatchCode)
            .filter((t) => t.text !== '');
        const isTiktoken = options.useTiktoken || !options.grammarPath;
        if (isTiktoken &&
            patchTokens.length >= 6 &&
            patchTokens.length < sourceTokens.length) {
            const innerTokens = patchTokens.slice(2, -2);
            patchResult = (0, patcher_1.handleStandardPatch)(sourceTokens, innerTokens);
        }
        else {
            patchResult = (0, patcher_1.handleStandardPatch)(sourceTokens, patchTokens);
        }
    }
    const prefix = sourceCode.slice(0, patchResult.replaceStart);
    const suffix = sourceCode.slice(patchResult.replaceEnd);
    const finalPatchContent = processedPatchCode.slice(patchResult.patchInsertStart, patchResult.patchInsertEnd);
    return prefix + finalPatchContent + suffix;
}
//# sourceMappingURL=index.js.map