# shellcheck shell=dash

xrc cfgy
___x_cmd_feishu_cfg(){
    param:void
    [ "$#" -gt 0 ] || {
        _____x_cmd_feishu_help --cfg
        return 64
    }

    local X_help_cmd=; X_help_cmd='x help -m feishu --cfg' help:arg:parse
    ___x_cmd_feishu_cfg___invoke "$@"
}

___x_cmd_feishu_cur(){
    param:void
    local X_help_cmd=; X_help_cmd='x help -m feishu --cur' help:arg:parse
    ___x_cmd_feishu_cfg___invoke --current "$@"
}

___x_cmd_feishu_init_chatid(){
    local cur_chatid=;
    if [ -f "$(___x_cmd_feishu_cur --get config)" ]; then
        ___x_cmd_feishu_cur cur_chatid:=chatid 2>/dev/null
    fi

    feishu:info "Get Chat ID: Click the group menu in the top right -> Group Settings -> View Group ID at the bottom of the page"
    local X_help_cmd=; X_help_cmd='x help -m feishu --init' help:arg:parse
    ___x_cmd_feishu_cfg___invoke --init \
        chatid      "Chat ID for private bot conversation"                     "$cur_chatid"       "=~*"   ".*"
}


___x_cmd_feishu_init(){
    param:void

    local cur_webhook=; local cur_app_id=; local cur_app_secret=; local cur_chatid=;
    if [ -f "$(___x_cmd_feishu_cur --get config)" ]; then
        ___x_cmd_feishu_cur cur_webhook:=webhook cur_app_id:=app_id cur_app_secret:=app_secret cur_chatid:=chatid 2>/dev/null
    fi

    local X_help_cmd=; X_help_cmd='x help -m feishu --init' help:arg:parse
    ___x_cmd_feishu_cfg___invoke --init \
        app_id      "App ID for Feishu bot"                                    "$cur_app_id"       "=~*"   ".*"    --                                              \
        app_secret  "App Secret for Feishu bot"                                "$cur_app_secret"   "=~*"   ".*"    --                                              \
        chatid      "Chat ID for private bot conversation"                     "$cur_chatid"       "=~*"   ".*"    --                                              \
        webhook     "Webhook URL for custom robot to send messages (bot mode)" "$cur_webhook"      "=~*"   "^https://open.feishu.cn/open-apis/bot/v2/hook/.*"
}

___X_CMD_FEISHU_CFG_VARLIST="webhook,app_id,app_secret,chatid"
___x_cmd_feishu_cfg___invoke(){
    ___x_cmd_cfgy_obj \
        --prefix            ___X_CMD_feishu_CFG_DATA                  \
        --default-config    "${___X_CMD_ROOT_CFG}/feishu/X.cfg.yml"   \
        --current-config    "${___X_CMD_FEISHU_LOCAL_CONFIG}"         \
        --current-profile   "${___X_CMD_FEISHU_LOCAL_PROFILE}"        \
        --varlist           "$___X_CMD_FEISHU_CFG_VARLIST"            \
        "$@"
}