#compdef _dunstctl dunstctl

# ZSH arguments completion script for the dunstctl command
# Depends on: gAWK (rule), jq (history-pop)

local curcontext="$curcontext" ret=1
local -a state line subs

_arguments -C \
  '1:cmd:->cmds' \
  '2:opt:->opts' \
  '3:third:->thirds' \
  && ret=0

case $state in
  (cmds)
    local -a commands
    commands=(
      'action:Perform the default action, or open the context menu of the notification at the given position'
      'close:Close the last notification or optionally the notification with given ID'
      'close-all:Close all the notifications'
      'context:Open context menu'
      'count:Show the number of notifications'
      'history:Display notification history (in JSON)'
      'history-pop:Pop the latest notification from history or optionally the notification with given ID'
      'is-paused:Check if pause level is greater than 0'
      'set-paused:Set the pause status'
      'get-pause-level:Get the current pause level'
      'set-pause-level:Set the pause level'
      'rule:Enable or disable a rule by its name'
      'rules:Displays configured rules'
      'reload:Reload the settings of the running instance, optionally with specific configuration files'
      'debug:Print debugging information'
      'help:Show help'
    )
    _describe commands commands && ret=0
    ;;

  (opts)
    case $line[1] in
      count)
        local -a count_opts;
        count_opts=(
          "displayed"
          "history"
          "waiting"
        )

        _describe count_opts count_opts && ret=0
        ;;

      set-paused)
        local -a setpaused_opts;
        setpaused_opts=(
          "true"
          "false"
          "toggle"
        )

        _describe setpaused_opts setpaused_opts && ret=0
        ;;

      rule)
        local -a rules;
        rules=(
          `dunstctl rules --json | jq -r '.data[][].name.data'`
        )
        _describe rules_opts rules && ret=0
        ;;

      history-pop)
         local -a history_ids;
         history_ids=(
           `dunstctl history | jq -M '.data[0][].id.data'`
         )
        _describe history_ids history_ids && ret=0
        ;;

    esac
    ;;

  (thirds)
    case $line[1] in
      rule)
        local -a rulestates_opts;
        rulestates_opts=(
          "enable"
          "disable"
          "toggle"
        )

        _describe rulestates_opts rulestates_opts && ret=0
        ;;

    esac
esac

return ret
