from __future__ import annotations

from dataclasses import dataclass

from flow_xhs.cli.spec.enums import ParameterKind
from flow_xhs.cli.spec.model import CliSpec


@dataclass(frozen=True)
class GlobalOptionRegistry:
    help_flags: frozenset[str]
    value_options: frozenset[str]
    flag_options: frozenset[str]


def build_global_option_registry(spec: CliSpec) -> GlobalOptionRegistry:
    help_flags: set[str] = set()
    value_options: set[str] = set()
    flag_options: set[str] = set()

    for option in spec.global_options:
        cli_names = tuple(name for name in option.cli_names if name.startswith("-"))
        if not cli_names:
            continue

        if option.kind == ParameterKind.FLAG:
            flag_options.update(cli_names)
        else:
            value_options.update(cli_names)

        if option.id == "help" or "--help" in cli_names or "-h" in cli_names:
            help_flags.update(cli_names)

    flag_options.difference_update(help_flags)
    return GlobalOptionRegistry(
        help_flags=frozenset(help_flags),
        value_options=frozenset(value_options),
        flag_options=frozenset(flag_options),
    )
