# shellcheck shell=dash disable=SC2317
___x_cmd_ls_app(){
    local ___X_CMD_TUI_NAVI_CUR_FILE=
    local ___X_CMD_TUI_NAVI_FINAL_COMMAND=
    local ___X_CMD_TUI_APP_IS_SMALL_SCREEN=

    local x_=; ___x_cmd storerat_ ___x_cmd_ls_app_ "$@"     || return $?
    [ -n "$x_" ] || return
    eval "$x_"
    case "$___X_CMD_TUI_NAVI_FINAL_COMMAND" in
        q)  ;;      # Just quit
        d)  ;;      # remove
        r)  ;;      # rename
        *)
            if [ ! -t 1 ]; then
                printf "%s\n" "$___X_CMD_TUI_NAVI_CUR_FILE"
            else
                ___x_cmd_ls_app_handle "$___X_CMD_TUI_NAVI_CUR_FILE"
            fi
            ;;
    esac
}

___x_cmd_ls_app_handle(){
    local fp="$1"
    local id=""
    if [ -d "$fp" ]; then
        ___x_cmd ui select id "Action for [dir=$fp]"   \
            "cd to the directory"               \
            "open directory using Explorer"     \
            "x stat <dir>"                      \
            "remove the directory recursively"   \
            "echo directory path"             \
            "EXIT" || return $?

        case "$id" in
            1)      ___x_cmd cd "$fp" ;;
            2)      ___x_cmd open "$fp" ;;
            3)      x:info "Stat of [dir=$fp]"
                    ___x_cmd stat "$fp" ;;
            4)      ! ___x_cmd ui yesno -r "Confirm to recursively removing [dir=$fp] ?" \
                        || ___x_cmd rmrf "$fp"
                    ;;
            5)      printf "%s\n" "$fp" ;;
            *)      return 1 ;;
        esac
    elif [ -f "$fp" ]; then
        ___x_cmd ui select id "Action for [file=$fp]"              \
            "cd to ${fp%/*}"                                \
            "open containing folder -> ${fp%/*}"            \
            "x stat $fp"                                    \
            "open file using system default application"    \
            "rm -f $fp"                                     \
            "echo file path"                                \
            "EXIT" || return

        case "$id" in
            1)      ___x_cmd cd "${fp%/*}"             ;;
            2)      ___x_cmd open "${fp%/*}"           ;;
            3)      x:info "Stat of [file=$fp]"
                    ___x_cmd stat "$fp"                ;;
            4)      ___x_cmd open "$fp"                ;;
            5)      ! ___x_cmd ui yesno -r "Confirm to removing [file=$fp] ?" \
                        || ___x_cmd_cmds rm -f "$fp"
                    ;;
            6)      printf "%s\n" "$fp" ;;
            *)      return 1 ;;
        esac
    fi
}

___x_cmd_ls_app_(){
    ___X_CMD_LS_APP_BASEDATA="$___X_CMD_LS_APP_BASEDATA" \
    ___X_CMD_SHBIN_CODE='
        xrc ls ;
        "$@"
    ' ___x_cmd antizshbin ___x_cmd_ls_app___ "$@"
}

___x_cmd_ls_app___(){
    local basepath=; local x_=;
    if [ -z "$___X_CMD_LS_APP_BASEDATA" ]; then
        x_=; ___x_cmd abspath_ "${1:-"$PWD"}"
        [ -d "$x_" ]    || return 1
        basepath="$x_"
    fi

    x_=; ___x_cmd tui position get_ lsapp
    local position="$x_"
    local position_isfuzzy=1    # not case sensitive

    local ___X_CMD_LS_APP_TMP_EXITCODE=
    local ___X_CMD_TUI_CURRENT_NAVI_POSITION=
    ___x_cmd_tui_app_processer(){ ___x_cmd_ls_app_processer "$@"; }
    ___X_CMD_LS_APP_BASEPATH="$basepath" \
    ___X_CMD_LS_APP_BASEDATA="$___X_CMD_LS_APP_BASEDATA" \
    ___X_CMD_TUI_NAVI_POSITION="$position" \
    ___X_CMD_TUI_NAVI_POSITION_ISFUZZY="$position_isfuzzy" \
    ___x_cmd tui bigapp --clear-on-exit \
        -f "$___X_CMD_ROOT_MOD/tui/lib/eg.comp/navi.comp.awk"  \
        -f "$___X_CMD_ROOT_MOD/ls/lib/app/lsapp.awk"
    ___X_CMD_LS_APP_TMP_EXITCODE="$?"

    unset -f ___x_cmd_tui_app_processer
    ___x_cmd tui position store lsapp "$___X_CMD_TUI_CURRENT_NAVI_POSITION"

    return "$___X_CMD_LS_APP_TMP_EXITCODE"
}

___x_cmd_ls_app_processer(){
    local item=; local dirpath=;
    local command_log=; local content=
    case "$1" in
        data:request:local:*)
            item="${1#"data:request:local:"}"
            dirpath="${item#*"$___X_CMD_UNSEENCHAR_NEWLINE"}"
            item="${item%%"$___X_CMD_UNSEENCHAR_NEWLINE"*}"
            printf "data:item:local:%s\n" "$item"
            command_log="$( ___x_cmd_cmds_cd "$dirpath" 2>&1 )" || command_log="${command_log#*"cd:"}"
            (
                if ___x_cmd_cmds_cd "$dirpath" 2>/dev/null; then
                    content="$( LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_cmds_ls -lAhL 2>/dev/null )" || {
                        command_log="$( exec 3>&1; { LC_ALL="$___X_CMD_LOCALE_DEF_C" ___x_cmd_cmds_ls -lAhL 2>&3 1>/dev/null; }; exec 3>&-; )"
                        if [ -z "$command_log" ]; then  command_log="Fail to list $dirpath"
                        else                            command_log="${command_log#*"ls:"}"
                        fi
                    }
                fi
                printf "data:log:%s\n" "$command_log"
                printf "%s\n" "$content"
            )
            ___x_cmd_ls_app_processer___fix_gitbash "$dirpath"
            ;;
        x:open:*)   ___x_cmd open      "${1#x:open:}"   ;;
        x:browse:*) ___x_cmd browse    "${1#x:browse:}" ;;
    esac
}

___x_cmd_ls_app_processer___fix_gitbash(){
    local dir="$1"
    { [ "$dir" = "/" ] && ___x_cmd os is win ; } || return 0
    local fp
    [ "$PWD" = "/" ] || {
        fp="${PWD%%/"${PWD#/*/}"*}"
        [ ! -d "$fp" ] || ___x_cmd_cmds_ls -lAhLd "$fp" 2>/dev/null
    }

    fp="${HOME%%/"${HOME#/*/}"*}"
    [ ! -d "$fp" ] || ___x_cmd_cmds_ls -lAhLd "$fp" 2>/dev/null
}
