# shellcheck shell=dash

___x_cmd_gh_codeurl(){
    param:void
    case "$1" in
        -h|--help)          ___x_cmd help -m gh codeurl   "$@"; return ;;
    esac

    local x_=""
    ___x_cmd_gh_codeurl_ "$@"
    [ -n "$x_" ] || return 1
    printf "%s\n" "$x_"
}

___x_cmd_gh_codeurl_(){
    x_=""

    local url="$1"
    local owner_repo=""

    case "$url" in
        https://github.com/*/*/blob/*)
            owner_repo="${url#https://github.com/}"
            owner_repo="${owner_repo%%/blob/*}"
            local blob_after="${url#*/blob/}"
            x_="https://raw.githubusercontent.com/${owner_repo}/${blob_after}"
            ;;
        https://github.com/*/*/wiki|https://github.com/*/*/wiki/*)
            owner_repo="${url#https://github.com/}"
            owner_repo="${owner_repo%%/wiki*}"
            local file="${url#*'/wiki'}"
            [ -n "$file" ] || file="/Home"
            x_="https://raw.githubusercontent.com/wiki/${owner_repo}${file}.md"
            ;;
        https://github.com/*/*/*)
            x_=""
            ;;
        https://github.com/*/*)
            owner_repo="${url#https://github.com/}"
            owner_repo="${owner_repo%/}"
            local x_default_branch=""
            if ! ___x_cmd_gh___get_x_default_branch_; then
                gh:error "Can not get default branch of repo $owner_repo. Please have check it."
                return 1
            fi
            x_="https://raw.githubusercontent.com/${owner_repo}/refs/heads/${x_default_branch}/README.md"
            ;;
    esac
}
