"""Pure helpers for talk command parsing.

Spec source: vectl step ``fix-parse-slash-command-shell-pure-logic``
requires moving slash-command parsing out of shell layer.
"""

from __future__ import annotations

from deal import post, pre


@pre(lambda message: message is not None)
@post(lambda result: isinstance(result, tuple) and len(result) == 2)
def parse_talk_slash_command(message: str) -> tuple[str | None, str | None]:
    """Parse talk REPL slash command and optional argument.

    >>> parse_talk_slash_command('/approve looks good')
    ('approve', 'looks good')
    >>> parse_talk_slash_command('/reject')
    ('reject', None)
    >>> parse_talk_slash_command('/help')
    ('help', None)
    >>> parse_talk_slash_command('/stop')
    ('stop', None)
    >>> parse_talk_slash_command('hello')
    (None, None)
    """
    stripped = message.strip()
    if not stripped.startswith("/"):
        return (None, None)

    command_token, _, remainder = stripped.partition(" ")
    command = command_token.lower()
    argument = remainder.strip() or None

    command_map: dict[str, str] = {
        "/approve": "approve",
        "/reject": "reject",
        "/help": "help",
        "/stop": "stop",
    }
    parsed_command = command_map.get(command)
    if parsed_command is None:
        return (None, None)
    if parsed_command == "help":
        return (parsed_command, None)
    return (parsed_command, argument)
