#compdef antidote

function _antidote_subcommands {
  local usage=$(
    antidote --help |
    ${__adote_awkcmd:-awk} '
      BEGIN{OFS=":"; p=0}
      /^commands:$/ {p=1; next}
      !p{next}
      { for(i=3; i<=NF; i++) { $2=$2" "$i } }
      { print $1,$2 }
    '
  )
  local -a subcommands=("${(@f)usage}")
  _describe -t subcommands 'subcommand' subcommands "$@"
}

function _antidote_installed_bundles {
  local -a bundles=("${(@f)$(antidote list -s)}")
  _describe 'installed bundles' bundles
}

function _antidote_bundle_kinds {
  local -a kinds=(
    'autoload' 'clone' 'defer' 'fpath' 'path' 'zsh'
  )
  _describe 'bundle kinds' kinds
}

function _antidote {
  typeset -A opt_args
  local context state line
  local curcontext="$curcontext"
  local ret=1

  _arguments -C \
    '(- *)'{-v,--version}'[Show version]' \
    '(- *)'{-h,--help}'[Show usage information]' \
    '1: :_antidote_subcommands' \
    '*:: :->subcmds' && return 0

  case "$state" in
    (subcmds)
      case $words[1] in
        (bundle)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (help)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (home)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (init)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (install|script)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            '(-k --kind)'{-k,--kind}'[The kind of bundle]:kinds:_antidote_bundle_kinds' \
            '(-p --path)'{-p,--path}'[A relative subpath within the bundle where the plugin is located]' \
            '(-a --autoload)'{-a,--autoload}'[A relative subpath within the bundle where autoload function files are located]' \
            '(-c --conditional)'{-c,--conditional}'[A conditional function used to check whether to load the bundle]' \
            '(-b --branch)'{-b,--branch}'[The git branch to use]' \
            '(--pre)--pre[A function to be called prior to loading the bundle]' \
            '(--post)--post[A function to be called after loading the bundle]' \
            && ret=0
          ;;
        (list)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            '(-s --short)'{-s,--short}'[Show shortened repos where possible]' \
            '(-d --dirs)'{-d,--dirs}'[Show only bundle directories]' \
            '(-u --url)'{-u,--url}'[Show bundle URLs]' \
            && ret=0
          ;;
        (load)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (path)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            && ret=0
          ;;
        (purge)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            '(-a --all)'{-a,--all}'[Purge all cloned bundles]' \
            "*::antidote bundles:_antidote_installed_bundles" \
            && ret=0
          ;;
        (update)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            '(-s --selp)'{-s,--self}'[Update antidote]' \
            '(-b --bundles)'{-b,--bundles}'[Update bundles]' \
            && ret=0
          ;;
        (*)
          _arguments \
            '(- *)'{-h,--help}'[Show usage information]' \
            '*: :_files' \
            && ret=0
          ;;
      esac
      ;;
  esac

  return ret
}
_antidote "$@"

# vim: ft=zsh sw=2 ts=2 et
