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

// BusinessChatLinks represents TL type `businessChatLinks#1569924`.
type BusinessChatLinks struct {
	// List of links
	Links []BusinessChatLink
}

// BusinessChatLinksTypeID is TL type id of BusinessChatLinks.
const BusinessChatLinksTypeID = 0x1569924

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

func (b *BusinessChatLinks) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Links == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BusinessChatLinks) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessChatLinks",
		ID:   BusinessChatLinksTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Links",
			SchemaName: "links",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessChatLinks) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessChatLinks#1569924 as nil")
	}
	buf.PutID(BusinessChatLinksTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BusinessChatLinks) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessChatLinks#1569924 as nil")
	}
	buf.PutInt(len(b.Links))
	for idx, v := range b.Links {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare businessChatLinks#1569924: field links element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BusinessChatLinks) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessChatLinks#1569924 to nil")
	}
	if err := buf.ConsumeID(BusinessChatLinksTypeID); err != nil {
		return fmt.Errorf("unable to decode businessChatLinks#1569924: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BusinessChatLinks) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode businessChatLinks#1569924 to nil")
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode businessChatLinks#1569924: field links: %w", err)
		}

		if headerLen > 0 {
			b.Links = make([]BusinessChatLink, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BusinessChatLink
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare businessChatLinks#1569924: field links: %w", err)
			}
			b.Links = append(b.Links, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessChatLinks) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessChatLinks#1569924 as nil")
	}
	buf.ObjStart()
	buf.PutID("businessChatLinks")
	buf.Comma()
	buf.FieldStart("links")
	buf.ArrStart()
	for idx, v := range b.Links {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode businessChatLinks#1569924: field links element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BusinessChatLinks) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessChatLinks#1569924 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessChatLinks"); err != nil {
				return fmt.Errorf("unable to decode businessChatLinks#1569924: %w", err)
			}
		case "links":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value BusinessChatLink
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode businessChatLinks#1569924: field links: %w", err)
				}
				b.Links = append(b.Links, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode businessChatLinks#1569924: field links: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetLinks returns value of Links field.
func (b *BusinessChatLinks) GetLinks() (value []BusinessChatLink) {
	if b == nil {
		return
	}
	return b.Links
}
