import {
  resolveProjectConfig
} from "./chunk-YZH4I4JO.js";
import {
  resultToMcpResponse
} from "./chunk-EPUKTTJZ.js";
import {
  sanitizePath
} from "./chunk-W6Y7ZW3Y.js";

// src/mcp/tools/architecture.ts
var checkDependenciesDefinition = {
  name: "check_dependencies",
  description: "Validate layer boundaries and detect circular dependencies",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" }
    },
    required: ["path"]
  }
};
async function handleCheckDependencies(input) {
  let projectPath;
  try {
    projectPath = sanitizePath(input.path);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
  const configResult = resolveProjectConfig(projectPath);
  if (!configResult.ok) return resultToMcpResponse(configResult);
  try {
    const { validateDependencies, TypeScriptParser } = await import("./dist-CPKL5Y2R.js");
    const config = configResult.value;
    const rawLayers = Array.isArray(config.layers) ? config.layers : [];
    const layers = rawLayers.map((l) => ({
      name: l.name,
      patterns: [l.pattern],
      allowedDependencies: l.allowedDependencies
    }));
    const parser = new TypeScriptParser();
    const { loadGraphStore } = await import("./graph-loader-QEI7SYOQ.js");
    const store = await loadGraphStore(projectPath);
    let graphDependencyData;
    if (store) {
      const { GraphConstraintAdapter } = await import("./dist-BZS2XRLG.js");
      const adapter = new GraphConstraintAdapter(store);
      const graphData = adapter.computeDependencyGraph();
      graphDependencyData = {
        nodes: [...graphData.nodes],
        edges: graphData.edges.map((e) => ({ ...e }))
      };
    }
    const result = await validateDependencies({
      layers,
      rootDir: projectPath,
      parser,
      ...graphDependencyData !== void 0 && { graphDependencyData }
    });
    let designViolations = [];
    if (store) {
      const designTokenNodes = store.findNodes({ type: "design_token" });
      if (designTokenNodes.length > 0) {
        const { DesignConstraintAdapter } = await import("./dist-BZS2XRLG.js");
        const designAdapter = new DesignConstraintAdapter(store);
        const fileNodes = store.findNodes({ type: "file" });
        for (const fileNode of fileNodes) {
          const filePath = fileNode.path ?? fileNode.id;
          if (!/\.(tsx?|jsx?|css|scss|less|vue|svelte)$/.test(filePath)) continue;
          const source = typeof fileNode.metadata?.source === "string" ? fileNode.metadata.source : "";
          if (!source) continue;
          designViolations.push(...designAdapter.checkAll(source, filePath));
        }
      }
    }
    if (result.ok && designViolations.length > 0) {
      const merged = {
        ...result.value,
        designViolations
      };
      return resultToMcpResponse({ ok: true, value: merged });
    }
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}

export {
  checkDependenciesDefinition,
  handleCheckDependencies
};
