# shellcheck shell=dash

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

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

___x_cmd_telegram_cur(){
    param:void
    local X_help_cmd=; X_help_cmd='x help -m telegram --cur' help:arg:parse
    ___x_cmd_telegram_cfg___invoke --current "$@"
}

___x_cmd_telegram_init(){
    param:void

    local cur_token=; local cur_chat=; local cur_proxy=;
    if [ -f "$(___x_cmd_telegram_cur --get config)" ]; then
        ___x_cmd_telegram_cur cur_token:=token cur_chat:=chat cur_proxy:=proxy 2>/dev/null
    fi

    local X_help_cmd=; X_help_cmd='x help -m telegram --init' help:arg:parse
    ___x_cmd_telegram_cfg___invoke --init \
        token   "Set up token of the bot"                               "${cur_token}"  "=~*"     ".*"       -- \
        chat    "Set up default chat id"                                "${cur_chat}"   "=~*"     ".*"       -- \
        proxy   "Set up the network proxy for API requests (optional)"  "${cur_proxy}"  "=~"    "^([a-z0-9]+://)?[0-9.:]+$"
}

___X_CMD_TELEGRAM_CFG_VARLIST="token,chat,proxy"
___x_cmd_telegram_cfg___invoke(){
    ___x_cmd_cfgy_obj \
        --prefix            ___X_CMD_telegram_CFG_DATA                  \
        --default-config    "${___X_CMD_ROOT_CFG}/telegram/X.cfg.yml"   \
        --current-config    "${___X_CMD_TELEGRAM_LOCAL_CONFIG}"         \
        --current-profile   "${___X_CMD_TELEGRAM_LOCAL_PROFILE}"        \
        --varlist           "$___X_CMD_TELEGRAM_CFG_VARLIST"            \
        "$@"
}