#!/usr/bin/env bash

if [ -n "$ZSH_NAME" ]; then
    # zsh sets $ZSH_NAME variable so it can be used to detect zsh
    # following enables using bash-completion under zsh
    autoload bashcompinit
    bashcompinit
fi

_minify_complete() {
    local cur prev flags mimes types
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    flags="-a --all --bundle --exclude --ext --include -l --list --match -o --output -p --preserve -q --quiet -r --recursive --type --url -v --verbose --version -w --watch --css-precision --html-keep-comments --html-keep-special-comments --html-keep-default-attrvals --html-keep-document-tags --html-keep-end-tags --html-keep-quotes --html-keep-whitespace --js-precision --js-keep-var-names --js-version --json-precision --json-keep-numbers --svg-keep-comments --svg-precision -s --sync --xml-keep-whitespace"
    types="css html js json svg xml text/css text/html text/javascript application/javascript application/json image/svg+xml text/xml application/xml"

    if echo "${cur}" | grep -Eq '^-'; then
        COMPREPLY=($(compgen -W "${flags}" -- "${cur}"))
    elif echo "${prev}" | grep -Eq '^--type$'; then
        COMPREPLY=($(compgen -W "${types}" -- "${cur}"))
    elif echo "${prev}" | grep -Eq '^--(exclude|include|match|url|css-precision|js-precision|json-precision|svg-precision)$'; then
        compopt +o default
        COMPREPLY=()
    else
        compopt -o default
        COMPREPLY=()
    fi
    return 0
}

complete -F _minify_complete minify
