
export const renderAsCurrency = (value: string | number,currencyString = "€") => {
    const formattedValue = Number(value).toLocaleString("de-DE", {
        minimumFractionDigits: 2,
        maximumFractionDigits: 2
    })

    return `${formattedValue} ${currencyString}`
}

export const splitStringBySpace = (input:string,maxSplitLength:number,removeLinebreaks = false) =>  {

    if(removeLinebreaks) {
        input = input.replaceAll("\n","")
    }

    let splitStrings: string[] = []

    input.split("\n").forEach(string => {
        splitStrings.push(string)
    })

    let returnSplitStrings: string[] = []

    splitStrings.forEach(string => {
        let regex = / /gi, result, indices = [];
        while ( (result = regex.exec(string)) ) {
            indices.push(result.index);
        }

        let lastIndex = 0

        if(string.length > maxSplitLength) {
            let tempStrings = []

            for (let i = maxSplitLength; i < string.length; i = i + maxSplitLength) {
                let nearestIndex = indices.length > 0 ? indices.reduce(function(prev, curr) {
                    return (Math.abs(curr - i) < Math.abs(prev - i) ? curr : prev);
                }) : i

                tempStrings.push(string.substring(lastIndex,nearestIndex))

                lastIndex = indices.length > 0 ? nearestIndex + 1 : nearestIndex
            }

            tempStrings.push(string.substring(lastIndex,input.length))

            returnSplitStrings.push(...tempStrings)

        } else {
            returnSplitStrings.push(string)
        }
    })

    return returnSplitStrings
}
