// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// HelpCountriesListNotModified represents TL type `help.countriesListNotModified#93cc1f32`.
// The country list has not changed
//
// See https://core.telegram.org/constructor/help.countriesListNotModified for reference.
type HelpCountriesListNotModified struct {
}

// HelpCountriesListNotModifiedTypeID is TL type id of HelpCountriesListNotModified.
const HelpCountriesListNotModifiedTypeID = 0x93cc1f32

// construct implements constructor of HelpCountriesListClass.
func (c HelpCountriesListNotModified) construct() HelpCountriesListClass { return &c }

// Ensuring interfaces in compile-time for HelpCountriesListNotModified.
var (
	_ bin.Encoder     = &HelpCountriesListNotModified{}
	_ bin.Decoder     = &HelpCountriesListNotModified{}
	_ bin.BareEncoder = &HelpCountriesListNotModified{}
	_ bin.BareDecoder = &HelpCountriesListNotModified{}

	_ HelpCountriesListClass = &HelpCountriesListNotModified{}
)

func (c *HelpCountriesListNotModified) Zero() bool {
	if c == nil {
		return true
	}

	return true
}

// String implements fmt.Stringer.
func (c *HelpCountriesListNotModified) String() string {
	if c == nil {
		return "HelpCountriesListNotModified(nil)"
	}
	type Alias HelpCountriesListNotModified
	return fmt.Sprintf("HelpCountriesListNotModified%+v", Alias(*c))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*HelpCountriesListNotModified) TypeID() uint32 {
	return HelpCountriesListNotModifiedTypeID
}

// TypeName returns name of type in TL schema.
func (*HelpCountriesListNotModified) TypeName() string {
	return "help.countriesListNotModified"
}

// TypeInfo returns info about TL type.
func (c *HelpCountriesListNotModified) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.countriesListNotModified",
		ID:   HelpCountriesListNotModifiedTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (c *HelpCountriesListNotModified) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode help.countriesListNotModified#93cc1f32 as nil")
	}
	b.PutID(HelpCountriesListNotModifiedTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *HelpCountriesListNotModified) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode help.countriesListNotModified#93cc1f32 as nil")
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *HelpCountriesListNotModified) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode help.countriesListNotModified#93cc1f32 to nil")
	}
	if err := b.ConsumeID(HelpCountriesListNotModifiedTypeID); err != nil {
		return fmt.Errorf("unable to decode help.countriesListNotModified#93cc1f32: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *HelpCountriesListNotModified) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode help.countriesListNotModified#93cc1f32 to nil")
	}
	return nil
}

// HelpCountriesList represents TL type `help.countriesList#87d0759e`.
// Name, ISO code, localized name and phone codes/patterns of all available countries
//
// See https://core.telegram.org/constructor/help.countriesList for reference.
type HelpCountriesList struct {
	// Name, ISO code, localized name and phone codes/patterns of all available countries
	Countries []HelpCountry
	// Hash used for caching, for more info click here¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets#hash-generation
	Hash int
}

// HelpCountriesListTypeID is TL type id of HelpCountriesList.
const HelpCountriesListTypeID = 0x87d0759e

// construct implements constructor of HelpCountriesListClass.
func (c HelpCountriesList) construct() HelpCountriesListClass { return &c }

// Ensuring interfaces in compile-time for HelpCountriesList.
var (
	_ bin.Encoder     = &HelpCountriesList{}
	_ bin.Decoder     = &HelpCountriesList{}
	_ bin.BareEncoder = &HelpCountriesList{}
	_ bin.BareDecoder = &HelpCountriesList{}

	_ HelpCountriesListClass = &HelpCountriesList{}
)

func (c *HelpCountriesList) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Countries == nil) {
		return false
	}
	if !(c.Hash == 0) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (c *HelpCountriesList) String() string {
	if c == nil {
		return "HelpCountriesList(nil)"
	}
	type Alias HelpCountriesList
	return fmt.Sprintf("HelpCountriesList%+v", Alias(*c))
}

// FillFrom fills HelpCountriesList from given interface.
func (c *HelpCountriesList) FillFrom(from interface {
	GetCountries() (value []HelpCountry)
	GetHash() (value int)
}) {
	c.Countries = from.GetCountries()
	c.Hash = from.GetHash()
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*HelpCountriesList) TypeID() uint32 {
	return HelpCountriesListTypeID
}

