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

// MessagePeerVoteClassArray is adapter for slice of MessagePeerVoteClass.
type MessagePeerVoteClassArray []MessagePeerVoteClass

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

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

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

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

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

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

// AsMessagePeerVote returns copy with only MessagePeerVote constructors.
func (s MessagePeerVoteClassArray) AsMessagePeerVote() (to MessagePeerVoteArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagePeerVote)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagePeerVoteInputOption returns copy with only MessagePeerVoteInputOption constructors.
func (s MessagePeerVoteClassArray) AsMessagePeerVoteInputOption() (to MessagePeerVoteInputOptionArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagePeerVoteInputOption)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagePeerVoteMultiple returns copy with only MessagePeerVoteMultiple constructors.
func (s MessagePeerVoteClassArray) AsMessagePeerVoteMultiple() (to MessagePeerVoteMultipleArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagePeerVoteMultiple)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// MessagePeerVoteArray is adapter for slice of MessagePeerVote.
type MessagePeerVoteArray []MessagePeerVote

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

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

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

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

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

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

// MessagePeerVoteInputOptionArray is adapter for slice of MessagePeerVoteInputOption.
type MessagePeerVoteInputOptionArray []MessagePeerVoteInputOption

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

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

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

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

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

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

// MessagePeerVoteMultipleArray is adapter for slice of MessagePeerVoteMultiple.
type MessagePeerVoteMultipleArray []MessagePeerVoteMultiple

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

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

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

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

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

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