package functions

import (
	"regexp"
	"strings"
)

func MapRoom(room string, output bool) string {
	// remove dots from room string

	if output {
		//replace second point in TR_A1.23.1 -> TR_A1.23-1 with a minus
		re := regexp.MustCompile(`\b[TR_]+[A-ZÄÖÜ]?[0-9]{1,4}[.][0-9]{1,4}[.]\b`)
		room = re.ReplaceAllStringFunc(room, func(match string) string {
			return match[:len(match)-1] + "-" + match[len(match)-1:]
		})
		// If the output flag is set, remove all dots from the room string
		room = strings.ReplaceAll(room, ".", "")
	} else {
		// If the output flag is false add a dot for all rooms with regexp TR_A123 -> TR_A1.23
		re := regexp.MustCompile(`\bTR_+[A-ZÄÖÜ]?[0-9]{1,4}[a-zäöü]?\b`)
		room = re.ReplaceAllStringFunc(room, func(match string) string {
			return match[:len(match)-2] + "." + match[len(match)-2:]
		})
		room = strings.ReplaceAll(room, "-", ".")
	}

	// Regular expression pattern to match room identifiers
	// The pattern looks for strings that start with two uppercase letters, optionally followed by an underscore,
	// followed by 1 to 3 digits, and ending with "-[A-Z]"
	re := regexp.MustCompile(`\b[A-ZÄÖÜ]{2}([_]+[A-ZÄÖÜ])?[0-9]{1,4}[a-zäöü]?[-]?[0-9]?-[A-ZÄÖÜ]\b`)

	// Use the ReplaceAllStringFunc to process each match
	room = re.ReplaceAllStringFunc(room, func(match string) string {
		// Remove the last two characters (i.e., "-<letter>")
		return match[:len(match)-2]
	})

	return room
}
