package utils

import "strings"

func Map[T any, R any](slice []T, mapper func(T) R) []R {
	result := make([]R, len(slice))
	for i, item := range slice {
		result[i] = mapper(item)
	}
	return result
}

func ConvertInterface[T any](data interface{}) T {
	var zero T

	// 检查 nil
	if data == nil {
		println("数据为空")
		return zero
	}

	// 类型断言
	if result, ok := data.(T); ok {
		// println("断言成功")
		return result
	}

	return zero
}

// 通用的Filter函数
func Filter[T any](slice []T, predicate func(T) bool) []T {
	var result []T
	for _, v := range slice {
		if predicate(v) {
			result = append(result, v)
		}
	}
	return result
}

func SliceSubtract[T comparable](slice1, slice2 []T) []T {
	exclude := make(map[T]bool)
	for _, v := range slice2 {
		exclude[v] = true
	}

	var result []T
	for _, v := range slice1 {
		if !exclude[v] {
			result = append(result, v)
		}
	}
	return result
}

func IsEmpty(value *string) bool {
	if value == nil {
		return true
	}
	if *value == "" {
		return true
	}
	if len(*value) == 0 {
		return true
	}
	if len(strings.TrimSpace(*value)) == 0 {
		return true
	}
	return false
}

func IsNotEmpty(value *string) bool {
	return !IsEmpty(value)
}
