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

// KeyboardButtonRow represents TL type `keyboardButtonRow#77608b83`.
// Inline keyboard row
//
// See https://core.telegram.org/constructor/keyboardButtonRow for reference.
type KeyboardButtonRow struct {
	// Bot or inline keyboard buttons
	Buttons []KeyboardButtonClass
}

// KeyboardButtonRowTypeID is TL type id of KeyboardButtonRow.
const KeyboardButtonRowTypeID = 0x77608b83

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

func (k *KeyboardButtonRow) Zero() bool {
	if k == nil {
		return true
	}
	if !(k.Buttons == nil) {
		return false
	}

	return true
}

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

// FillFrom fills KeyboardButtonRow from given interface.
func (k *KeyboardButtonRow) FillFrom(from interface {
	GetButtons() (value []KeyboardButtonClass)
}) {
	k.Buttons = from.GetButtons()
}

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

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

// TypeInfo returns info about TL type.
func (k *KeyboardButtonRow) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "keyboardButtonRow",
		ID:   KeyboardButtonRowTypeID,
	}
	if k == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Buttons",
			SchemaName: "buttons",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (k *KeyboardButtonRow) Encode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButtonRow#77608b83 as nil")
	}
	b.PutID(KeyboardButtonRowTypeID)
	return k.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (k *KeyboardButtonRow) EncodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't encode keyboardButtonRow#77608b83 as nil")
	}
	b.PutVectorHeader(len(k.Buttons))
	for idx, v := range k.Buttons {
		if v == nil {
			return fmt.Errorf("unable to encode keyboardButtonRow#77608b83: field buttons element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode keyboardButtonRow#77608b83: field buttons element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (k *KeyboardButtonRow) Decode(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButtonRow#77608b83 to nil")
	}
	if err := b.ConsumeID(KeyboardButtonRowTypeID); err != nil {
		return fmt.Errorf("unable to decode keyboardButtonRow#77608b83: %w", err)
	}
	return k.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (k *KeyboardButtonRow) DecodeBare(b *bin.Buffer) error {
	if k == nil {
		return fmt.Errorf("can't decode keyboardButtonRow#77608b83 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode keyboardButtonRow#77608b83: field buttons: %w", err)
		}

		if headerLen > 0 {
			k.Buttons = make([]KeyboardButtonClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeKeyboardButton(b)
			if err != nil {
				return fmt.Errorf("unable to decode keyboardButtonRow#77608b83: field buttons: %w", err)
			}
			k.Buttons = append(k.Buttons, value)
		}
	}
	return nil
}

// GetButtons returns value of Buttons field.
func (k *KeyboardButtonRow) GetButtons() (value []KeyboardButtonClass) {
	if k == nil {
		return
	}
	return k.Buttons
}

// MapButtons returns field Buttons wrapped in KeyboardButtonClassArray helper.
func (k *KeyboardButtonRow) MapButtons() (value KeyboardButtonClassArray) {
	return KeyboardButtonClassArray(k.Buttons)
}
