import { g as getBrainAccessor } from './memory-accessor2-BzlgmY-Z.js';
import { t as typedAll } from './typed-query-l1D_QElt.js';
import { isEmbeddingAvailable, embedText } from './brain-embedding-Djw9ekXu.js';

//#region ../core/dist/memory/brain-similarity.js
/**
* BRAIN Vector Similarity Search
*
* Provides vector-based KNN search against brain_embeddings vec0 table.
* Falls back gracefully when no embedding provider is registered or
* sqlite-vec is unavailable.
*
* @task T5388
* @epic T5149
*/
function parseIdPrefix(id) {
	if (id.startsWith("D-") || /^D\d/.test(id)) return "decision";
	if (id.startsWith("P-") || /^P\d/.test(id)) return "pattern";
	if (id.startsWith("L-") || /^L\d/.test(id)) return "learning";
	if (id.startsWith("O-") || id.startsWith("O") || id.startsWith("CM-")) return "observation";
	return null;
}
/**
* Search for entries similar to a query string using vector similarity.
*
* 1. Embeds the query text via the registered embedding provider.
* 2. Runs KNN query against brain_embeddings vec0 table.
* 3. Joins with observation/decision/pattern/learning tables for full entries.
*
* Returns empty array when embedding is unavailable (graceful fallback).
*
* @param query - Text to find similar entries for
* @param projectRoot - Project root directory
* @param limit - Maximum results to return (default 10)
* @returns Array of similar entries ranked by distance (ascending)
*/
async function searchSimilar(query, projectRoot, limit) {
	if (!query?.trim()) return [];
	if (!isEmbeddingAvailable()) return [];
	const maxResults = limit ?? 10;
	const queryVector = await embedText(query);
	if (!queryVector) return [];
	const { getBrainDb, getBrainNativeDb, isBrainVecLoaded } = await import('./memory-sqlite-Dg5uGJCo.js');
	await getBrainDb(projectRoot);
	const nativeDb = getBrainNativeDb();
	if (!nativeDb) return [];
	if (!isBrainVecLoaded()) return [];
	let knnRows;
	try {
		knnRows = typedAll(nativeDb.prepare("SELECT id, distance FROM brain_embeddings WHERE embedding MATCH ? ORDER BY distance LIMIT ?"), new Float32Array(queryVector.buffer, queryVector.byteOffset, queryVector.length), maxResults);
	} catch {
		return [];
	}
	if (knnRows.length === 0) return [];
	const accessor = await getBrainAccessor(projectRoot);
	const results = [];
	for (const row of knnRows) {
		const entryType = parseIdPrefix(row.id);
		if (!entryType) continue;
		try {
			switch (entryType) {
				case "observation": {
					const entry = await accessor.getObservation(row.id);
					if (entry) results.push({
						id: row.id,
						distance: row.distance,
						type: "observation",
						title: entry.title,
						text: entry.narrative ?? entry.title
					});
					break;
				}
				case "decision": {
					const entry = await accessor.getDecision(row.id);
					if (entry) results.push({
						id: row.id,
						distance: row.distance,
						type: "decision",
						title: entry.decision,
						text: `${entry.decision} — ${entry.rationale}`
					});
					break;
				}
				case "pattern": {
					const entry = await accessor.getPattern(row.id);
					if (entry) results.push({
						id: row.id,
						distance: row.distance,
						type: "pattern",
						title: entry.pattern,
						text: `${entry.pattern} — ${entry.context}`
					});
					break;
				}
				case "learning": {
					const entry = await accessor.getLearning(row.id);
					if (entry) results.push({
						id: row.id,
						distance: row.distance,
						type: "learning",
						title: entry.insight,
						text: `${entry.insight} (source: ${entry.source})`
					});
					break;
				}
			}
		} catch {}
	}
	return results;
}

export { searchSimilar as s };
//# sourceMappingURL=brain-similarity-B2HLXCge.js.map
