# shellcheck shell=dash

___x_cmd_dnf_install(){
    case "$1" in
        -h|--help) ___x_cmd help -m dnf install ; return 0 ;;
    esac

    local ___X_CMD_DNF___FILTER="${___X_CMD_DNF___FILTER}"
    if [ $# -eq 0 ]; then
        ___x_cmd_dnf_install___app
    else
        ___x_cmd_dnf___exec install "$@"
    fi
}

___x_cmd_dnf_install___app(){
    # TODO: fedora39 -- /var/cache/dnf/fedora.solv
    [ -f "/var/cache/dnf/fedora.solv" ] || [ -d "/var/cache/libdnf5" ] || {
        dnf:info "The first use of dnf is detected and the local software repository list is ready to be updated."
        ___x_cmd dnf makecache --refresh || return 1
        ___x_cmd_dnf___repo_lsname > "$___X_CMD_DNF_CACHE_PATH/lsname"
    }

    { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; } \
        || {
            ___x_cmd_dnf___lsraw
            return 0
        }

    dnf:info "Use 'x dnf -h' for help."
    dnf:info "Select a package to install, or view information."

    local software=
    local x_=
    if [ "$___X_CMD_DNF___FILTER" = "fzf" ];then
        software="$( ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" ___x_cmdexe dnf --fzdata )" || return $?
    else
        ___x_cmd_dnf___lsraw_ || return $?
        local datafile="$x_"
        x_=; ___x_cmd storeread_ ___x_cmd pick --width 40 --col 10 --datafile "$datafile"
        software="$x_"
    fi

    [ -n "$software" ] || {
            dnf:info "Exit because no software is selected."
            return 1
        }

    local id=
    ___x_cmd ui select id "What to do NEXT"    \
        "x dnf install   $software"     \
        "x dnf reinstall $software"     \
        "x dnf info      $software"     \
        "browse repo of  $software"      \
        "dnf --help"                    \
        "EXIT"     || return $?
    case "$id" in
        1)      ___x_cmd dnf install   "$software" ;;
        2)      ___x_cmd dnf reinstall "$software" ;;
        3)      ___x_cmd dnf info      "$software" ;;
        4)      ___x_cmd_dnf___repo_browse "$software" ;;
        5)      ___x_cmd help -m dnf ;;
        6)      return 0 ;;
    esac
}

___x_cmd_dnf_install___fz_data(){
    ___x_cmd_dnf___lsraw \
        | ___x_cmd_dnf_install___fzf            \
            --preview='___x_cmdexe dnf --fzfpreview {1}'  \
            --preview-window=right:70%:wrap
}

___x_cmd_dnf_install___fzf_preview(){
    ___x_cmd dnf info "${1}"
}

___x_cmd_dnf_install___fzf(){
    FZF_DEFAULT_OPTS="
--ansi
--reverse
--height='80%'
--bind='ctrl-w:toggle-preview-wrap'
--bind='ctrl-r:change-preview-window(right,70%|down,40%,99%,border-horizontal|hidden|right)'
"   ___x_cmd fzf "$@"
}