// TypeName returns name of type in TL schema.
func (*HelpCountriesList) TypeName() string {
	return "help.countriesList"
}

// TypeInfo returns info about TL type.
func (c *HelpCountriesList) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "help.countriesList",
		ID:   HelpCountriesListTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Countries",
			SchemaName: "countries",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *HelpCountriesList) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode help.countriesList#87d0759e as nil")
	}
	b.PutID(HelpCountriesListTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *HelpCountriesList) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode help.countriesList#87d0759e as nil")
	}
	b.PutVectorHeader(len(c.Countries))
	for idx, v := range c.Countries {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode help.countriesList#87d0759e: field countries element with index %d: %w", idx, err)
		}
	}
	b.PutInt(c.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (c *HelpCountriesList) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode help.countriesList#87d0759e to nil")
	}
	if err := b.ConsumeID(HelpCountriesListTypeID); err != nil {
		return fmt.Errorf("unable to decode help.countriesList#87d0759e: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *HelpCountriesList) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode help.countriesList#87d0759e to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode help.countriesList#87d0759e: field countries: %w", err)
		}

		if headerLen > 0 {
			c.Countries = make([]HelpCountry, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value HelpCountry
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode help.countriesList#87d0759e: field countries: %w", err)
			}
			c.Countries = append(c.Countries, value)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode help.countriesList#87d0759e: field hash: %w", err)
		}
		c.Hash = value
	}
	return nil
}

// GetCountries returns value of Countries field.
func (c *HelpCountriesList) GetCountries() (value []HelpCountry) {
	if c == nil {
		return
	}
	return c.Countries
}

// GetHash returns value of Hash field.
func (c *HelpCountriesList) GetHash() (value int) {
	if c == nil {
		return
	}
	return c.Hash
}

// HelpCountriesListClassName is schema name of HelpCountriesListClass.
const HelpCountriesListClassName = "help.CountriesList"

// HelpCountriesListClass represents help.CountriesList generic type.
//
// See https://core.telegram.org/type/help.CountriesList for reference.
//
// Example:
//
//	g, err := tg.DecodeHelpCountriesList(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tg.HelpCountriesListNotModified: // help.countriesListNotModified#93cc1f32
//	case *tg.HelpCountriesList: // help.countriesList#87d0759e
//	default: panic(v)
//	}
type HelpCountriesListClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() HelpCountriesListClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	// AsModified tries to map HelpCountriesListClass to HelpCountriesList.
	AsModified() (*HelpCountriesList, bool)
}

// AsModified tries to map HelpCountriesListNotModified to HelpCountriesList.
func (c *HelpCountriesListNotModified) AsModified() (*HelpCountriesList, bool) {
	return nil, false
}

// AsModified tries to map HelpCountriesList to HelpCountriesList.
func (c *HelpCountriesList) AsModified() (*HelpCountriesList, bool) {
	return c, true
}

// DecodeHelpCountriesList implements binary de-serialization for HelpCountriesListClass.
func DecodeHelpCountriesList(buf *bin.Buffer) (HelpCountriesListClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case HelpCountriesListNotModifiedTypeID:
		// Decoding help.countriesListNotModified#93cc1f32.
		v := HelpCountriesListNotModified{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpCountriesListClass: %w", err)
		}
		return &v, nil
	case HelpCountriesListTypeID:
		// Decoding help.countriesList#87d0759e.
		v := HelpCountriesList{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode HelpCountriesListClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode HelpCountriesListClass: %w", bin.NewUnexpectedID(id))
	}
}

// HelpCountriesList boxes the HelpCountriesListClass providing a helper.
type HelpCountriesListBox struct {
	CountriesList HelpCountriesListClass
}

// Decode implements bin.Decoder for HelpCountriesListBox.
func (b *HelpCountriesListBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode HelpCountriesListBox to nil")
	}
	v, err := DecodeHelpCountriesList(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.CountriesList = v
	return nil
}

// Encode implements bin.Encode for HelpCountriesListBox.
func (b *HelpCountriesListBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.CountriesList == nil {
		return fmt.Errorf("unable to encode HelpCountriesListClass as nil")
	}
	return b.CountriesList.Encode(buf)
}
