package querystring

import (
	"fmt"
	"strconv"
	"strings"
)

func SplitIntArray(value string) ([]int64, error) {
	items := strings.Split(value, ",")
	result := make([]int64, len(items))

	for i, item := range items {
		val, err := strconv.ParseInt(item, 10, 64)
		if err != nil || val < 1 {
			return result, fmt.Errorf("invalid id %s", item)
		}
		result[i] = val
	}

	return result, nil
}

func ConvertStringToInt(input string) (int, error) {
	// Try converting the string to float
	floatValue, err := strconv.ParseFloat(input, 64)
	if err == nil {
		// Float conversion successful, return the integer part
		return int(floatValue), nil
	}

	// Float conversion failed, try converting the string to integer
	intValue, err := strconv.Atoi(input)
	if err == nil {
		// Integer conversion successful, return the integer value
		return intValue, nil
	}

	// Conversion failed
	return 0, err
}
