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

// RequestedPeerClassArray is adapter for slice of RequestedPeerClass.
type RequestedPeerClassArray []RequestedPeerClass

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

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

// Retain filters in-place slice of RequestedPeerClass.
func (s RequestedPeerClassArray) Retain(keep func(x RequestedPeerClass) bool) RequestedPeerClassArray {
	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 RequestedPeerClassArray) First() (v RequestedPeerClass, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestedPeerClassArray) Last() (v RequestedPeerClass, 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 *RequestedPeerClassArray) PopFirst() (v RequestedPeerClass, 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 RequestedPeerClass
	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 *RequestedPeerClassArray) Pop() (v RequestedPeerClass, 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
}

// AsRequestedPeerUser returns copy with only RequestedPeerUser constructors.
func (s RequestedPeerClassArray) AsRequestedPeerUser() (to RequestedPeerUserArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestedPeerUser)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsRequestedPeerChat returns copy with only RequestedPeerChat constructors.
func (s RequestedPeerClassArray) AsRequestedPeerChat() (to RequestedPeerChatArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestedPeerChat)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsRequestedPeerChannel returns copy with only RequestedPeerChannel constructors.
func (s RequestedPeerClassArray) AsRequestedPeerChannel() (to RequestedPeerChannelArray) {
	for _, elem := range s {
		value, ok := elem.(*RequestedPeerChannel)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// RequestedPeerUserArray is adapter for slice of RequestedPeerUser.
type RequestedPeerUserArray []RequestedPeerUser

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

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

// Retain filters in-place slice of RequestedPeerUser.
func (s RequestedPeerUserArray) Retain(keep func(x RequestedPeerUser) bool) RequestedPeerUserArray {
	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 RequestedPeerUserArray) First() (v RequestedPeerUser, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestedPeerUserArray) Last() (v RequestedPeerUser, 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 *RequestedPeerUserArray) PopFirst() (v RequestedPeerUser, 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 RequestedPeerUser
	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 *RequestedPeerUserArray) Pop() (v RequestedPeerUser, 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
}

// RequestedPeerChatArray is adapter for slice of RequestedPeerChat.
type RequestedPeerChatArray []RequestedPeerChat

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

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

// Retain filters in-place slice of RequestedPeerChat.
func (s RequestedPeerChatArray) Retain(keep func(x RequestedPeerChat) bool) RequestedPeerChatArray {
	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 RequestedPeerChatArray) First() (v RequestedPeerChat, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestedPeerChatArray) Last() (v RequestedPeerChat, 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 *RequestedPeerChatArray) PopFirst() (v RequestedPeerChat, 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 RequestedPeerChat
	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 *RequestedPeerChatArray) Pop() (v RequestedPeerChat, 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
}

// RequestedPeerChannelArray is adapter for slice of RequestedPeerChannel.
type RequestedPeerChannelArray []RequestedPeerChannel

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

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

// Retain filters in-place slice of RequestedPeerChannel.
func (s RequestedPeerChannelArray) Retain(keep func(x RequestedPeerChannel) bool) RequestedPeerChannelArray {
	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 RequestedPeerChannelArray) First() (v RequestedPeerChannel, ok bool) {
	if len(s) < 1 {
		return
	}
	return s[0], true
}

// Last returns last element of slice (if exists).
func (s RequestedPeerChannelArray) Last() (v RequestedPeerChannel, 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 *RequestedPeerChannelArray) PopFirst() (v RequestedPeerChannel, 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 RequestedPeerChannel
	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 *RequestedPeerChannelArray) Pop() (v RequestedPeerChannel, 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
}
