import {
  resultToMcpResponse
} from "./chunk-EPUKTTJZ.js";
import {
  sanitizePath
} from "./chunk-W6Y7ZW3Y.js";
import {
  findConfigFile,
  loadConfig
} from "./chunk-A4ETL34H.js";
import {
  init_dist
} from "./chunk-IBTHD2UA.js";
import {
  Ok
} from "./chunk-RL4VMEXL.js";

// src/mcp/tools/docs.ts
init_dist();
import * as path from "path";
function resolveDocsDir(projectPath) {
  const configResult = findConfigFile(projectPath);
  if (configResult.ok) {
    const config = loadConfig(configResult.value);
    if (config.ok) {
      return path.resolve(projectPath, config.value.docsDir);
    }
  }
  return path.resolve(projectPath, "docs");
}
var checkDocsDefinition = {
  name: "check_docs",
  description: "Analyze documentation coverage and/or validate knowledge map integrity",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" },
      domain: { type: "string", description: "Domain/module to check" },
      scope: {
        type: "string",
        enum: ["coverage", "integrity", "all"],
        description: "Scope of check: 'coverage' (doc coverage), 'integrity' (knowledge map validation), 'all' (both). Default: 'coverage'"
      }
    },
    required: ["path"]
  }
};
async function handleCheckDocs(input) {
  try {
    const projectPath = sanitizePath(input.path);
    const scope = input.scope ?? "coverage";
    if (scope === "integrity") {
      const { validateKnowledgeMap } = await import("./dist-CPKL5Y2R.js");
      const result2 = await validateKnowledgeMap(projectPath);
      return resultToMcpResponse(result2);
    }
    if (scope === "all") {
      const { checkDocCoverage: checkDocCoverage2, validateKnowledgeMap } = await import("./dist-CPKL5Y2R.js");
      const domain2 = input.domain ?? "src";
      const { loadGraphStore: loadGraphStore2 } = await import("./graph-loader-QEI7SYOQ.js");
      const store2 = await loadGraphStore2(projectPath);
      let graphCoverage2;
      if (store2) {
        const { Assembler } = await import("./dist-BZS2XRLG.js");
        const assembler = new Assembler(store2);
        const report = assembler.checkCoverage();
        graphCoverage2 = {
          documented: [...report.documented],
          undocumented: [...report.undocumented],
          coveragePercentage: report.coveragePercentage
        };
      }
      const [coverageResult, integrityResult] = await Promise.allSettled([
        checkDocCoverage2(domain2, {
          sourceDir: path.resolve(projectPath, "src"),
          docsDir: resolveDocsDir(projectPath),
          ...graphCoverage2 !== void 0 && { graphCoverage: graphCoverage2 }
        }),
        validateKnowledgeMap(projectPath)
      ]);
      let coverage;
      if (coverageResult.status === "fulfilled") {
        const r = coverageResult.value;
        coverage = r.ok ? r.value : { error: r.error };
      } else {
        coverage = { error: String(coverageResult.reason) };
      }
      let integrity;
      if (integrityResult.status === "fulfilled") {
        const r = integrityResult.value;
        integrity = r.ok ? r.value : { error: r.error };
      } else {
        integrity = { error: String(integrityResult.reason) };
      }
      return resultToMcpResponse(Ok({ coverage, integrity }));
    }
    const { checkDocCoverage } = await import("./dist-CPKL5Y2R.js");
    const domain = input.domain ?? "src";
    const { loadGraphStore } = await import("./graph-loader-QEI7SYOQ.js");
    const store = await loadGraphStore(projectPath);
    let graphCoverage;
    if (store) {
      const { Assembler } = await import("./dist-BZS2XRLG.js");
      const assembler = new Assembler(store);
      const report = assembler.checkCoverage();
      graphCoverage = {
        documented: [...report.documented],
        undocumented: [...report.undocumented],
        coveragePercentage: report.coveragePercentage
      };
    }
    const result = await checkDocCoverage(domain, {
      sourceDir: path.resolve(projectPath, "src"),
      docsDir: resolveDocsDir(projectPath),
      ...graphCoverage !== void 0 && { graphCoverage }
    });
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}

export {
  checkDocsDefinition,
  handleCheckDocs
};
