//go:build !no_gotd_slices
// +build !no_gotd_slices

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

// RequestPeerTypeClassArray is adapter for slice of RequestPeerTypeClass.
type RequestPeerTypeClassArray []RequestPeerTypeClass

// Sort sorts slice of RequestPeerTypeClass.
func (s RequestPeerTypeClassArray) Sort(less func(a, b RequestPeerTypeClass) bool) RequestPeerTypeClassArray {
	sort.Slice(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// SortStable sorts slice of RequestPeerTypeClass.
func (s RequestPeerTypeClassArray) SortStable(less func(a, b RequestPeerTypeClass) bool) RequestPeerTypeClassArray {
	sort.SliceStable(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// Retain filters in-place slice of RequestPeerTypeClass.
func (s RequestPeerTypeClassArray) Retain(keep func(x RequestPeerTypeClass) bool) RequestPeerTypeClassArray {
	n := 0
	for _, x := range s {
		if keep(x) {
			s[n] = x
			n++
		}
	}
	s = s[:n]

	return s
}

// First returns first element of slice (if exists).
func (s RequestPeerTypeClassArray) First() (v RequestPeerTypeClass, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestPeerTypeClassArray) Last() (v RequestPeerTypeClass, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[len(s)-1], true
}

// PopFirst returns first element of slice (if exists) and deletes it.
func (s *RequestPeerTypeClassArray) PopFirst() (v RequestPeerTypeClass, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[0]

	// Delete by index from SliceTricks.
	copy(a[0:], a[1:])
	var zero RequestPeerTypeClass
	a[len(a)-1] = zero
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// Pop returns last element of slice (if exists) and deletes it.
func (s *RequestPeerTypeClassArray) Pop() (v RequestPeerTypeClass, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[len(a)-1]
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// AsRequestPeerTypeUser returns copy with only RequestPeerTypeUser constructors.
func (s RequestPeerTypeClassArray) AsRequestPeerTypeUser() (to RequestPeerTypeUserArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestPeerTypeUser)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsRequestPeerTypeChat returns copy with only RequestPeerTypeChat constructors.
func (s RequestPeerTypeClassArray) AsRequestPeerTypeChat() (to RequestPeerTypeChatArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestPeerTypeChat)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsRequestPeerTypeBroadcast returns copy with only RequestPeerTypeBroadcast constructors.
func (s RequestPeerTypeClassArray) AsRequestPeerTypeBroadcast() (to RequestPeerTypeBroadcastArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestPeerTypeBroadcast)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsRequestPeerTypeCreateBot returns copy with only RequestPeerTypeCreateBot constructors.
func (s RequestPeerTypeClassArray) AsRequestPeerTypeCreateBot() (to RequestPeerTypeCreateBotArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestPeerTypeCreateBot)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// RequestPeerTypeUserArray is adapter for slice of RequestPeerTypeUser.
type RequestPeerTypeUserArray []RequestPeerTypeUser

// Sort sorts slice of RequestPeerTypeUser.
func (s RequestPeerTypeUserArray) Sort(less func(a, b RequestPeerTypeUser) bool) RequestPeerTypeUserArray {
	sort.Slice(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// SortStable sorts slice of RequestPeerTypeUser.
func (s RequestPeerTypeUserArray) SortStable(less func(a, b RequestPeerTypeUser) bool) RequestPeerTypeUserArray {
	sort.SliceStable(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// Retain filters in-place slice of RequestPeerTypeUser.
func (s RequestPeerTypeUserArray) Retain(keep func(x RequestPeerTypeUser) bool) RequestPeerTypeUserArray {
	n := 0
	for _, x := range s {
		if keep(x) {
			s[n] = x
			n++
		}
	}
	s = s[:n]

	return s
}

// First returns first element of slice (if exists).
func (s RequestPeerTypeUserArray) First() (v RequestPeerTypeUser, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestPeerTypeUserArray) Last() (v RequestPeerTypeUser, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[len(s)-1], true
}

// PopFirst returns first element of slice (if exists) and deletes it.
func (s *RequestPeerTypeUserArray) PopFirst() (v RequestPeerTypeUser, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[0]

	// Delete by index from SliceTricks.
	copy(a[0:], a[1:])
	var zero RequestPeerTypeUser
	a[len(a)-1] = zero
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// Pop returns last element of slice (if exists) and deletes it.
func (s *RequestPeerTypeUserArray) Pop() (v RequestPeerTypeUser, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[len(a)-1]
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// RequestPeerTypeChatArray is adapter for slice of RequestPeerTypeChat.
type RequestPeerTypeChatArray []RequestPeerTypeChat

// Sort sorts slice of RequestPeerTypeChat.
func (s RequestPeerTypeChatArray) Sort(less func(a, b RequestPeerTypeChat) bool) RequestPeerTypeChatArray {
	sort.Slice(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// SortStable sorts slice of RequestPeerTypeChat.
func (s RequestPeerTypeChatArray) SortStable(less func(a, b RequestPeerTypeChat) bool) RequestPeerTypeChatArray {
	sort.SliceStable(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// Retain filters in-place slice of RequestPeerTypeChat.
func (s RequestPeerTypeChatArray) Retain(keep func(x RequestPeerTypeChat) bool) RequestPeerTypeChatArray {
	n := 0
	for _, x := range s {
		if keep(x) {
			s[n] = x
			n++
		}
	}
	s = s[:n]

	return s
}

// First returns first element of slice (if exists).
func (s RequestPeerTypeChatArray) First() (v RequestPeerTypeChat, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestPeerTypeChatArray) Last() (v RequestPeerTypeChat, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[len(s)-1], true
}

// PopFirst returns first element of slice (if exists) and deletes it.
func (s *RequestPeerTypeChatArray) PopFirst() (v RequestPeerTypeChat, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[0]

	// Delete by index from SliceTricks.
	copy(a[0:], a[1:])
	var zero RequestPeerTypeChat
	a[len(a)-1] = zero
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// Pop returns last element of slice (if exists) and deletes it.
func (s *RequestPeerTypeChatArray) Pop() (v RequestPeerTypeChat, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[len(a)-1]
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// RequestPeerTypeBroadcastArray is adapter for slice of RequestPeerTypeBroadcast.
type RequestPeerTypeBroadcastArray []RequestPeerTypeBroadcast

// Sort sorts slice of RequestPeerTypeBroadcast.
func (s RequestPeerTypeBroadcastArray) Sort(less func(a, b RequestPeerTypeBroadcast) bool) RequestPeerTypeBroadcastArray {
	sort.Slice(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// SortStable sorts slice of RequestPeerTypeBroadcast.
func (s RequestPeerTypeBroadcastArray) SortStable(less func(a, b RequestPeerTypeBroadcast) bool) RequestPeerTypeBroadcastArray {
	sort.SliceStable(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// Retain filters in-place slice of RequestPeerTypeBroadcast.
func (s RequestPeerTypeBroadcastArray) Retain(keep func(x RequestPeerTypeBroadcast) bool) RequestPeerTypeBroadcastArray {
	n := 0
	for _, x := range s {
		if keep(x) {
			s[n] = x
			n++
		}
	}
	s = s[:n]

	return s
}

// First returns first element of slice (if exists).
func (s RequestPeerTypeBroadcastArray) First() (v RequestPeerTypeBroadcast, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestPeerTypeBroadcastArray) Last() (v RequestPeerTypeBroadcast, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[len(s)-1], true
}

// PopFirst returns first element of slice (if exists) and deletes it.
func (s *RequestPeerTypeBroadcastArray) PopFirst() (v RequestPeerTypeBroadcast, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[0]

	// Delete by index from SliceTricks.
	copy(a[0:], a[1:])
	var zero RequestPeerTypeBroadcast
	a[len(a)-1] = zero
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// Pop returns last element of slice (if exists) and deletes it.
func (s *RequestPeerTypeBroadcastArray) Pop() (v RequestPeerTypeBroadcast, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[len(a)-1]
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// RequestPeerTypeCreateBotArray is adapter for slice of RequestPeerTypeCreateBot.
type RequestPeerTypeCreateBotArray []RequestPeerTypeCreateBot

// Sort sorts slice of RequestPeerTypeCreateBot.
func (s RequestPeerTypeCreateBotArray) Sort(less func(a, b RequestPeerTypeCreateBot) bool) RequestPeerTypeCreateBotArray {
	sort.Slice(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// SortStable sorts slice of RequestPeerTypeCreateBot.
func (s RequestPeerTypeCreateBotArray) SortStable(less func(a, b RequestPeerTypeCreateBot) bool) RequestPeerTypeCreateBotArray {
	sort.SliceStable(s, func(i, j int) bool {
		return less(s[i], s[j])
	})
	return s
}

// Retain filters in-place slice of RequestPeerTypeCreateBot.
func (s RequestPeerTypeCreateBotArray) Retain(keep func(x RequestPeerTypeCreateBot) bool) RequestPeerTypeCreateBotArray {
	n := 0
	for _, x := range s {
		if keep(x) {
			s[n] = x
			n++
		}
	}
	s = s[:n]

	return s
}

// First returns first element of slice (if exists).
func (s RequestPeerTypeCreateBotArray) First() (v RequestPeerTypeCreateBot, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestPeerTypeCreateBotArray) Last() (v RequestPeerTypeCreateBot, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[len(s)-1], true
}

// PopFirst returns first element of slice (if exists) and deletes it.
func (s *RequestPeerTypeCreateBotArray) PopFirst() (v RequestPeerTypeCreateBot, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[0]

	// Delete by index from SliceTricks.
	copy(a[0:], a[1:])
	var zero RequestPeerTypeCreateBot
	a[len(a)-1] = zero
	a = a[:len(a)-1]
	*s = a

	return v, true
}

// Pop returns last element of slice (if exists) and deletes it.
func (s *RequestPeerTypeCreateBotArray) Pop() (v RequestPeerTypeCreateBot, ok bool) {
	if s == nil || len(*s) < 1 {
		return
	}

	a := *s
	v = a[len(a)-1]
	a = a[:len(a)-1]
	*s = a

	return v, true
}
