import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
import {
  VectorIndex
} from "./chunk-4RMSHZE4.js";
import {
  generateEmbedding
} from "./chunk-RGVBGTD6.js";
import {
  createEmbeddingProvider
} from "./chunk-V5R6O6RP.js";
import {
  searchFts
} from "./chunk-6FQISQNA.js";
import {
  MycoIndex
} from "./chunk-TWSTAVLO.js";
import "./chunk-MWW62YZP.js";
import {
  loadConfig
} from "./chunk-YG6MLLGL.js";
import "./chunk-JSK7L46L.js";
import "./chunk-WBLTISAK.js";
import "./chunk-PZUWP5VK.js";

// src/cli/search.ts
import fs from "fs";
import path from "path";
async function run(args, vaultDir) {
  const query = args.join(" ");
  if (!query) {
    console.error("Usage: myco search <query>");
    process.exit(1);
  }
  const index = new MycoIndex(path.join(vaultDir, "index.db"));
  const vecDb = path.join(vaultDir, "vectors.db");
  if (fs.existsSync(vecDb)) {
    try {
      const config = loadConfig(vaultDir);
      const embeddingProvider = createEmbeddingProvider(config.intelligence.embedding);
      const emb = await generateEmbedding(embeddingProvider, query);
      const vec = new VectorIndex(vecDb, emb.dimensions);
      console.log(`=== Semantic Search: "${query}" ===`);
      const results = vec.search(emb.embedding, { limit: 10 });
      if (results.length === 0) {
        console.log("  (no results)");
      } else {
        const noteMap = new Map(
          index.queryByIds(results.map((r) => r.id)).map((n) => [n.id, n])
        );
        for (const r of results) {
          const title = noteMap.get(r.id)?.title || r.id;
          console.log(`  sim: ${r.similarity.toFixed(3)} | [${r.metadata.type}] ${title.slice(0, 60)}`);
        }
      }
      vec.close();
    } catch (e) {
      console.log(`Semantic search unavailable: ${e.message}`);
    }
  }
  console.log(`
=== FTS Search: "${query}" ===`);
  const ftsResults = searchFts(index, query, { limit: 10 });
  if (ftsResults.length === 0) {
    console.log("  (no results)");
  } else {
    for (const r of ftsResults) {
      console.log(`  [${r.type}] ${r.title?.slice(0, 70)}`);
      if (r.snippet) console.log(`    ${r.snippet.slice(0, 100)}`);
    }
  }
  index.close();
}
async function runVectors(args, vaultDir) {
  const query = args.join(" ");
  if (!query) {
    console.error("Usage: myco vectors <query>");
    process.exit(1);
  }
  const config = loadConfig(vaultDir);
  const embeddingProvider = createEmbeddingProvider(config.intelligence.embedding);
  const emb = await generateEmbedding(embeddingProvider, query);
  const vecDb = path.join(vaultDir, "vectors.db");
  if (!fs.existsSync(vecDb)) {
    console.error("No vector index found");
    process.exit(1);
  }
  const vec = new VectorIndex(vecDb, emb.dimensions);
  const index = new MycoIndex(path.join(vaultDir, "index.db"));
  const results = vec.search(emb.embedding, { limit: 20, relativeThreshold: 0 });
  console.log(`Query: "${query}"`);
  console.log(`Dimensions: ${emb.dimensions}`);
  console.log(`Total vectors: ${vec.count()}`);
  console.log();
  if (results.length === 0) {
    console.log("(no results)");
  } else {
    const noteMap = new Map(
      index.queryByIds(results.map((r) => r.id)).map((n) => [n.id, n])
    );
    const topScore = results[0].similarity;
    console.log(`Top score: ${topScore.toFixed(4)}`);
    console.log(`Default threshold (0.5x): ${(topScore * 0.5).toFixed(4)}`);
    console.log();
    console.log("  Sim     Ratio  Type       ID / Title");
    console.log("  ------  -----  ---------  " + "-".repeat(50));
    for (const r of results) {
      const title = noteMap.get(r.id)?.title || r.id;
      const ratio = (r.similarity / topScore).toFixed(2);
      const pass = r.similarity >= topScore * 0.5 ? "\u2713" : " ";
      console.log(`${pass} ${r.similarity.toFixed(4)}  ${ratio}   ${r.metadata.type.padEnd(9)}  ${title.slice(0, 50)}`);
    }
  }
  vec.close();
  index.close();
}
export {
  run,
  runVectors
};
//# sourceMappingURL=search-SWMJ4MZ3.js.map