#compdef shellkeys

_shellkeys_commands() {
  local -a commands
  commands=(
    {b,bash}:'List all keybindings (default: emacs)'
    {k,kitten}:'Run kitten show-key (default: normal)'
    {t,tmux}:'List all keybindings (default: default)'
    {z,zsh}:'List all keybindings (default: normal)'
    {h,help}:'Print this message or the help of the given subcommand(s)'
  )
  _describe 'command' commands
}

__shellkeys_bash_user() {
  local -a opts=(
    'all'
    'vicommand'
    'viinsert'
    'macros'
  )
  _wanted opts expl 'options' compadd -S '' ${^opts} && ret=0
}

__shellkeys_zsh_user() {
  local -a opts=(
    'all'
    'main'
    'menu'
    'vicmd'
    'viins'
    'viopp'
    'visual'
  )
  _wanted opts expl 'options' compadd -S '' ${^opts} && ret=0
}

_shellkeys() {
  local context state line curcontext="$curcontext"
  typeset -A opt_args
  local ret=1

  _arguments -C \
    '(- 1 *)'{-h,--help}'[Display help message]' \
    '1: :_shellkeys_commands' \
    '*:: :->command_args' \
    && ret=0

  case $state in
    command_args)
      case "$line[1]" in
        b | bash)
          _arguments \
            '--abcd['\''\e[A'\'' '\''\e[B'\'' '\''\e[C'\'' '\''\e[D'\'' bindings]' \
            {-v,--vi}'[Default vi bindings]' \
            {-s,--special}'[User script bindings]' \
            {-u,--user}'[User bindings \[all vicommand viinsert macros\]]:options:__shellkeys_bash_user' \
            {-h,--help}'[Display help message]' \
            && ret=0
          ;;
        k | kitty)
          _arguments \
            {-k,--kitty}[Show-key with '\''--key-mode kitty'\'']' \
            {-h,--help}'[Display help message]' \
            && ret=0
          ;;
        t | tmux)
          _arguments \
            {-c,--copy}'[Default copy-mode bindings]' \
            {-cv,--vi}'[Default copy-mode-vi bindings]' \
            {-p,--prefix}'[Default prefix bindings]' \
            {-r,--root}'[Default root bindings]' \
            {-s,--special}'[User script bindings]' \
            {-u,--user}'[User bindings]' \
            {-h,--help}'[Display help message]' \
            && ret=0
          ;;
        z | zsh)
          _arguments \
            {-a,--all}'[All default bindings]' \
            {-e,--emacs}'[Default emacs bindings]' \
            {-v,--vi}'[Default vi bindings]' \
            {-s,--special}'[User script bindings]' \
            {-u,--user}'[User bindings \[all main menu vicmd viins viopp visual\]]:options:__shellkeys_zsh_user' \
            {-h,--help}'[Display help message]' \
            && ret=0
          ;;
      esac
      ;;
  esac
  return ret
}

_shellkeys "$@"
