# shellcheck shell=sh
# shellcheck disable=SC2039,3043,2154

# Section: Fork
# https://gitee.com/api/v5/swagger#/postV5ReposOwnerRepoForks
___x_cmd_gt_repo_fork(){
    param:scope     ___x_cmd_gt
    param:dsl       '
options:
    #1                  "<owner_path>/<repo_path>"                                  <>:RepoPath
    -p|--path           "path for the fork repository"                              <>=""
    --name              "A new name for the fork repository"                        <>=""
    --org               "organization path. (default fork to current user path)"    <>=""
    --json|-j           "output json data"
'
    param:run
    local gen_gt_json=""
    gen_gt_json="$(param:option2json  -repo -p -org "${p:+"path=p"}" "${org:+"organization=org"}" )"
    gt:debug "$gen_gt_json"

    local repo="$1"
    ___x_cmd_gt_param_init_owner_repo
    ___x_cmd_gt_curl post "/repos/${owner_repo}/forks" "gen_gt_json" | ___x_cmd_gt_repo_fork____ui_handler
}

___x_cmd_gt_repo_fork____ui_handler(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_gt_http_error
        return
    fi
    (
        local _name=""
        x jo env . _name=.full_name html_url=.html_url gt_resp_err=.message gt_resp_err=.error
        if [ -n "$_name" ]; then
            ___x_cmd_ui_tf  true "Forking $owner_repo successfully, You may have to wait a short period of time before you can access the git objects:" \
                ${_name:+"Path: $_name"}  ${html_url:+"URL: $html_url"}
        else
            ___x_cmd_ui_tf false "Forking $owner_repo failure:" >&2
            ___x_cmd_gt____handle_resp
        fi
    )
}

# EndSection

# Section: Clone
___x_cmd_gt_repo_cl()(
    param:void
    if [ "$1" = "-h" ] || [ "$1" = "--help" ]; then _____x_cmd_gt_help repo clone; return;
    fi
    # shellcheck disable=SC2317
    ___x_cmd_git_clone___defaultowner(){
        local x_=""; ___x_cmd_gt_cur get_ owner 2>/dev/null
        if [ -z "$x_" ]; then
            gt:warn "There is no default owner found."
            gt:error "Please specify an owner for repo ${repo}"
            return 1
        fi
        printf "%s\n" "$x_"
    }

    ___X_CMD_GIT_CLONE_HOST=gitee.com x git clone "$@"
)
# EndSection
