"""Shared helpers for resource-type-inheritance BDD step files.

These helpers are used by the split step modules:
- resource_type_inheritance_chain_steps.py
- resource_type_inheritance_merge_steps.py
- resource_type_inheritance_extra_steps.py
"""

from __future__ import annotations

from typing import Any

from cleveragents.tool.registry import ToolRegistry

# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

_NOOP_HANDLER = lambda **kw: None  # noqa: E731


def _make_root_entry(
    *,
    description: str = "Root type",
    handler: str | None = None,
    cli_args: list[dict[str, Any]] | None = None,
    child_types: list[str] | None = None,
    parent_types: list[str] | None = None,
) -> dict[str, Any]:
    """Create a minimal root type registry entry (no inherits)."""
    entry: dict[str, Any] = {"description": description}
    if handler is not None:
        entry["handler"] = handler
    if cli_args is not None:
        entry["cli_args"] = cli_args
    if child_types is not None:
        entry["child_types"] = child_types
    if parent_types is not None:
        entry["parent_types"] = parent_types
    return entry


def _make_child_entry(
    parent: str,
    *,
    description: str | None = None,
    handler: str | None = None,
    cli_args: list[dict[str, Any]] | None = None,
    cli_args_replace: bool = False,
    child_types: list[str] | None = None,
    child_types_replace: bool = False,
    parent_types: list[str] | None = None,
    parent_types_replace: bool = False,
) -> dict[str, Any]:
    """Create a child type registry entry pointing at *parent*."""
    entry: dict[str, Any] = {"inherits": parent}
    if description is not None:
        entry["description"] = description
    if handler is not None:
        entry["handler"] = handler
    if cli_args is not None:
        entry["cli_args"] = cli_args
    if cli_args_replace:
        entry["cli_args_replace"] = True
    if child_types is not None:
        entry["child_types"] = child_types
    if child_types_replace:
        entry["child_types_replace"] = True
    if parent_types is not None:
        entry["parent_types"] = parent_types
    if parent_types_replace:
        entry["parent_types_replace"] = True
    return entry


def _names_from_csv(csv: str) -> list[str]:
    """Split a comma-separated string into stripped tokens."""
    return [t.strip() for t in csv.split(",") if t.strip()]


def _ensure_registry(context: Any) -> dict[str, Any]:
    """Return (and lazily create) the type registry on *context*."""
    if not hasattr(context, "type_inherit_registry"):
        context.type_inherit_registry = {}  # type: ignore[attr-defined]
    return context.type_inherit_registry  # type: ignore[attr-defined]


def _ensure_tool_registry(context: Any) -> ToolRegistry:
    """Return (and lazily create) the ToolRegistry on *context*."""
    if not hasattr(context, "type_inherit_tool_registry"):
        context.type_inherit_tool_registry = ToolRegistry()  # type: ignore[attr-defined]
    return context.type_inherit_tool_registry  # type: ignore[attr-defined]
