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

___x_cmd log init gh
xrc api arg param ui
xrc:mod gx/lib/share

xrc:mod:lib gh              \
    util cfg avatar       repo/index            org/index                                           \
    user/index            user/repo             user/ssh                 user/gpg                   \
    package/index         package/tag           team/index               team/repo                  \
    action/index          action/workflow       action/run               action/artifact            \
    secret/index          issue/index           _type/index              awk/app/table              \
    notification/index    search                model/_index             readme                     \
    codeurl

xrc:mod:lib gh ddgo

if ___x_cmd_is_suitable_advise_env; then
    xrc:mod:lib gh advise
fi

___x_cmd_gh___main(){
    local ___X_CMD_GH_LOCAL_CONFIG="${___X_CMD_GH_LOCAL_CONFIG}"
    local ___X_CMD_GH_LOCAL_PROFILE="${___X_CMD_GH_LOCAL_PROFILE}"
    while [ $# -gt 0 ]; do
        case "${1}" in
            :)              break;; # gh ddgo
            ::)             break;; # gh ddgoai
            :*:*)
                            ___X_CMD_GH_LOCAL_CONFIG="${1#:}"
                            ___X_CMD_GH_LOCAL_CONFIG="${___X_CMD_GH_LOCAL_CONFIG%%:*}"
                            ___X_CMD_GH_LOCAL_PROFILE="${1#:"$___X_CMD_GH_LOCAL_CONFIG":}"
                            shift
                            ;;
            :*)             ___X_CMD_GH_LOCAL_PROFILE="${1#:}";   shift   ;;
            --config)       ___X_CMD_GH_LOCAL_CONFIG="$2";        shift 2 ;;
            --profile)      ___X_CMD_GH_LOCAL_PROFILE="$2";       shift 2 ;;
            *=*)            ___x_cmd_gh_cur "$@"; return ;;
            *)              break;;
        esac
    done

    arg:init gh
    param:advise gh

    # "model|m"           "llm model management"                                          \

    param:subcmd ___x_cmd_gh                                                            \
        repo                "repository management"                                     \
        action              "github action management"                                  \
        package             "github packages management"                                \
        issue               "issue management"                                          \
        user                "user management"                                           \
        org                 "organization management"                                   \
        team                "team management"                                           \
        search              "search repository and topics"                              \
        secret              "github secret management"                                  \
        avatar              "manage user addressbook for gh"                            \
        "notification|n"    "notification management"                                   \
        readme              "Download readme"                                           \
        codeurl             "Get GitHub source url"                                     \
        "browse|b"          "Open GitHub repository in the web browser"                 \
        "ddgo|:"            "List about GitHub content using DuckDuckGo"                \
        "ddgoai|::"         "List about GitHub content using DuckDuckGo and AI module"  \
        --cfg               "manage configuration for gh"                               \
        --cur               "current CLI owner, repo"                                   \
        "init"              "Interactive UI init configuration"                         \
        "model"             "github model management"
    param:subcmd:try

    # Notice: Maybe deleted.
    param:subcmd ___x_cmd_gh_action                     \
        "wf"        "shortcut for 'x gh action wf'"     \
        "run"       "shortcut for 'x gh action run'"    \
        "art"       "shortcut for 'x gh action art'"
    param:subcmd:try

    param:subcmd ___x_cmd_gh_repo                       \
        pr          "pull request management"           \
        "cl|clone"  "short cut for 'x gh repo clone'"
    param:subcmd:try
    param:run

    ___x_cmd_gh _param_help_doc
    return 1
}

___x_cmd_gh_cur(){
    param:void
    case "$1" in
        -h|--help)      _____x_cmd_gh_help --cur     ;;
        *)              ___x_cmd_gh_cfg current "$@" ;;
    esac
}


___x_cmd_gh____init(){
    ___X_CMD_GH_EDITOR="${___X_CMD_GH_EDITOR:-"command less -r"}"

    # ___X_CMD_GH_CFG="${___X_CMD_GH_CFG:-"${___X_CMD_ROOT_CFG}/app.github.config"}"
    ___X_CMD_GH_TMP="${___X_CMD_GH_TMP:-"${___X_CMD_ROOT_TMP}/gh"}"
    ___X_CMD_GH_CACHE="${___X_CMD_GH_CACHE:-"${___X_CMD_ROOT_CACHE}/gh"}"
    ___X_CMD_GH_ART="${___X_CMD_GH_ART:-"${___X_CMD_ROOT_ART}/gh"}"

    ___x_cmd mkdirp "$___X_CMD_GH_TMP"
}

___x_cmd_gh____init


___x_cmd_gh_latency(){
    ___x_cmd curl -o /dev/null -s -w "%{time_total}" https://api.github.com/rate_limit
}
