# shellcheck shell=dash
xrc:mod:lib     gx      ws/browse ws/collaborator ws/edit ws/info ws/issue ws/pr ws/pull_push

___x_cmd_gx_ws(){
    local X_help_cmd='___x_cmd_gx_help ws'
    help:arg:parse
    [ $# -gt 0 ] ||     set -- info

    local op="$1";      shift
    case "$op" in
        collaborator)   ___x_cmd_gx_ws_collaborator "$@" ;;
        issue)          ___x_cmd_gx_ws_issue        "$@" ;;
        pr)             ___x_cmd_gx_ws_pr           "$@" ;;

        info)           ___x_cmd_gx_ws_info         "$@" ;;
        edit)           ___x_cmd_gx_ws_edit   "$op" "$@" ;;
        browse)         ___x_cmd_gx_ws_browse       "$@" ;;

        pull|push)     ___x_cmd_gx_ws_"${op}"       "$@" ;;
    esac
}

___x_cmd_gx_ws_gethttpurl_(){
    # example https://github.com/x-bash/xrc

    ___x_cmd_gx_ws_repopath_ || return
    local p="$x_"

    ___x_cmd_gx_ws_provider_ || return
    case "$x_" in
        gh)         x_="https://github.com"   ;;
        gt)         x_="https://gitee.com"    ;;
        gl)         x_="https://gitlab.com"   ;;
        gitcode)    x_="https://gitcode.com"  ;;
    esac
}

# github, gitcode, gitee, gitlab
___x_cmd_gx_ws_provider_(){
    # x_=$(x git meta url) || return
    x_=$(___x_cmd_gx_ws_url_) || return
    case "$x_" in
        github.com)     x_=gh       ;;
        gitee.com)      x_=gt       ;;
        gitcode.net)    x_=gitcode  ;;
        gitlab.com)     x_=gl       ;;
        *)              return 1
    esac
}

___x_cmd_gx_ws_repopath_(){
    local repopath
    repopath="$(x git meta path)"
    x_="${repopath%.*}"
}

___x_cmd_gx_ws_branch_origin_(){
    local branch
    branch="$(command git branch -r)"
    branch="${branch##*"-> "}"
    branch="${branch%%"
"*}"
    x_="$branch"
}

___x_cmd_gx_ws_branch_current_(){
    local branch
    branch="$(command git branch)"
    branch="${branch##*"* "}"
    branch="${branch%%"
"*}"
    x_="$branch"
}


___x_cmd_gx_ws_url_(){
    local url
    url="$(command git remote get-url origin)"
    url="${url#*@}"
    printf "%s" "${url%:*}"
}


