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

// GetFileExtensionRequest represents TL type `getFileExtension#f9adb934`.
type GetFileExtensionRequest struct {
	// The MIME type of the file
	MimeType string
}

// GetFileExtensionRequestTypeID is TL type id of GetFileExtensionRequest.
const GetFileExtensionRequestTypeID = 0xf9adb934

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

func (g *GetFileExtensionRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.MimeType == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetFileExtensionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getFileExtension",
		ID:   GetFileExtensionRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MimeType",
			SchemaName: "mime_type",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetFileExtensionRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getFileExtension#f9adb934 as nil")
	}
	b.PutID(GetFileExtensionRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetFileExtensionRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getFileExtension#f9adb934 as nil")
	}
	b.PutString(g.MimeType)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetFileExtensionRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getFileExtension#f9adb934 to nil")
	}
	if err := b.ConsumeID(GetFileExtensionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getFileExtension#f9adb934: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetFileExtensionRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getFileExtension#f9adb934 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getFileExtension#f9adb934: field mime_type: %w", err)
		}
		g.MimeType = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetFileExtensionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getFileExtension#f9adb934 as nil")
	}
	b.ObjStart()
	b.PutID("getFileExtension")
	b.Comma()
	b.FieldStart("mime_type")
	b.PutString(g.MimeType)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetFileExtensionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getFileExtension#f9adb934 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getFileExtension"); err != nil {
				return fmt.Errorf("unable to decode getFileExtension#f9adb934: %w", err)
			}
		case "mime_type":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getFileExtension#f9adb934: field mime_type: %w", err)
			}
			g.MimeType = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMimeType returns value of MimeType field.
func (g *GetFileExtensionRequest) GetMimeType() (value string) {
	if g == nil {
		return
	}
	return g.MimeType
}

// GetFileExtension invokes method getFileExtension#f9adb934 returning error if any.
func (c *Client) GetFileExtension(ctx context.Context, mimetype string) (*Text, error) {
	var result Text

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