# shellcheck shell=dash disable=SC2142,2016

# winget source remove winget; winget source add winget https://mirrors.ustc.edu.cn/winget-source
# winget source reset winget
# https://learn.microsoft.com/en-us/windows/wsl/filesystems#share-environment-variables-between-windows-and-wsl-with-wslenv
___x_cmd_pwsh_admin(){

    local cmd="$*"; # [ -n "$cmd" ] || cmd="$(cat)"
    pwsh:debug -c "$cmd" admin
    WSLENV="${WSLENV}:admin_file/p" \
    admin_file="$___X_CMD_ROOT_MOD/pwsh/lib/ps1/util/admin.ps1" \
    ___x_cmd_pwsh_bin -ExecutionPolicy unrestricted -NoProfile -Command "& \$env:admin_file '$cmd';exit \$lastexitcode"
}

___x_cmd_pwsh___exec(){
    local mode="$1";    shift
    pwsh:debug -c "$*" --mode "$mode" exec
    ___x_cmd_pwsh___exec_to_result <<A
$@ | Out-File -Encoding UTF8 -FilePath "result"
A
}

___x_cmd_pwsh___exec_admin(){
    local mode="$1";    shift
    pwsh:debug -c "$*" --mode "$mode" admin
    ___x_cmd_pwsh_admin "$* | Out-File -Encoding UTF8 -FilePath \"result\""
}

___x_cmd_pwsh___exec_to_result(){(
    local TgtDir="$___X_CMD_ROOT_TMP/pwsh/$$"
    trap '___x_cmd rmrf "$TgtDir"' EXIT

    ___x_cmd mkdirp "$TgtDir"
    ___x_cmd_cmds cd "$TgtDir"
    ___x_cmd_pwsh_bin > /dev/null

    [ -f "result" ] || return 1
    ___x_cmd_cmds sed -i '1 s/^\xef\xbb\xbf//' result
    ___x_cmd_cmds cat result
)}


alias pwsh:arg:null='
[ "$#" -gt 0 ] || {
    set -- --app
    { ___x_cmd_is_stdout2tty && ___x_cmd_runmode_allow_manual; } || set -- --csv
}
'

alias pwsh:output:mode='
case "$1" in
    -l|--list)  x_output_mode=list; shift; continue ;;
    -j|--json)  x_output_mode=json; x_output="| ConvertTo-Json"; shift; continue ;;
    -c|--csv)   x_output_mode=csv;  x_output="| ConvertTo-Csv -NoTypeInformation"; shift; continue ;;
    --app)      x_output_mode=app;  x_output="| ConvertTo-Csv -NoTypeInformation"; shift; continue ;;
esac
'

alias pwsh:output:mode:all='
pwsh:output:mode
case "$1" in
    *)          N=pwsh M="Unsupported option => $1" log:ret:64  ;;
esac
'
___x_cmd_pwsh_slash2backslash(){
    local path="$1"
    local tmp=
    local str=
    while [ "$path" != "${path#*/}" ]; do
        tmp="${path%%/*}"
        str="$str\\$tmp"
        path="${path#*/}"
    done
    x_="${str}\\$path"
}

# /usr/bin => <mingws root path in windowstyle>\\usr\\bin

# /etc/os-release
# lsb_release -a
___x_cmd_pwsh___get_wsl_distro_(){
    ___x_cmd os is wsl || return $?

    if [ -n "$___X_CMD_PWSH_WSL_DISTRO" ]; then
        x_="$___X_CMD_PWSH_WSL_DISTRO"
    elif [ -n "$WSL_DISTRO_NAME" ]; then
        x_="$WSL_DISTRO_NAME"
        ___X_CMD_PWSH_WSL_DISTRO="$x_"
    else
        x_="$(
            ___x_cmd pwsh -ExecutionPolicy RemoteSigned -NoProfile -Command 'wsl -l -v' 2>/dev/null | \
                ___x_cmd_cmds awk '{
                    s = $0
                    gsub("[\000-\037]", "", s)
                    gsub("[\t ]+", " ", s)
                    if ((id = index( s, " Running" )) > 0 ) {
                        s = substr( s, 1, id-1 )
                        id  = index( s, " " )
                        s = substr( s, id+1 )
                        print s
                        exit(0)
                    }
                }'
        )"
        [ -n "$x_" ] || return $?
        ___X_CMD_PWSH_WSL_DISTRO="$x_"
    fi
}

