
___x_cmd_mac_as_ls(){
    [ $# -gt 0 ]    ||  set -- --auto

    local op="$1"; shift
    case "$op" in
        -h|--help)      ___x_cmd help -m mac as ls "$@"; return 0 ;;

        --fzdata)       ___x_cmd_mac_as_ls___fz_data    "$@" ;;
        --fz)           ___x_cmd_mac_as_ls___fzapp      "$@" ;;

        -c|--csv)       ___x_cmd_mac_as_ls___csv        "$@" ;;
        -t|--tsv)       ___x_cmd_mac_as_ls___tsv        "$@" ;;
        --auto)         ___x_cmd_mac_as_ls___auto       "$@" ;;
        --raw)          ___x_cmd_mac_as_ls___raw        "$@" ;;
        *)              ___x_cmd_mac_as_ls___auto "$op" "$@" ;;
    esac
}

___x_cmd_mac_as_ls___raw(){
    ___x_cmd_mac_as___runcmd list "$@"
}

___x_cmd_mac_as_ls___tsv(){
    ___x_cmd_mac_as_ls___raw "$@" | ___x_cmd_cmds awk -f "$___X_CMD_ROOT_MOD/mac/lib/awk/as_ls.awk"
}

___x_cmd_mac_as_ls___csv(){
    ___x_cmd_mac_as_ls___tsv "$@" | ___x_cmd tsv tocsv
}

___x_cmd_mac_as_ls___auto(){
    if ___x_cmd_is_stdout2tty; then
        ___x_cmd_mac_as_ls___fzapp "$@"
    else
        ___x_cmd_mac_as_ls___raw "$@"
    fi
}

___x_cmd_mac_as_ls___fzapp(){
    local x_=""; ___x_cmd_mac_as_ls___fzapp_ "$@" || return $?

    mac:info "Selected ==> $x_"
    local app_id="$(printf "$x_" | ___x_cmd_cmds awk '{print $1}')"
    local id=
    ___x_cmd ui select id "NEXT" \
        "x mac as info $app_id"         \
        "x mac as open $app_id"         \
        "x mac as reset $app_id"        \
        "x mac as seller $app_id"       \
        "x mac as upgrade $app_id"      \
        "x mac as uninstall $app_id"    \
        "return"     || return $?

    case "$id" in
        1)      ___x_cmd mac as info     "$app_id" ;;
        2)      ___x_cmd mac as open     "$app_id" ;;
        3)      ___x_cmd mac as reset    "$app_id" ;;
        4)      ___x_cmd mac as seller   "$app_id" ;;
        5)      ___x_cmd mac as upgrade  "$app_id" ;;
        6)      ___x_cmd mac as uninstal "$app_id" ;;
        *)      return 0 ;;
    esac
}

___x_cmd_mac_as_ls___fzapp_(){
    x_="";___x_cmd_runmode_allow_manual || N=mac M="Not supported in non-interactive mode" log:ret:64
    mac:info "Find installed software"
    x_="$( ___X_CMD_RUNMODE="$___X_CMD_RUNMODE" ___x_cmdexe mac as ls --fzdata ___x_cmd_mac_as_ls___tsv "$@" )" || return $?
    [ -n "$x_" ] || return 1
}

___x_cmd_mac_as_ls___fz_data(){
    ___x_cmd_mac_as___install || return $?
    "$@" \
        | ___x_cmd_cmds awk -F'\t' -f "$___X_CMD_ROOT_MOD/mac/lib/awk/as_fzdata.awk" \
        | ___x_cmd fzf \
            --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)' \
            --preview='___x_cmdexe mac as info {1} 2>/dev/null ' \
            --preview-window=right:70%:wrap
}
