//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"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// WebDocumentClassArray is adapter for slice of WebDocumentClass.
type WebDocumentClassArray []WebDocumentClass

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

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

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

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

// AsWebDocument returns copy with only WebDocument constructors.
func (s WebDocumentClassArray) AsWebDocument() (to WebDocumentArray) {
	for _, elem := range s {
		value, ok := elem.(*WebDocument)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// AsWebDocumentNoProxy returns copy with only WebDocumentNoProxy constructors.
func (s WebDocumentClassArray) AsWebDocumentNoProxy() (to WebDocumentNoProxyArray) {
	for _, elem := range s {
		value, ok := elem.(*WebDocumentNoProxy)
		if !ok {
			continue
		}
		to = append(to, *value)
	}

	return to
}

// WebDocumentArray is adapter for slice of WebDocument.
type WebDocumentArray []WebDocument

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

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

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

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

// WebDocumentNoProxyArray is adapter for slice of WebDocumentNoProxy.
type WebDocumentNoProxyArray []WebDocumentNoProxy

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

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

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

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