"""Code-generation naming helpers for the build pipeline.

Converts directive and option names between the various forms
needed by generated Python code and test files.
"""

from __future__ import annotations


def directive_to_node_class(name: str) -> str:
    """Convert a directive name to a docutils node class name.

    Node classes in docutils are conventionally lowercase; the kebab
    directive name is transliterated to snake_case and used verbatim.

    >>> directive_to_node_class("h5p-true-false")
    'h5p_true_false'
    >>> directive_to_node_class("h5p-multi-choice")
    'h5p_multi_choice'
    """
    return name.replace("-", "_")


def directive_to_directive_class(name: str) -> str:
    """Convert a directive name to the Python directive class name.

    The segments become PascalCase; the ``H5P`` segment keeps its
    all-caps abbreviation because :meth:`str.title` on ``"h5p"`` gives
    ``"H5P"`` directly (the ``5`` breaks the word).  The ``Directive``
    suffix makes the class distinguishable from the node class.

    >>> directive_to_directive_class("h5p-true-false")
    'H5PTrueFalseDirective'
    >>> directive_to_directive_class("h5p-multi-choice")
    'H5PMultiChoiceDirective'
    """
    parts = name.split("-")
    return "".join(p.title() for p in parts) + "Directive"


def directive_to_snake(name: str) -> str:
    """Convert a directive name to a Python snake_case identifier.

    Used anywhere Python syntax requires a plain identifier derived
    from the directive name (module-level constants, fixture names).

    >>> directive_to_snake("h5p-true-false")
    'h5p_true_false'
    """
    return name.replace("-", "_")


def option_to_test_name(name: str) -> str:
    """Convert a kebab-case option name to a test method suffix.

    Used when the directive generator emits tests named
    ``test_option_<snake>``; the returned suffix keeps the option
    recognisable while being a valid Python identifier.

    >>> option_to_test_name("enable-retry")
    'enable_retry'
    >>> option_to_test_name("feedback-on-correct")
    'feedback_on_correct'
    """
    return name.replace("-", "_")
