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

// InputFileClassArray is adapter for slice of InputFileClass.
type InputFileClassArray []InputFileClass

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

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

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

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

// AsInputFile returns copy with only InputFile constructors.
func (s InputFileClassArray) AsInputFile() (to InputFileArray) {
	for _, elem := range s {
		value, ok := elem.(*InputFile)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsInputFileBig returns copy with only InputFileBig constructors.
func (s InputFileClassArray) AsInputFileBig() (to InputFileBigArray) {
	for _, elem := range s {
		value, ok := elem.(*InputFileBig)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsInputFileStoryDocument returns copy with only InputFileStoryDocument constructors.
func (s InputFileClassArray) AsInputFileStoryDocument() (to InputFileStoryDocumentArray) {
	for _, elem := range s {
		value, ok := elem.(*InputFileStoryDocument)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// InputFileArray is adapter for slice of InputFile.
type InputFileArray []InputFile

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

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

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

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

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

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

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

// InputFileBigArray is adapter for slice of InputFileBig.
type InputFileBigArray []InputFileBig

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

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

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

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

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

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

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

// InputFileStoryDocumentArray is adapter for slice of InputFileStoryDocument.
type InputFileStoryDocumentArray []InputFileStoryDocument

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

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

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

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