"""Helpers for ~/.claude.json MCP server registration checks."""
from __future__ import annotations

import json
from pathlib import Path

from mcp_server.constants import LEGACY_MCP_SERVER_NAME, MCP_SERVER_NAME


def load_claude_json(path: Path | None = None) -> dict | None:
    claude_json = path or Path.home() / ".claude.json"
    if not claude_json.exists():
        return None
    return json.loads(claude_json.read_text())


def mcp_server_registered(config: dict | None) -> bool:
    if not config:
        return False
    servers = config.get("mcpServers", {})
    return MCP_SERVER_NAME in servers or LEGACY_MCP_SERVER_NAME in servers


def resolve_mcp_server_name(config: dict) -> str | None:
    servers = config.get("mcpServers", {})
    if MCP_SERVER_NAME in servers:
        return MCP_SERVER_NAME
    if LEGACY_MCP_SERVER_NAME in servers:
        return LEGACY_MCP_SERVER_NAME
    return None