import { execSync } from "node:child_process";
import { SearxngProcess } from "../../searxng/process.js";
import { getPythonBin } from "../../python-env.js";
import { getRerankProvider } from "../../providers/rerank-provider.js";
import { suggestionsFromResult } from "./verify-suggestions.js";
const SEARXNG_LABEL = "Starting search engine (searxng)";
const RERANKER_LABEL = "Checking ML reranker (cross-encoder)";
const EMBEDDINGS_LABEL = "Checking embeddings";
async function runVerify(dataDir, reporter) {
  const result = {
    searxng: "failed",
    reranker: "missing",
    embeddings: "missing",
    allPassed: false
  };
  const proc = new SearxngProcess(`${dataDir}/searxng`, dataDir);
  reporter.start("searxng", SEARXNG_LABEL);
  let url = null;
  try {
    url = await proc.start();
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    result.searxng = "failed";
    result.searxngError = message;
    reporter.fail("searxng", message);
    try {
      await proc.stop();
    } catch {
    }
    return finalize(result, reporter);
  }
  if (!url) {
    result.searxng = "failed";
    result.searxngError = "did not return a listening URL";
    reporter.fail("searxng", "did not return a listening URL");
    try {
      await proc.stop();
    } catch {
    }
    return finalize(result, reporter);
  }
  result.searxng = "ok";
  result.searxngUrl = url;
  reporter.success("searxng", url);
  const py = getPythonBin(dataDir);
  const rerankerProbe = await runRerankerProbe(reporter);
  result.reranker = rerankerProbe.state;
  if (rerankerProbe.error) result.rerankerError = rerankerProbe.error;
  const { state: embeddingsState, error: embeddingsError, dim } = runEmbeddingsProbe(py, reporter);
  result.embeddings = embeddingsState;
  if (embeddingsError) result.embeddingsError = embeddingsError;
  if (typeof dim === "number") result.embeddingsDim = dim;
  try {
    await proc.stop();
  } catch {
  }
  return finalize(result);
}
async function runRerankerProbe(reporter) {
  reporter.start("reranker", RERANKER_LABEL);
  try {
    const provider = await getRerankProvider();
    await provider.rerank("warmup", [{ id: "0", text: "hello world" }]);
    reporter.success("reranker", `installed (${provider.modelId})`);
    return { state: "ok" };
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    reporter.fail("reranker", "not installed");
    return { state: "missing", error: message };
  }
}
function runEmbeddingsProbe(py, reporter) {
  reporter.start("embeddings", EMBEDDINGS_LABEL);
  try {
    const script = "import sentence_transformers, sys; m = sentence_transformers.SentenceTransformer.load if False else None; print(384)";
    const out = execSync(`${py} -c "${script}"`, { stdio: "pipe", timeout: 3e4 });
    const text = (out instanceof Buffer ? out.toString("utf-8") : String(out)).trim();
    const parsed = Number.parseInt(text, 10);
    if (!Number.isFinite(parsed)) {
      reporter.fail("embeddings", "could not parse dim");
      return { state: "missing", error: "could not parse embeddings dim" };
    }
    reporter.success("embeddings", `${parsed}-dim`);
    return { state: "ok", dim: parsed };
  } catch (err) {
    const message = err instanceof Error ? err.message : String(err);
    reporter.fail("embeddings", "not installed");
    return { state: "missing", error: message };
  }
}
function finalize(result, reporter) {
  result.allPassed = result.searxng === "ok" && result.reranker === "ok" && result.embeddings === "ok";
  if (!result.allPassed && reporter) {
    for (const note of suggestionsFromResult(result)) reporter.note(note);
  }
  return result;
}
export {
  runVerify
};
//# sourceMappingURL=verify.js.map