"""Step definitions for tui_help_panel_overlay_coverage.feature."""

from __future__ import annotations

from behave import given, then, when

from cleveragents.tui.widgets.help_panel_overlay import (
    HelpPanelOverlay,
    resolve_help_context,
)


@when("I resolve help context for an empty prompt")
def step_resolve_help_context_empty(context):
    context.resolved_help_context = resolve_help_context("")


@when('I resolve help context for prompt text "{text}"')
def step_resolve_help_context(context, text):
    context.resolved_help_context = resolve_help_context(text)


@then('the resolved help context should be "{expected}"')
def step_verify_resolved_help_context(context, expected):
    assert context.resolved_help_context == expected


@given("a fresh help panel overlay")
def step_fresh_help_panel_overlay(context):
    context.help_panel = HelpPanelOverlay()


@when('I show help for context "{context_name}"')
def step_show_help_for_context(context, context_name):
    context.help_panel.show_context(context_name)


@when('I toggle help for context "{context_name}"')
def step_toggle_help_for_context(context, context_name):
    context.help_panel.toggle(context_name)


@then('the standalone help panel text should contain "{text}"')
def step_standalone_help_panel_text_contains(context, text):
    assert text in context.help_panel._text, (
        f"Expected {text!r} in help panel text, got:\n{context.help_panel._text}"
    )


@then('the standalone help panel text should not contain "{text}"')
def step_standalone_help_panel_text_not_contains(context, text):
    assert text not in context.help_panel._text, (
        f"Expected {text!r} NOT in help panel text, but found it in:\n{context.help_panel._text}"
    )


@then("the help panel should be visible")
def step_help_panel_visible(context):
    assert context.help_panel.visible is True


@then("the help panel should be hidden")
def step_help_panel_hidden(context):
    assert context.help_panel.visible is False
    assert context.help_panel._text == ""
