package com.laseroptek.raman.utils.ext

import java.util.regex.Pattern

fun String.formatWithDots(): String {
    if (this.isEmpty()) {
        return this
    }

    // Regex to capture the initial numeric part and the subsequent alphabetic/symbol part
    // (\d+) captures one or more digits at the beginning.
    // ([a-zA-Z_].*)? captures an optional group starting with a letter or underscore, followed by anything.
    val pattern = Pattern.compile("^(\\d+)([a-zA-Z_].*)?$")
    val matcher = pattern.matcher(this)

    if (matcher.matches()) {
        val numericPart = matcher.group(1) ?: "" // Should always exist if matcher.matches() is true
        val alphaPart = matcher.group(2) ?: ""   // Optional part

        // Insert dots between digits
        val formattedNumericPart = numericPart.toCharArray().joinToString(".")

        return when {
            formattedNumericPart.isNotEmpty() && alphaPart.isNotEmpty() -> "$formattedNumericPart.$alphaPart"
            formattedNumericPart.isNotEmpty() -> formattedNumericPart
            else -> this // Should not happen if numericPart was matched, but as a fallback
        }
    } else {
        // String does not start with a digit as per the pattern, or is only alphabetic
        return this
    }
}
