package queryparser

import (
	"regexp"
	"strconv"
	"strings"

	"github.com/gofiber/fiber/v3"
)

func ParseQuery(c fiber.Ctx) map[string]interface{} {
	queryParams := map[string]interface{}{}
	re := regexp.MustCompile(`\?(\w.+)$`)
	xx := re.FindAllStringSubmatch(c.Request().URI().String(), -1)

	if len(xx) > 0 {
		if len(xx[0]) == 2 {
			queryParts := strings.Split(xx[0][1], "&")
			for _, q := range queryParts {
				qq := strings.Split(q, "=")
				if len(qq) == 2 {
					if num, err := strconv.ParseInt(qq[1], 10, 64); err == nil {
						queryParams[qq[0]] = num
					} else if float, err := strconv.ParseFloat(qq[1], 64); err == nil {
						queryParams[qq[0]] = float
					} else {
						queryParams[qq[0]] = qq[1]
					}
				} else {
					queryParams[qq[0]] = true
				}
			}
		}
	}

	return queryParams
}
