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

// IPPortClassArray is adapter for slice of IPPortClass.
type IPPortClassArray []IPPortClass

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

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

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

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

// AsIPPort returns copy with only IPPort constructors.
func (s IPPortClassArray) AsIPPort() (to IPPortArray) {
	for _, elem := range s {
		value, ok := elem.(*IPPort)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsIPPortSecret returns copy with only IPPortSecret constructors.
func (s IPPortClassArray) AsIPPortSecret() (to IPPortSecretArray) {
	for _, elem := range s {
		value, ok := elem.(*IPPortSecret)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// IPPortArray is adapter for slice of IPPort.
type IPPortArray []IPPort

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

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

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

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

// IPPortSecretArray is adapter for slice of IPPortSecret.
type IPPortSecretArray []IPPortSecret

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

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

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

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