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

// MessagesSavedReactionTagsClassArray is adapter for slice of MessagesSavedReactionTagsClass.
type MessagesSavedReactionTagsClassArray []MessagesSavedReactionTagsClass

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

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

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

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

// AsMessagesSavedReactionTags returns copy with only MessagesSavedReactionTags constructors.
func (s MessagesSavedReactionTagsClassArray) AsMessagesSavedReactionTags() (to MessagesSavedReactionTagsArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesSavedReactionTags)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AppendOnlyModified appends only Modified constructors to
// given slice.
func (s MessagesSavedReactionTagsClassArray) AppendOnlyModified(to []*MessagesSavedReactionTags) []*MessagesSavedReactionTags {
	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 MessagesSavedReactionTagsClassArray) AsModified() (to []*MessagesSavedReactionTags) {
	return s.AppendOnlyModified(to)
}

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

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

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

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

// MessagesSavedReactionTagsArray is adapter for slice of MessagesSavedReactionTags.
type MessagesSavedReactionTagsArray []MessagesSavedReactionTags

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

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

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

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