#!/usr/bin/env zsh
# Description:
#   Return true if all packages are installed, false otherwise

local     is_verbose=false
local     arg repo
local -aU repos not_installed_repos
local -A  tags

while (( $# > 0 ))
do
    arg="$1"
    case "$arg" in
        --verbose)
            is_verbose=true
            ;;
        -*|--*)
            __zplug::core::options::unknown "$arg"
            return $status
            ;;
        "")
            # Invalid
            return 1
            ;;
        */*)
            repos+=( "${arg:gs:@::}" )
            ;;
        *)
            return 1
            ;;
    esac
    shift
done

if (( $#repos == 0 )); then
    repos=( "${(k)zplugs[@]:gs:@::}" )
fi

for repo in "${repos[@]}"
do
    tags[from]="$(
    __zplug::core::core::run_interfaces \
        'from' \
        "$repo" \
        2> >(__zplug::io::log::capture)
    )"
    if [[ -z "$tags[from]" ]]; then
        not_installed_repos+=( "$repo" )
        continue
    fi

    if __zplug::core::sources::is_handler_defined "check" "$tags[from]"; then
        __zplug::core::sources::use_handler \
            "check" \
            "$tags[from]" \
            "$repo"

        if (( $status != 0 )); then
            not_installed_repos+=( "$repo" )
        fi
    fi
done

if (( $#not_installed_repos > 0 )); then
    # Share not installed repos information
    # e.g. for __install__ command
    reply=( "${not_installed_repos[@]}" )

    if $is_verbose; then
        __zplug::io::print::put \
            "- $fg[red]%s$reset_color: not installed\n" \
            "${not_installed_repos[@]}"
    fi

    return 1
fi

return 0
