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

// WebPageAttributeClassArray is adapter for slice of WebPageAttributeClass.
type WebPageAttributeClassArray []WebPageAttributeClass

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

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

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

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

// AsWebPageAttributeTheme returns copy with only WebPageAttributeTheme constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeTheme() (to WebPageAttributeThemeArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeTheme)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebPageAttributeStory returns copy with only WebPageAttributeStory constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeStory() (to WebPageAttributeStoryArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeStory)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebPageAttributeStickerSet returns copy with only WebPageAttributeStickerSet constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeStickerSet() (to WebPageAttributeStickerSetArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeStickerSet)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebPageAttributeUniqueStarGift returns copy with only WebPageAttributeUniqueStarGift constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeUniqueStarGift() (to WebPageAttributeUniqueStarGiftArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeUniqueStarGift)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebPageAttributeStarGiftCollection returns copy with only WebPageAttributeStarGiftCollection constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeStarGiftCollection() (to WebPageAttributeStarGiftCollectionArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeStarGiftCollection)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebPageAttributeStarGiftAuction returns copy with only WebPageAttributeStarGiftAuction constructors.
func (s WebPageAttributeClassArray) AsWebPageAttributeStarGiftAuction() (to WebPageAttributeStarGiftAuctionArray) {
	for _, elem := range s {
		value, ok := elem.(*WebPageAttributeStarGiftAuction)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// WebPageAttributeThemeArray is adapter for slice of WebPageAttributeTheme.
type WebPageAttributeThemeArray []WebPageAttributeTheme

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

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

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

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

// WebPageAttributeStoryArray is adapter for slice of WebPageAttributeStory.
type WebPageAttributeStoryArray []WebPageAttributeStory

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

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

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

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

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

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

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

// WebPageAttributeStickerSetArray is adapter for slice of WebPageAttributeStickerSet.
type WebPageAttributeStickerSetArray []WebPageAttributeStickerSet

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

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

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

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

// WebPageAttributeUniqueStarGiftArray is adapter for slice of WebPageAttributeUniqueStarGift.
type WebPageAttributeUniqueStarGiftArray []WebPageAttributeUniqueStarGift

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

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

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

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

// WebPageAttributeStarGiftCollectionArray is adapter for slice of WebPageAttributeStarGiftCollection.
type WebPageAttributeStarGiftCollectionArray []WebPageAttributeStarGiftCollection

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

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

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

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

// WebPageAttributeStarGiftAuctionArray is adapter for slice of WebPageAttributeStarGiftAuction.
type WebPageAttributeStarGiftAuctionArray []WebPageAttributeStarGiftAuction

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

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

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

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