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

// AiComposeToneClassArray is adapter for slice of AiComposeToneClass.
type AiComposeToneClassArray []AiComposeToneClass

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

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

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

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

// AsAiComposeTone returns copy with only AiComposeTone constructors.
func (s AiComposeToneClassArray) AsAiComposeTone() (to AiComposeToneArray) {
	for _, elem := range s {
		value, ok := elem.(*AiComposeTone)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsAiComposeToneDefault returns copy with only AiComposeToneDefault constructors.
func (s AiComposeToneClassArray) AsAiComposeToneDefault() (to AiComposeToneDefaultArray) {
	for _, elem := range s {
		value, ok := elem.(*AiComposeToneDefault)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AiComposeToneArray is adapter for slice of AiComposeTone.
type AiComposeToneArray []AiComposeTone

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

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

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

// Last returns last element of slice (if exists).
func (s AiComposeToneArray) Last() (v AiComposeTone, 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 *AiComposeToneArray) PopFirst() (v AiComposeTone, 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 AiComposeTone
	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 *AiComposeToneArray) Pop() (v AiComposeTone, 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 AiComposeTone by ID.
func (s AiComposeToneArray) SortByID() AiComposeToneArray {
	return s.Sort(func(a, b AiComposeTone) bool {
		return a.GetID() < b.GetID()
	})
}

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

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

// ToMap collects constructors to map.
func (s AiComposeToneArray) ToMap() map[int64]AiComposeTone {
	r := make(map[int64]AiComposeTone, len(s))
	s.FillMap(r)
	return r
}

// AiComposeToneDefaultArray is adapter for slice of AiComposeToneDefault.
type AiComposeToneDefaultArray []AiComposeToneDefault

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

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

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

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