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

// SearchChatAffiliateProgramRequest represents TL type `searchChatAffiliateProgram#b02c09ba`.
type SearchChatAffiliateProgramRequest struct {
	// Username of the chat
	Username string
	// The referrer from an internalLinkTypeChatAffiliateProgram link
	Referrer string
}

// SearchChatAffiliateProgramRequestTypeID is TL type id of SearchChatAffiliateProgramRequest.
const SearchChatAffiliateProgramRequestTypeID = 0xb02c09ba

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

func (s *SearchChatAffiliateProgramRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Username == "") {
		return false
	}
	if !(s.Referrer == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchChatAffiliateProgramRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchChatAffiliateProgram",
		ID:   SearchChatAffiliateProgramRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Username",
			SchemaName: "username",
		},
		{
			Name:       "Referrer",
			SchemaName: "referrer",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchChatAffiliateProgramRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatAffiliateProgram#b02c09ba as nil")
	}
	b.PutID(SearchChatAffiliateProgramRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchChatAffiliateProgramRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatAffiliateProgram#b02c09ba as nil")
	}
	b.PutString(s.Username)
	b.PutString(s.Referrer)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchChatAffiliateProgramRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchChatAffiliateProgram#b02c09ba to nil")
	}
	if err := b.ConsumeID(SearchChatAffiliateProgramRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchChatAffiliateProgramRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchChatAffiliateProgram#b02c09ba to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: field username: %w", err)
		}
		s.Username = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: field referrer: %w", err)
		}
		s.Referrer = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchChatAffiliateProgramRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchChatAffiliateProgram#b02c09ba as nil")
	}
	b.ObjStart()
	b.PutID("searchChatAffiliateProgram")
	b.Comma()
	b.FieldStart("username")
	b.PutString(s.Username)
	b.Comma()
	b.FieldStart("referrer")
	b.PutString(s.Referrer)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchChatAffiliateProgramRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchChatAffiliateProgram#b02c09ba to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchChatAffiliateProgram"); err != nil {
				return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: %w", err)
			}
		case "username":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: field username: %w", err)
			}
			s.Username = value
		case "referrer":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchChatAffiliateProgram#b02c09ba: field referrer: %w", err)
			}
			s.Referrer = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUsername returns value of Username field.
func (s *SearchChatAffiliateProgramRequest) GetUsername() (value string) {
	if s == nil {
		return
	}
	return s.Username
}

// GetReferrer returns value of Referrer field.
func (s *SearchChatAffiliateProgramRequest) GetReferrer() (value string) {
	if s == nil {
		return
	}
	return s.Referrer
}

// SearchChatAffiliateProgram invokes method searchChatAffiliateProgram#b02c09ba returning error if any.
func (c *Client) SearchChatAffiliateProgram(ctx context.Context, request *SearchChatAffiliateProgramRequest) (*Chat, error) {
	var result Chat

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
