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

// BotAppClassArray is adapter for slice of BotAppClass.
type BotAppClassArray []BotAppClass

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

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

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

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

// AsBotApp returns copy with only BotApp constructors.
func (s BotAppClassArray) AsBotApp() (to BotAppArray) {
	for _, elem := range s {
		value, ok := elem.(*BotApp)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// FillModifiedMap fills only Modified constructors to given map.
func (s BotAppClassArray) FillModifiedMap(to map[int64]*BotApp) {
	for _, elem := range s {
		value, ok := elem.AsModified()
		if !ok {
			continue
		}
		to[value.GetID()] = value
	}
}

// ModifiedToMap collects only Modified constructors to map.
func (s BotAppClassArray) ModifiedToMap() map[int64]*BotApp {
	r := make(map[int64]*BotApp, len(s))
	s.FillModifiedMap(r)
	return r
}

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

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

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

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

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

// BotAppArray is adapter for slice of BotApp.
type BotAppArray []BotApp

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

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

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

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

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

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

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