"""Keyboard helpers for pyratatui KeyEvent values."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Protocol, runtime_checkable


@runtime_checkable
class KeyLike(Protocol):
	@property
	def code(self) -> str: ...

	@property
	def ctrl(self) -> bool: ...

	@property
	def alt(self) -> bool: ...

	@property
	def shift(self) -> bool: ...


@dataclass(frozen=True, slots=True)
class Key:
	"""Test-friendly stand-in for ``pyratatui.KeyEvent``."""

	code: str
	ctrl: bool = False
	alt: bool = False
	shift: bool = False


def key(code: str, *, ctrl: bool = False, alt: bool = False, shift: bool = False) -> Key:
	return Key(code=code, ctrl=ctrl, alt=alt, shift=shift)


def is_quit(ev: KeyLike) -> bool:
	return ev.code.casefold() == "q" or (ev.code == "c" and ev.ctrl)


def is_up(ev: KeyLike) -> bool:
	return ev.code in {"Up", "k", "K"}


def is_down(ev: KeyLike) -> bool:
	return ev.code in {"Down", "j", "J"}


def is_left(ev: KeyLike) -> bool:
	return ev.code in {"Left", "h", "H"}


def is_right(ev: KeyLike) -> bool:
	return ev.code in {"Right", "l", "L"}


def is_enter(ev: KeyLike) -> bool:
	return ev.code == "Enter"


def is_esc(ev: KeyLike) -> bool:
	return ev.code == "Esc"


def is_tab(ev: KeyLike) -> bool:
	return ev.code == "Tab" and not ev.shift


def is_backtab(ev: KeyLike) -> bool:
	return ev.code in {"BackTab", "Backtab"} or (ev.code == "Tab" and ev.shift)


def is_backspace(ev: KeyLike) -> bool:
	return ev.code == "Backspace"


def is_space(ev: KeyLike) -> bool:
	return ev.code == " "


def printable_char(ev: KeyLike) -> str | None:
	if ev.ctrl or ev.alt:
		return None
	if len(ev.code) == 1 and ev.code.isprintable():
		return ev.code
	return None