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

// UsersSavedMusicClassArray is adapter for slice of UsersSavedMusicClass.
type UsersSavedMusicClassArray []UsersSavedMusicClass

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

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

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

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

// AsUsersSavedMusicNotModified returns copy with only UsersSavedMusicNotModified constructors.
func (s UsersSavedMusicClassArray) AsUsersSavedMusicNotModified() (to UsersSavedMusicNotModifiedArray) {
	for _, elem := range s {
		value, ok := elem.(*UsersSavedMusicNotModified)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsUsersSavedMusic returns copy with only UsersSavedMusic constructors.
func (s UsersSavedMusicClassArray) AsUsersSavedMusic() (to UsersSavedMusicArray) {
	for _, elem := range s {
		value, ok := elem.(*UsersSavedMusic)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

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

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

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

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

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

// UsersSavedMusicNotModifiedArray is adapter for slice of UsersSavedMusicNotModified.
type UsersSavedMusicNotModifiedArray []UsersSavedMusicNotModified

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

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

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

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

// UsersSavedMusicArray is adapter for slice of UsersSavedMusic.
type UsersSavedMusicArray []UsersSavedMusic

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

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

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

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