package productTranslationService

import (
	"strings"
	"unicode"

	constdata "git.ma-al.com/goc_daniel/b2b/app/utils/const_data"
	"github.com/dlclark/regexp2"
	"golang.org/x/text/runes"
	"golang.org/x/text/transform"
	"golang.org/x/text/unicode/norm"
)

func IsValidSlug(s string) bool {
	var slug_regex2 = regexp2.MustCompile(constdata.SLUG_REGEX, regexp2.None)

	ok, _ := slug_regex2.MatchString(s)
	return ok
}

func SanitizeSlug(s string) string {
	s = strings.TrimSpace(strings.ToLower(s))

	// First apply explicit transliteration for language-specific letters.
	s = transliterateWithTable(s)

	// Then normalize and strip any remaining combining marks.
	s = removeDiacritics(s)

	// Replace all non-alphanumeric runs with "-"
	var non_alphanum_regex2 = regexp2.MustCompile(constdata.NON_ALNUM_REGEX, regexp2.None)
	s, _ = non_alphanum_regex2.Replace(s, "-", -1, -1)

	// Collapse repeated "-" and trim edges
	var multi_dash_regex2 = regexp2.MustCompile(constdata.MULTI_DASH_REGEX, regexp2.None)
	s, _ = multi_dash_regex2.Replace(s, "-", -1, -1)

	s = strings.Trim(s, "-")

	return s
}

func transliterateWithTable(s string) string {
	var b strings.Builder
	b.Grow(len(s))

	for _, r := range s {
		if repl, ok := constdata.TRANSLITERATION_TABLE[r]; ok {
			b.WriteString(repl)
		} else {
			b.WriteRune(r)
		}
	}

	return b.String()
}

func removeDiacritics(s string) string {
	t := transform.Chain(
		norm.NFD,
		runes.Remove(runes.In(unicode.Mn)),
		norm.NFC,
	)
	out, _, err := transform.String(t, s)
	if err != nil {
		return s
	}
	return out
}
