# shellcheck shell=dash disable=2016

___x_cmd_hok_qsearch(){
    local X_help_cmd=; X_help_cmd='___x_cmd help -m hok qs' help:arg:parse

    if ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_chatty ; then
        ___x_cmd_hok_qsearch___fz "$@"
    else
        ___x_cmd_hok_qsearch___raw "$@"
    fi
}

___x_cmd_hok_qsearch___prepare(){
    ___x_cmd hascmd scoop-search.exe || ___x_cmd hok install scoop-search
}

___x_cmd_hok_qsearch___raw(){
    ___x_cmd_hok_qsearch___prepare || return
    scoop-search.exe "$@"
}

___x_cmd_hok_qsearch___fz(){
    local x_=;
    ___x_cmd_scoop___fzf_  ___x_cmd_hok_qsearch___fzlist "$@" || return
    [ -n "$x_" ] || return
    x_="${x_% *}"
    local software="${x_%% *}"
    local bucket="${x_##* }"
    local _id; local _cmd
    ___x_cmd ui select _id,_cmd NEXT    \
        "x hok info     ${bucket}/^${software}$"    \
        "x hok install  ${bucket}/${software}"    \
        "return"    || return
    case "$_id" in
        1)  hok:info "cmd -> $_cmd"; ___x_cmd hok info "${bucket}/^${software}$" ;;
        2)  hok:info "cmd -> $_cmd"; ___x_cmd hok install "${bucket}/${software}" ;;
        *)  return ;;
    esac
}

___x_cmd_hok_qsearch___fzlist(){
    ___x_cmd_hok_qsearch___raw "$@" | ___x_cmd_scoop___parse_qs_list
}
