"""Micro editor detection - extracts editor state for other detectors."""

from ..models import Context
from .state import DetectionState


def get_micro_hint(mode: str = "reference") -> str | None:
    """Get activation hint for micro detector."""
    if mode == "reference":
        return "Opening a file in micro editor"
    return None


def detect_micro(state: DetectionState) -> Context | None:
    """Detect micro editor and extract language information into state.

    Micro shows a status line like:
    src/cheatly/config.py (1,1) | ft:python | unix | utf-8

    This detector:
    1. Detects micro editor
    2. Extracts language from status line into state
    3. Other detectors (python.py, etc) can use this info

    Args:
        state: Detection state with environment

    Returns:
        Micro editor context if detected
    """
    command = state.get("pane_command")

    if not command or command != "micro":
        return None

    content = state.get("pane_content")
    if content:
        lines = content.strip().split("\n")
        if len(lines) >= 1:
            status_line = lines[-1]
            if "| ft:" in status_line:
                parts = status_line.split("| ft:")
                if len(parts) > 1:
                    lang_part = parts[1].split("|")[0].strip()
                    if lang_part:
                        state.set("editor_language", lang_part)
                        state.set("editor", "micro")

    return Context(
        name="micro",
        type="editor",
        priority=10,
        sheet="micro",
        metadata={"command": command},
    )
