//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"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesMessagesClassArray is adapter for slice of MessagesMessagesClass.
type MessagesMessagesClassArray []MessagesMessagesClass

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

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

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

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

// AsMessagesMessages returns copy with only MessagesMessages constructors.
func (s MessagesMessagesClassArray) AsMessagesMessages() (to MessagesMessagesArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesMessages)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagesMessagesSlice returns copy with only MessagesMessagesSlice constructors.
func (s MessagesMessagesClassArray) AsMessagesMessagesSlice() (to MessagesMessagesSliceArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesMessagesSlice)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagesChannelMessages returns copy with only MessagesChannelMessages constructors.
func (s MessagesMessagesClassArray) AsMessagesChannelMessages() (to MessagesChannelMessagesArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesChannelMessages)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagesMessagesNotModified returns copy with only MessagesMessagesNotModified constructors.
func (s MessagesMessagesClassArray) AsMessagesMessagesNotModified() (to MessagesMessagesNotModifiedArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesMessagesNotModified)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AppendOnlyModified appends only Modified constructors to
// given slice.
func (s MessagesMessagesClassArray) AppendOnlyModified(to []ModifiedMessagesMessages) []ModifiedMessagesMessages {
	for _, elem := range s {
		value, ok := elem.AsModified()
		if !ok {
			continue
		}
		to = append(to, value)
	}

	return to
}

// AsModified returns copy with only Modified constructors.
func (s MessagesMessagesClassArray) AsModified() (to []ModifiedMessagesMessages) {
	return s.AppendOnlyModified(to)
}

// FirstAsModified returns first element of slice (if exists).
func (s MessagesMessagesClassArray) FirstAsModified() (v ModifiedMessagesMessages, ok bool) {
	value, ok := s.First()
	if !ok {
		return
	}
	return value.AsModified()
}

// LastAsModified returns last element of slice (if exists).
func (s MessagesMessagesClassArray) LastAsModified() (v ModifiedMessagesMessages, ok bool) {
	value, ok := s.Last()
	if !ok {
		return
	}
	return value.AsModified()
}

// PopFirstAsModified returns element of slice (if exists).
func (s *MessagesMessagesClassArray) PopFirstAsModified() (v ModifiedMessagesMessages, ok bool) {
	value, ok := s.PopFirst()
	if !ok {
		return
	}
	return value.AsModified()
}

// PopAsModified returns element of slice (if exists).
func (s *MessagesMessagesClassArray) PopAsModified() (v ModifiedMessagesMessages, ok bool) {
	value, ok := s.Pop()
	if !ok {
		return
	}
	return value.AsModified()
}

// MessagesMessagesArray is adapter for slice of MessagesMessages.
type MessagesMessagesArray []MessagesMessages

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

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

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

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

// MessagesMessagesSliceArray is adapter for slice of MessagesMessagesSlice.
type MessagesMessagesSliceArray []MessagesMessagesSlice

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

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

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

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

// MessagesChannelMessagesArray is adapter for slice of MessagesChannelMessages.
type MessagesChannelMessagesArray []MessagesChannelMessages

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

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

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

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

// MessagesMessagesNotModifiedArray is adapter for slice of MessagesMessagesNotModified.
type MessagesMessagesNotModifiedArray []MessagesMessagesNotModified

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

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

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

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