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

// MessagesSavedDialogsClassArray is adapter for slice of MessagesSavedDialogsClass.
type MessagesSavedDialogsClassArray []MessagesSavedDialogsClass

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

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

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

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

// AsMessagesSavedDialogs returns copy with only MessagesSavedDialogs constructors.
func (s MessagesSavedDialogsClassArray) AsMessagesSavedDialogs() (to MessagesSavedDialogsArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesSavedDialogs)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagesSavedDialogsSlice returns copy with only MessagesSavedDialogsSlice constructors.
func (s MessagesSavedDialogsClassArray) AsMessagesSavedDialogsSlice() (to MessagesSavedDialogsSliceArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesSavedDialogsSlice)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsMessagesSavedDialogsNotModified returns copy with only MessagesSavedDialogsNotModified constructors.
func (s MessagesSavedDialogsClassArray) AsMessagesSavedDialogsNotModified() (to MessagesSavedDialogsNotModifiedArray) {
	for _, elem := range s {
		value, ok := elem.(*MessagesSavedDialogsNotModified)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

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

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

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

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

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

// MessagesSavedDialogsArray is adapter for slice of MessagesSavedDialogs.
type MessagesSavedDialogsArray []MessagesSavedDialogs

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

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

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

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

// MessagesSavedDialogsSliceArray is adapter for slice of MessagesSavedDialogsSlice.
type MessagesSavedDialogsSliceArray []MessagesSavedDialogsSlice

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

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

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

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

// MessagesSavedDialogsNotModifiedArray is adapter for slice of MessagesSavedDialogsNotModified.
type MessagesSavedDialogsNotModifiedArray []MessagesSavedDialogsNotModified

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

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

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

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