package validate_utils

import (
	"fmt"
	"reflect"

	"github.com/go-playground/validator/v10"
)

type indexFun func(index int) string

func SetErrorStr(err error, f func(tag string) string) indexFun {
	result := make([]string, 0)

	validError, ok := err.(validator.ValidationErrors)
	if !ok {
		fmt.Println(reflect.TypeOf(err))
		result = append(result, "internel error")
	} else {
		for _, item := range validError {
			result = append(result, f(item.StructField()+"."+item.Tag()))
		}
	}

	return func(index int) string {
		if index >= len(result) {
			return ""
		}

		return result[index]
	}
}
