package telegram

import (
	

	
	
)

// ConfigError is returned by Client.Run when the server rejects the
// connection setup (initConnection) with an error that reconnecting cannot
// fix, e.g. API_ID_INVALID, API_ID_PUBLISHED_FLOOD or
// CONNECTION_API_ID_INVALID: the client is misconfigured.
//
// Use errors.As to match it.
type ConfigError struct {
	// Err is the RPC error returned to initConnection.
	Err *tgerr.Error
}

// Error implements error.
func ( *ConfigError) () string {
	return "connection setup rejected: " + .Err.Error()
}

// Unwrap returns the RPC error.
func ( *ConfigError) () error {
	return .Err
}

// asConfigError returns ConfigError if err is a permanent error of
// initConnection.
//
// These are 400 and 406 errors: the same request with the same parameters
// fails again. Official clients send initConnection as a prefix of the
// first real request, so such an error fails that request instead of
// reconnecting.
//
// CONNECTION_NOT_INITED and CONNECTION_LAYER_INVALID are excluded: TDLib and
// Telegram Desktop handle them by sending initConnection again. Errors of
// IsAuthLost (e.g. AUTH_KEY_DUPLICATED) keep their own handling.
func ( error) (*ConfigError, bool) {
	var  *manager.InitError
	if !errors.As(, &) {
		return nil, false
	}
	,  := tgerr.As(.Err)
	if ! {
		return nil, false
	}
	if .Code != 400 && .Code != 406 {
		return nil, false
	}
	if .IsOneOf("CONNECTION_NOT_INITED", "CONNECTION_LAYER_INVALID") || IsAuthLost() {
		return nil, false
	}
	return &ConfigError{Err: }, true
}