import {
  IngestConfigSchema
} from "./chunk-ZCZD7FHU.js";

// src/commands/graph/scan.ts
import { Command } from "commander";
import * as path2 from "path";

// src/commands/graph/ingest-options.ts
import * as fs from "fs";
import * as path from "path";
function loadIngestOptions(projectPath) {
  const configPath = path.join(projectPath, "harness.config.json");
  if (!fs.existsSync(configPath)) return {};
  let raw;
  try {
    raw = JSON.parse(fs.readFileSync(configPath, "utf-8"));
  } catch {
    return {};
  }
  const ingestRaw = raw?.ingest;
  if (ingestRaw === void 0) return {};
  const parsed = IngestConfigSchema.safeParse(ingestRaw);
  if (!parsed.success) return {};
  const out = {};
  if (parsed.data.skipDirs !== void 0) out.skipDirs = parsed.data.skipDirs;
  if (parsed.data.additionalSkipDirs !== void 0)
    out.additionalSkipDirs = parsed.data.additionalSkipDirs;
  if (parsed.data.excludePatterns !== void 0) out.excludePatterns = parsed.data.excludePatterns;
  if (parsed.data.respectGitignore !== void 0)
    out.respectGitignore = parsed.data.respectGitignore;
  return out;
}

// src/commands/graph/scan.ts
async function runScan(projectPath) {
  const {
    GraphStore,
    CodeIngestor,
    TopologicalLinker,
    KnowledgeIngestor,
    GitIngestor,
    RequirementIngestor
  } = await import("./dist-D27LJR2T.js");
  const store = new GraphStore();
  const start = Date.now();
  const ingestOptions = loadIngestOptions(projectPath);
  await new CodeIngestor(store, ingestOptions).ingest(projectPath);
  new TopologicalLinker(store).link();
  const knowledgeIngestor = new KnowledgeIngestor(store);
  await knowledgeIngestor.ingestAll(projectPath);
  const specsDir = path2.join(projectPath, "docs", "changes");
  await new RequirementIngestor(store).ingestSpecs(specsDir);
  try {
    await new GitIngestor(store).ingest(projectPath);
  } catch {
  }
  const graphDir = path2.join(projectPath, ".harness", "graph");
  await store.save(graphDir);
  return { nodeCount: store.nodeCount, edgeCount: store.edgeCount, durationMs: Date.now() - start };
}
function createScanCommand() {
  return new Command("scan").description("Scan project and build knowledge graph").argument("[path]", "Project root path", ".").action(async (inputPath, _opts, cmd) => {
    const projectPath = path2.resolve(inputPath);
    const globalOpts = cmd.optsWithGlobals();
    try {
      const result = await runScan(projectPath);
      if (globalOpts.json) {
        console.log(JSON.stringify(result));
      } else {
        console.log(
          `Graph built: ${result.nodeCount} nodes, ${result.edgeCount} edges (${result.durationMs}ms)`
        );
      }
    } catch (err) {
      console.error("Scan failed:", err instanceof Error ? err.message : err);
      process.exit(2);
    }
  });
}

export {
  loadIngestOptions,
  runScan,
  createScanCommand
};
