// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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{}
)

// PassportElementsWithErrors represents TL type `passportElementsWithErrors#438d1abf`.
type PassportElementsWithErrors struct {
	// Telegram Passport elements
	Elements []PassportElementClass
	// Errors in the elements that are already available
	Errors []PassportElementError
}

// PassportElementsWithErrorsTypeID is TL type id of PassportElementsWithErrors.
const PassportElementsWithErrorsTypeID = 0x438d1abf

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

func (p *PassportElementsWithErrors) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Elements == nil) {
		return false
	}
	if !(p.Errors == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*PassportElementsWithErrors) TypeName() string {
	return "passportElementsWithErrors"
}

// TypeInfo returns info about TL type.
func (p *PassportElementsWithErrors) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "passportElementsWithErrors",
		ID:   PassportElementsWithErrorsTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Elements",
			SchemaName: "elements",
		},
		{
			Name:       "Errors",
			SchemaName: "errors",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *PassportElementsWithErrors) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passportElementsWithErrors#438d1abf as nil")
	}
	b.PutID(PassportElementsWithErrorsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *PassportElementsWithErrors) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode passportElementsWithErrors#438d1abf as nil")
	}
	b.PutInt(len(p.Elements))
	for idx, v := range p.Elements {
		if v == nil {
			return fmt.Errorf("unable to encode passportElementsWithErrors#438d1abf: field elements element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare passportElementsWithErrors#438d1abf: field elements element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(p.Errors))
	for idx, v := range p.Errors {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare passportElementsWithErrors#438d1abf: field errors element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *PassportElementsWithErrors) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode passportElementsWithErrors#438d1abf to nil")
	}
	if err := b.ConsumeID(PassportElementsWithErrorsTypeID); err != nil {
		return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *PassportElementsWithErrors) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode passportElementsWithErrors#438d1abf to nil")
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field elements: %w", err)
		}

		if headerLen > 0 {
			p.Elements = make([]PassportElementClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodePassportElement(b)
			if err != nil {
				return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field elements: %w", err)
			}
			p.Elements = append(p.Elements, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field errors: %w", err)
		}

		if headerLen > 0 {
			p.Errors = make([]PassportElementError, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value PassportElementError
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare passportElementsWithErrors#438d1abf: field errors: %w", err)
			}
			p.Errors = append(p.Errors, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *PassportElementsWithErrors) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode passportElementsWithErrors#438d1abf as nil")
	}
	b.ObjStart()
	b.PutID("passportElementsWithErrors")
	b.Comma()
	b.FieldStart("elements")
	b.ArrStart()
	for idx, v := range p.Elements {
		if v == nil {
			return fmt.Errorf("unable to encode passportElementsWithErrors#438d1abf: field elements element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode passportElementsWithErrors#438d1abf: field elements element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("errors")
	b.ArrStart()
	for idx, v := range p.Errors {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode passportElementsWithErrors#438d1abf: field errors element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *PassportElementsWithErrors) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode passportElementsWithErrors#438d1abf to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("passportElementsWithErrors"); err != nil {
				return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: %w", err)
			}
		case "elements":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONPassportElement(b)
				if err != nil {
					return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field elements: %w", err)
				}
				p.Elements = append(p.Elements, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field elements: %w", err)
			}
		case "errors":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value PassportElementError
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field errors: %w", err)
				}
				p.Errors = append(p.Errors, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode passportElementsWithErrors#438d1abf: field errors: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetElements returns value of Elements field.
func (p *PassportElementsWithErrors) GetElements() (value []PassportElementClass) {
	if p == nil {
		return
	}
	return p.Elements
}

// GetErrors returns value of Errors field.
func (p *PassportElementsWithErrors) GetErrors() (value []PassportElementError) {
	if p == nil {
		return
	}
	return p.Errors
}
