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

// src/mcp/tools/performance.ts
init_dist();
var checkPerformanceDefinition = {
  name: "check_performance",
  description: "Run performance checks: structural complexity, coupling metrics, and size budgets",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" },
      type: {
        type: "string",
        enum: ["structural", "coupling", "size", "all"],
        description: "Type of performance check (default: all)"
      }
    },
    required: ["path"]
  }
};
async function handleCheckPerformance(input) {
  try {
    const { EntropyAnalyzer } = await import("./dist-I4LWG3IN.js");
    const typeFilter = input.type ?? "all";
    const projectPath = sanitizePath(input.path);
    let entryPoints;
    const configFileResult = findConfigFile(projectPath);
    if (configFileResult.ok) {
      const configResult = loadConfig(configFileResult.value);
      if (configResult.ok) {
        const perfConfig = configResult.value.performance;
        entryPoints = perfConfig?.entryPoints;
      }
    }
    const analyzer = new EntropyAnalyzer({
      rootDir: projectPath,
      ...entryPoints ? { entryPoints } : {},
      analyze: {
        complexity: typeFilter === "all" || typeFilter === "structural",
        coupling: typeFilter === "all" || typeFilter === "coupling",
        sizeBudget: typeFilter === "all" || typeFilter === "size"
      }
    });
    let graphOptions;
    try {
      const { loadGraphStore } = await import("./graph-loader-5MKHAJJF.js");
      const store = await loadGraphStore(projectPath);
      if (store) {
        const { GraphComplexityAdapter, GraphCouplingAdapter } = await import("./dist-D27LJR2T.js");
        const complexityAdapter = new GraphComplexityAdapter(store);
        const couplingAdapter = new GraphCouplingAdapter(store);
        graphOptions = {
          graphComplexityData: complexityAdapter.computeComplexityHotspots(),
          graphCouplingData: couplingAdapter.computeCouplingData()
        };
      }
    } catch {
    }
    const result = await analyzer.analyze(graphOptions);
    return resultToMcpResponse(result);
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}
var getPerfBaselinesDefinition = {
  name: "get_perf_baselines",
  description: "Read current performance baselines from .harness/perf/baselines.json",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" }
    },
    required: ["path"]
  }
};
async function handleGetPerfBaselines(input) {
  try {
    const { BaselineManager } = await import("./dist-I4LWG3IN.js");
    const manager = new BaselineManager(sanitizePath(input.path));
    const baselines = manager.load();
    return resultToMcpResponse(
      Ok(baselines ?? { version: 1, updatedAt: "", updatedFrom: "", benchmarks: {} })
    );
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}
var updatePerfBaselinesDefinition = {
  name: "update_perf_baselines",
  description: "Update performance baselines from benchmark results. Run benchmarks first via CLI.",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" },
      commitHash: { type: "string", description: "Current commit hash for baseline tracking" },
      results: {
        type: "array",
        description: "Array of benchmark results to save as baselines",
        items: {
          type: "object",
          properties: {
            name: { type: "string" },
            file: { type: "string" },
            opsPerSec: { type: "number" },
            meanMs: { type: "number" },
            p99Ms: { type: "number" },
            marginOfError: { type: "number" }
          },
          required: ["name", "file", "opsPerSec", "meanMs", "p99Ms", "marginOfError"]
        }
      }
    },
    required: ["path", "commitHash", "results"]
  }
};
async function handleUpdatePerfBaselines(input) {
  try {
    const { BaselineManager } = await import("./dist-I4LWG3IN.js");
    const manager = new BaselineManager(sanitizePath(input.path));
    manager.save(input.results, input.commitHash);
    const updated = manager.load();
    return resultToMcpResponse(Ok(updated));
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}
var getCriticalPathsDefinition = {
  name: "get_critical_paths",
  description: "List performance-critical functions from @perf-critical annotations and graph inference",
  inputSchema: {
    type: "object",
    properties: {
      path: { type: "string", description: "Path to project root" }
    },
    required: ["path"]
  }
};
async function handleGetCriticalPaths(input) {
  try {
    const { CriticalPathResolver } = await import("./dist-I4LWG3IN.js");
    const resolver = new CriticalPathResolver(sanitizePath(input.path));
    const result = await resolver.resolve();
    return resultToMcpResponse(Ok(result));
  } catch (error) {
    return {
      content: [
        {
          type: "text",
          text: `Error: ${error instanceof Error ? error.message : String(error)}`
        }
      ],
      isError: true
    };
  }
}

export {
  checkPerformanceDefinition,
  handleCheckPerformance,
  getPerfBaselinesDefinition,
  handleGetPerfBaselines,
  updatePerfBaselinesDefinition,
  handleUpdatePerfBaselines,
  getCriticalPathsDefinition,
  handleGetCriticalPaths
};
