# shellcheck shell=bash disable=SC2034,SC2154
# shellcheck disable=SC2039,3043

xrc:mod:lib     fjo     repo/branch/ls          \
                        repo/branch/ll          \
                        repo/branch/info        \
                        repo/branch/create      \
                        repo/branch/protection/_index

___x_cmd_fjo_repo_branch(){
    param:subcmd ___x_cmd_fjo_repo_branch                              \
        ls                  "Interactive UI show repo branch"              \
        ll                  "List repo branch"                             \
        "info"              "get repo branch info"                         \
        create              "create the repo branch"                       \
        default             "set default branch"                           \
        protection          "protection branch management"
    param:subcmd:try
    param:run


    ___x_cmd_fjo_repo_branch _param_help_doc
    return 1
}

# BeginSection: default

# https://gitea.com/api/swagger#/repository/repoCreateBranch
___x_cmd_fjo_repo_branch_default(){
    param:dsl       '
options:
    #1          "branch name"                                                       <>
    --repo|-r   "<owner_path>/<repo_path>"                                          <>:RepoName
    --json|-j   "output json data"
'
    param:run
    # TODO:source data
    ___x_cmd_fjo_repo_edit -j --default_branch "$1" "$repo" | ___x_cmd_fjo_branch_____ui_handler Default
}



___x_cmd_fjo_branch_____ui_handler(){
    if [ -n "$ENFORCE_JSON" ] || [ -n "$json" ]; then
        ___x_cmd_cmds_cat
        ___x_cmd_fjo_http_error
        return
    fi
    (
        local data
        local msg
        local error_msg
        case "$1" in
            Info)
                ___x_cmd jo env . name=.name fjo_resp_err=.message \
                    Protected=.protected sha=.commit.id url=.commit.url
                msg="get repo ${owner_repo} branch info success"
                error_msg="get repo ${owner_repo} branch info fail"
                ;;
            Creating)
                ___x_cmd jo env . name=.name fjo_resp_err=.message \
                    Protected=.protected sha=.commit.id url=.commit.url
                msg="Creating $branch successfully"
                error_msg="Creating $branch failed"
                ;;
            Default)
                ___x_cmd jo env . name=.name fjo_resp_err=.message
                msg="Successfully set repo default branch"
                error_msg="Failed to set repo default branch"
                ;;
        esac

        if [ -n "$name" ]; then
            ___x_cmd_ui_tf true "$msg" ${name+"Name: $name"} ${Protected+"Protected: $Protected"}  ${sha+"LastCommit: $sha"} ${url+"Url: $url"}
        else
            ___x_cmd_ui_tf false "$error_msg"
            ___x_cmd_fjo____handle_resp
            return 1
        fi
    )
}

