# shellcheck shell=dash

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

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

___x_cmd_gl_codeurl_(){
    x_=""

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

    case "$url" in
        https://gitlab.com/*/*/-/blob/*)
            owner_repo="${url#'https://gitlab.com/'}"
            owner_repo="${owner_repo%%'/-/blob/'*}"
            local blob_after="${url#*'/-/blob/'}"
            x_="https://gitlab.com/${owner_repo}/-/raw/${blob_after}"
            ;;
        # wiki not found raw url
        https://gitlab.com/*/*)
            owner_repo="${url#'https://gitlab.com/'}"
            owner_repo="${owner_repo%/}"
            local default_branch="main"
            default_branch="$(___x_cmd gl repo info --json "${owner_repo}" | x jq -r .default_branch)"
            x_="https://gitlab.com/${owner_repo}/-/raw/${default_branch}/README.md"
            ;;
    esac
}
