#compdef snip

_snip_commands() {
  local -a commands
  commands=(
    'a:Open all snippets with a name that matches the pattern in IDE'
    'all:Open all snippets with a name that matches the pattern in IDE'
    'c:Copy first snippet with name that matches pattern'
    'clip:Copy first snippet with name that matches pattern'
    'e:Open first snippet with name that matches pattern in TUI_EDITOR'
    'edit:Open first snippet with name that matches pattern in TUI_EDITOR'
    'f:Search and lists all snippets with name that matches pattern'
    'fd:Search and lists all snippets with name that matches pattern'
    'g:Grep text inside all snippets'
    'gr:Grep text inside all snippets'
    'h:Display this message'
    'l:List snippets'
    'ls:List snippets'
    'n:New snippet with name'
    'new:New snippet with name'
    'o:Open root folder of snippets using IDE'
    'open:Open root folder of snippets using IDE'
    'r:Grep text inside all snippets'
    'rg:Grep text inside all snippets'
    's:Show all snippets with a name that matches pattern'
    'show:Show all snippets with a name that matches pattern'
  )
  _describe 'commands' commands
}

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

  local -a snips
  snips=("${SNIPPETS_PATH:-${DOTFILES:-${HOME}/.dotfiles}/snippets}"/*(:t:r))

  local -a extensions
  extensions=("${SNIPPETS_PATH:-${DOTFILES:-${HOME}/.dotfiles}/snippets}"/*(:e))
  # ls -A -L -R | grep -oE '\.[^./]+$' | sort | uniq

  _arguments \
    '(-h)-h[Display help]' \
    '(--help)--help[Display help]' \
    '1:: :_snip_commands' \
    '*:: :->args' && ret=0

  case $state in
    args)
      # help) _arguments '1: :_snip_commands' && ret=0 ;;
      case $line[1] in
        c | clip | s | show) compadd $snips ;;
        e | edit | a | all) compadd $extensions ;;
      esac
      ;;
  esac

  return ret
}

_snip "$@"
