"""Editor context tool — get live editor state or filesystem fallback."""

from __future__ import annotations

import os

from godotiq.bridge.session import get_bridge_or_none


async def editor_context() -> dict:
    """Get the current editor state from the Godot addon.

    Returns connected editor state (open scenes, selected nodes, game state,
    project path) when the addon is running, or a filesystem-based fallback
    when disconnected.

    Returns:
        Dict with editor context and addon_connected flag.
    """
    bridge = await get_bridge_or_none()

    if bridge is not None:
        result = await bridge.request("editor_context")
        result["addon_connected"] = True
        return result

    project_path = os.environ.get("GODOTIQ_PROJECT_ROOT", "")
    return {
        "addon_connected": False,
        "project_path": project_path,
        "note": "GodotIQ addon not connected. Editor state unavailable. "
                "Enable the addon in Godot to get live editor context.",
    }
