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

// MessageClassArray is adapter for slice of MessageClass.
type MessageClassArray []MessageClass

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

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

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

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

// SortByID sorts slice of MessageClass by ID.
func (s MessageClassArray) SortByID() MessageClassArray {
	return s.Sort(func(a, b MessageClass) bool {
		return a.GetID() < b.GetID()
	})
}

// SortStableByID sorts slice of MessageClass by ID.
func (s MessageClassArray) SortStableByID() MessageClassArray {
	return s.SortStable(func(a, b MessageClass) bool {
		return a.GetID() < b.GetID()
	})
}

// FillMessageEmptyMap fills only MessageEmpty constructors to given map.
func (s MessageClassArray) FillMessageEmptyMap(to map[int]*MessageEmpty) {
	for _, elem := range s {
		value, ok := elem.(*MessageEmpty)
		if !ok {
			continue
		}
		to[value.GetID()] = value
	}
}

// MessageEmptyToMap collects only MessageEmpty constructors to map.
func (s MessageClassArray) MessageEmptyToMap() map[int]*MessageEmpty {
	r := make(map[int]*MessageEmpty, len(s))
	s.FillMessageEmptyMap(r)
	return r
}

// AsMessageEmpty returns copy with only MessageEmpty constructors.
func (s MessageClassArray) AsMessageEmpty() (to MessageEmptyArray) {
	for _, elem := range s {
		value, ok := elem.(*MessageEmpty)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// FillMessageMap fills only Message constructors to given map.
func (s MessageClassArray) FillMessageMap(to map[int]*Message) {
	for _, elem := range s {
		value, ok := elem.(*Message)
		if !ok {
			continue
		}
		to[value.GetID()] = value
	}
}

// MessageToMap collects only Message constructors to map.
func (s MessageClassArray) MessageToMap() map[int]*Message {
	r := make(map[int]*Message, len(s))
	s.FillMessageMap(r)
	return r
}

// AsMessage returns copy with only Message constructors.
func (s MessageClassArray) AsMessage() (to MessageArray) {
	for _, elem := range s {
		value, ok := elem.(*Message)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// FillMessageServiceMap fills only MessageService constructors to given map.
func (s MessageClassArray) FillMessageServiceMap(to map[int]*MessageService) {
	for _, elem := range s {
		value, ok := elem.(*MessageService)
		if !ok {
			continue
		}
		to[value.GetID()] = value
	}
}

// MessageServiceToMap collects only MessageService constructors to map.
func (s MessageClassArray) MessageServiceToMap() map[int]*MessageService {
	r := make(map[int]*MessageService, len(s))
	s.FillMessageServiceMap(r)
	return r
}

// AsMessageService returns copy with only MessageService constructors.
func (s MessageClassArray) AsMessageService() (to MessageServiceArray) {
	for _, elem := range s {
		value, ok := elem.(*MessageService)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// FillNotEmptyMap fills only NotEmpty constructors to given map.
func (s MessageClassArray) FillNotEmptyMap(to map[int]NotEmptyMessage) {
	for _, elem := range s {
		value, ok := elem.AsNotEmpty()
		if !ok {
			continue
		}
		to[value.GetID()] = value
	}
}

// NotEmptyToMap collects only NotEmpty constructors to map.
func (s MessageClassArray) NotEmptyToMap() map[int]NotEmptyMessage {
	r := make(map[int]NotEmptyMessage, len(s))
	s.FillNotEmptyMap(r)
	return r
}

// AppendOnlyNotEmpty appends only NotEmpty constructors to
// given slice.
func (s MessageClassArray) AppendOnlyNotEmpty(to []NotEmptyMessage) []NotEmptyMessage {
	for _, elem := range s {
		value, ok := elem.AsNotEmpty()
		if !ok {
			continue
		}
		to = append(to, value)
	}

	return to
}

// AsNotEmpty returns copy with only NotEmpty constructors.
func (s MessageClassArray) AsNotEmpty() (to []NotEmptyMessage) {
	return s.AppendOnlyNotEmpty(to)
}

// FirstAsNotEmpty returns first element of slice (if exists).
func (s MessageClassArray) FirstAsNotEmpty() (v NotEmptyMessage, ok bool) {
	value, ok := s.First()
	if !ok {
		return
	}
	return value.AsNotEmpty()
}

// LastAsNotEmpty returns last element of slice (if exists).
func (s MessageClassArray) LastAsNotEmpty() (v NotEmptyMessage, ok bool) {
	value, ok := s.Last()
	if !ok {
		return
	}
	return value.AsNotEmpty()
}

// PopFirstAsNotEmpty returns element of slice (if exists).
func (s *MessageClassArray) PopFirstAsNotEmpty() (v NotEmptyMessage, ok bool) {
	value, ok := s.PopFirst()
	if !ok {
		return
	}
	return value.AsNotEmpty()
}

// PopAsNotEmpty returns element of slice (if exists).
func (s *MessageClassArray) PopAsNotEmpty() (v NotEmptyMessage, ok bool) {
	value, ok := s.Pop()
	if !ok {
		return
	}
	return value.AsNotEmpty()
}

// MessageEmptyArray is adapter for slice of MessageEmpty.
type MessageEmptyArray []MessageEmpty

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

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

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

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

// SortByID sorts slice of MessageEmpty by ID.
func (s MessageEmptyArray) SortByID() MessageEmptyArray {
	return s.Sort(func(a, b MessageEmpty) bool {
		return a.GetID() < b.GetID()
	})
}

// SortStableByID sorts slice of MessageEmpty by ID.
func (s MessageEmptyArray) SortStableByID() MessageEmptyArray {
	return s.SortStable(func(a, b MessageEmpty) bool {
		return a.GetID() < b.GetID()
	})
}

// FillMap fills constructors to given map.
func (s MessageEmptyArray) FillMap(to map[int]MessageEmpty) {
	for _, value := range s {
		to[value.GetID()] = value
	}
}

// ToMap collects constructors to map.
func (s MessageEmptyArray) ToMap() map[int]MessageEmpty {
	r := make(map[int]MessageEmpty, len(s))
	s.FillMap(r)
	return r
}

// MessageArray is adapter for slice of Message.
type MessageArray []Message

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

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

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

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

// SortByID sorts slice of Message by ID.
func (s MessageArray) SortByID() MessageArray {
	return s.Sort(func(a, b Message) bool {
		return a.GetID() < b.GetID()
	})
}

// SortStableByID sorts slice of Message by ID.
func (s MessageArray) SortStableByID() MessageArray {
	return s.SortStable(func(a, b Message) bool {
		return a.GetID() < b.GetID()
	})
}

// SortByDate sorts slice of Message by Date.
func (s MessageArray) SortByDate() MessageArray {
	return s.Sort(func(a, b Message) bool {
		return a.GetDate() < b.GetDate()
	})
}

// SortStableByDate sorts slice of Message by Date.
func (s MessageArray) SortStableByDate() MessageArray {
	return s.SortStable(func(a, b Message) bool {
		return a.GetDate() < b.GetDate()
	})
}

// FillMap fills constructors to given map.
func (s MessageArray) FillMap(to map[int]Message) {
	for _, value := range s {
		to[value.GetID()] = value
	}
}

// ToMap collects constructors to map.
func (s MessageArray) ToMap() map[int]Message {
	r := make(map[int]Message, len(s))
	s.FillMap(r)
	return r
}

// MessageServiceArray is adapter for slice of MessageService.
type MessageServiceArray []MessageService

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

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

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

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

// SortByID sorts slice of MessageService by ID.
func (s MessageServiceArray) SortByID() MessageServiceArray {
	return s.Sort(func(a, b MessageService) bool {
		return a.GetID() < b.GetID()
	})
}

// SortStableByID sorts slice of MessageService by ID.
func (s MessageServiceArray) SortStableByID() MessageServiceArray {
	return s.SortStable(func(a, b MessageService) bool {
		return a.GetID() < b.GetID()
	})
}

// SortByDate sorts slice of MessageService by Date.
func (s MessageServiceArray) SortByDate() MessageServiceArray {
	return s.Sort(func(a, b MessageService) bool {
		return a.GetDate() < b.GetDate()
	})
}

// SortStableByDate sorts slice of MessageService by Date.
func (s MessageServiceArray) SortStableByDate() MessageServiceArray {
	return s.SortStable(func(a, b MessageService) bool {
		return a.GetDate() < b.GetDate()
	})
}

// FillMap fills constructors to given map.
func (s MessageServiceArray) FillMap(to map[int]MessageService) {
	for _, value := range s {
		to[value.GetID()] = value
	}
}

// ToMap collects constructors to map.
func (s MessageServiceArray) ToMap() map[int]MessageService {
	r := make(map[int]MessageService, len(s))
	s.FillMap(r)
	return r
}
