# shellcheck shell=dash
___x_cmd_winget_mirror(){
    [ $# -gt 0 ]    ||  set - --help
    local op="$1";      shift
    case "$op" in
        set)        ___x_cmd_winget_mirror___set     "$@" ;;
        unset)      ___x_cmd_winget_mirror___unset   "$@" ;;
        ls)         ___x_cmd_winget_mirror___ls      "$@" ;;
        current)    ___x_cmd_winget_mirror___current "$@" ;;
        -h|--help)
                ___x_cmd help -m winget mirror ;;
        *)      ___x_cmd help -m winget mirror >&2; return 64 ;;
    esac
}

___x_cmd_winget_mirror___set(){
    [ $# -gt 0 ]    ||  set -- ustc
    local name="$1"
    case "$name" in
        ustc)
            # https://mirrors.ustc.edu.cn/help/winget-source.html
            winget:info --Thanks "https://mirrors.ustc.edu.cn" \
                        --cmd "winget source remove ustc; winget source add ustc https://mirrors.ustc.edu.cn/winget-source" \
                        "Set the USTC mirror source, which will replace the \"ustc\" source"
            ___x_cmd pwsh admin "winget source remove ustc; winget source add ustc https://mirrors.ustc.edu.cn/winget-source"
            ;;
        msstore)
            winget:info --cmd "winget source remove msstore; winget source add msstore https://storeedgefd.dsx.mp.microsoft.com/v9.0" \
                        "Set the official Microsoft software source for winget, which will replace the \"msstore\" source"
            ___x_cmd pwsh admin "winget source remove msstore; winget source add msstore https://storeedgefd.dsx.mp.microsoft.com/v9.0"
            ;;
        -h|--help)
            ___x_cmd help -m winget mirror set ;;
        *)  ___x_cmd help -m winget mirror set >&2; return 64
            ;;
    esac
}

___x_cmd_winget_mirror___unset(){
    [ $# -gt 0 ]    ||  set -- all
    local name="$1"
    case "$name" in
        ustc)
            winget:info --cmd "winget source reset ustc" \
                        "Unset the mirror \"ustc\" source, it will remove the \"ustc\" source"
            ___x_cmd pwsh admin "winget source reset ustc"
            ;;
        msstore)
            winget:info --cmd "winget source reset ustc" \
                        "Unset the \"msstore\" source, it will remove the \"msstore\" source"
            ___x_cmd pwsh admin "winget source reset msstore"
            ;;
        all)
            winget:info --cmd "winget source reset --force" \
                        "Unset the all mirror source"
            ___x_cmd pwsh admin "winget source reset --force"
            ;;
        -h|--help)
            ___x_cmd help -m winget mirror unset ;;
        *)  ___x_cmd help -m winget mirror unset >&2; return 64
            ;;
    esac
}

___x_cmd_winget_mirror___ls(){
    printf "%s\n" \
        "Code       Url                                         Name"                    \
        "ustc       https://mirrors.ustc.edu.cn/winget-source   USTC Mirror WinGet Software Source"
}

___x_cmd_winget_mirror___current(){
    ___x_cmd_winget___bin source list
}
