"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.textSearch = textSearch;
const embeddings_1 = require("../../embeddings");
const execCommand_1 = require("./execCommand");
async function textSearch(searchTerm) {
    try {
        const normalized = String(searchTerm)
            .replace(/\r\n?/g, "\n")
            .replace(/\n/g, " ")
            .replace(/\t/g, " ")
            .replace(/\s+/g, " ")
            .trim();
        const escapedTerm = normalized.replace(/'/g, "'\\''");
        const command = `ag -m 3 -Q '${escapedTerm}'`;
        return await (0, execCommand_1.execAsync)(command);
    }
    catch (err) {
        console.log("Falling back to embeddings text search since ag was not available");
        const searchTermLower = searchTerm.toLowerCase();
        const embeddings = await (0, embeddings_1.getConfiguredEmbeddings)();
        const results = embeddings.filter((embedding) => embedding.text.toLowerCase().includes(searchTermLower));
        results.forEach((r) => delete r.vector);
        return results;
    }
}
//# sourceMappingURL=textSearch.js.map