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

// BusinessFeatures represents TL type `businessFeatures#e1593e48`.
type BusinessFeatures struct {
	// The list of available business features
	Features []BusinessFeatureClass
}

// BusinessFeaturesTypeID is TL type id of BusinessFeatures.
const BusinessFeaturesTypeID = 0xe1593e48

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BusinessFeatures) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "businessFeatures",
		ID:   BusinessFeaturesTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Features",
			SchemaName: "features",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BusinessFeatures) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode businessFeatures#e1593e48 as nil")
	}
	buf.PutID(BusinessFeaturesTypeID)
	return b.EncodeBare(buf)
}

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

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

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

		if headerLen > 0 {
			b.Features = make([]BusinessFeatureClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeBusinessFeature(buf)
			if err != nil {
				return fmt.Errorf("unable to decode businessFeatures#e1593e48: field features: %w", err)
			}
			b.Features = append(b.Features, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BusinessFeatures) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode businessFeatures#e1593e48 as nil")
	}
	buf.ObjStart()
	buf.PutID("businessFeatures")
	buf.Comma()
	buf.FieldStart("features")
	buf.ArrStart()
	for idx, v := range b.Features {
		if v == nil {
			return fmt.Errorf("unable to encode businessFeatures#e1593e48: field features element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode businessFeatures#e1593e48: field features 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 *BusinessFeatures) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode businessFeatures#e1593e48 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("businessFeatures"); err != nil {
				return fmt.Errorf("unable to decode businessFeatures#e1593e48: %w", err)
			}
		case "features":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				value, err := DecodeTDLibJSONBusinessFeature(buf)
				if err != nil {
					return fmt.Errorf("unable to decode businessFeatures#e1593e48: field features: %w", err)
				}
				b.Features = append(b.Features, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode businessFeatures#e1593e48: field features: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetFeatures returns value of Features field.
func (b *BusinessFeatures) GetFeatures() (value []BusinessFeatureClass) {
	if b == nil {
		return
	}
	return b.Features
}
