package langext

import (
	"reflect"
)

func BoolCount(arr ...bool) int {
	c := 0
	for _, v := range arr {
		if v {
			c++
		}
	}
	return c
}

func Range[T IntegerConstraint](start T, end T) []T {
	r := make([]T, 0, end-start)
	for i := start; i < end; i++ {
		r = append(r, i)
	}
	return r
}

func ForceArray[T any](v []T) []T {
	if v == nil {
		return make([]T, 0)
	} else {
		return v
	}
}

func ReverseArray[T any](v []T) {
	for i, j := 0, len(v)-1; i < j; i, j = i+1, j-1 {
		v[i], v[j] = v[j], v[i]
	}
}

func InArray[T comparable](needle T, haystack []T) bool {
	for _, v := range haystack {
		if v == needle {
			return true
		}
	}
	return false
}

func ArrUnique[T comparable](array []T) []T {
	m := make(map[T]bool, len(array))
	for _, v := range array {
		m[v] = true
	}
	result := make([]T, 0, len(m))
	for v := range m {
		result = append(result, v)
	}
	return result
}

func ArrEqualsExact[T comparable](arr1 []T, arr2 []T) bool {
	if len(arr1) != len(arr2) {
		return false
	}

	for i := range arr1 {
		if arr1[i] != arr2[i] {
			return false
		}
	}
	return true
}

func ArrAll[T any](arr []T, fn func(T) bool) bool {
	for _, av := range arr {
		if !fn(av) {
			return false
		}
	}
	return true
}

func ArrAllErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) {
	for _, av := range arr {
		v, err := fn(av)
		if err != nil {
			return false, err
		}
		if !v {
			return false, nil
		}
	}
	return true, nil
}

func ArrNone[T any](arr []T, fn func(T) bool) bool {
	for _, av := range arr {
		if fn(av) {
			return false
		}
	}
	return true
}

func ArrNoneErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) {
	for _, av := range arr {
		v, err := fn(av)
		if err != nil {
			return false, err
		}
		if v {
			return false, nil
		}
	}
	return true, nil
}

func ArrAny[T any](arr []T, fn func(T) bool) bool {
	for _, av := range arr {
		if fn(av) {
			return true
		}
	}
	return false
}

func ArrAnyErr[T any](arr []T, fn func(T) (bool, error)) (bool, error) {
	for _, av := range arr {
		v, err := fn(av)
		if err != nil {
			return false, err
		}
		if v {
			return true, nil
		}
	}
	return false, nil
}

func ArrIdxAll(arr any, fn func(int) bool) bool {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		if !fn(i) {
			return false
		}
	}
	return true
}

func ArrIdxAllErr(arr any, fn func(int) (bool, error)) (bool, error) {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		v, err := fn(i)
		if err != nil {
			return false, err
		}
		if !v {
			return false, nil
		}
	}
	return true, nil
}

func ArrIdxNone(arr any, fn func(int) bool) bool {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		if fn(i) {
			return false
		}
	}
	return true
}

func ArrIdxNoneErr(arr any, fn func(int) (bool, error)) (bool, error) {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		v, err := fn(i)
		if err != nil {
			return false, err
		}
		if v {
			return false, nil
		}
	}
	return true, nil
}

func ArrIdxAny(arr any, fn func(int) bool) bool {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		if fn(i) {
			return true
		}
	}
	return false
}

func ArrIdxAnyErr(arr any, fn func(int) (bool, error)) (bool, error) {
	av := reflect.ValueOf(arr)
	for i := 0; i < av.Len(); i++ {
		v, err := fn(i)
		if err != nil {
			return false, err
		}
		if v {
			return true, nil
		}
	}
	return false, nil
}

func ArrFirst[T any](arr []T, comp func(v T) bool) (T, bool) {
	for _, v := range arr {
		if comp(v) {
			return v, true
		}
	}
	return *new(T), false
}

func ArrLast[T any](arr []T, comp func(v T) bool) (T, bool) {
	found := false
	result := *new(T)
	for _, v := range arr {
		if comp(v) {
			found = true
			result = v
		}
	}
	return result, found
}

func ArrFirstIndex[T comparable](arr []T, needle T) int {
	for i, v := range arr {
		if v == needle {
			return i
		}
	}
	return -1
}

func ArrLastIndex[T comparable](arr []T, needle T) int {
	result := -1
	for i, v := range arr {
		if v == needle {
			result = i
		}
	}
	return result
}

func AddToSet[T comparable](set []T, add T) []T {
	for _, v := range set {
		if v == add {
			return set
		}
	}
	return append(set, add)
}

func ArrMap[T1 any, T2 any](arr []T1, conv func(v T1) T2) []T2 {
	r := make([]T2, len(arr))
	for i, v := range arr {
		r[i] = conv(v)
	}
	return r
}

func ArrFilterMap[T1 any, T2 any](arr []T1, filter func(v T1) bool, conv func(v T1) T2) []T2 {
	r := make([]T2, 0, len(arr))
	for _, v := range arr {
		if filter(v) {
			r = append(r, conv(v))
		}
	}
	return r
}

func ArrSum[T NumberConstraint](arr []T) T {
	var r T = 0
	for _, v := range arr {
		r += v
	}
	return r
}