# shellcheck shell=sh
# shellcheck disable=SC3043,2034

xrc cfgy
___x_cmd_cf_cfg(){
    param:void
    [ "$#" -gt 0 ] || {
        ___x_cmd_cf_init
        return
    }

    local op="$1"; shift
    case "$op" in
        -h|--help)      ___x_cmd help -m cf --cfg ;;
        *)              ___x_cmd_cf_cfg___invoke "$op" "$@" ;;
    esac
}

___x_cmd_cf_cur(){
    param:void
    local X_help_cmd=; X_help_cmd='x help -m cf --cur' help:arg:parse
    ___x_cmd_cf_cfg___invoke --current "$@"
}


# x cf --cfg init <token> <accound_id>
___x_cmd_cf_init(){
    param:void
    local cur_token=; local cur_account_id=; local cur_email=;
    if [ -f "$(___x_cmd_cf_cur --get config)" ]; then
        ___x_cmd_cf_cur cur_token:=token cur_email:=email cur_account_id:=account_id
    fi

    local X_help_cmd=; X_help_cmd='x help -m cf --init' help:arg:parse
    ___x_cmd_cf_cfg___invoke --init \
        email        "Set up Cloudflare email"               "${cur_email}"          --     \
        token        "Set up Cloudflare token"               "${cur_token}"  '=~*'   --     \
        account_id   "Set up acccout id"                     "${cur_account_id}"     --    || return
}

___X_CMD_CF_CFG_VARLIST="token,email,account_id"
___x_cmd_cf_cfg___invoke(){
    ___x_cmd_cfgy_obj   \
        --prefix            ___X_CMD_CF_CFG_DATA                    \
        --default-config    "${___X_CMD_ROOT_CFG}/cf/X.cfg.yml"     \
        --current-config    "${___X_CMD_CF_LOCAL_CONFIG}"           \
        --current-profile   "${___X_CMD_CF_LOCAL_PROFILE}"          \
        --varlist           "${___X_CMD_CF_CFG_VARLIST}"            \
        "$@"
}
