function currency(st) {

    // schema for Mittelreich: 1 Ducat = 10 Silver = 100 Kreutzer = 1000 Heller
    // internally the price is always given in Silver
    // so we need to inflate the value of price by 100 to be able to divide beginning from Heller

    const baseValue = Math.round(st * 100)

    // then we can regex over it

    const currencyRegexp = /(.*)(.)(.)(.)/g
    const withDucats = currencyRegexp.exec(baseValue)
    let _ = undefined
    let ducats = 0
    let silver = 0
    let kreutzer = 0
    let heller = 0

    if (withDucats) {
        [_, ducats, silver, kreutzer, heller] = withDucats
    } else {
        const currencyRegexp = /(.)(.)(.)/g
        const withSilver = currencyRegexp.exec(baseValue)
        if (withSilver) {
            [_, silver, kreutzer, heller] = withSilver
        } else {
            const currencyRegexp = /(.)(.)/g
            const withKreutzer = currencyRegexp.exec(baseValue)

            if (withKreutzer) {
                [_, kreutzer, heller] = withKreutzer

            } else {
                heller = baseValue
            }
        }
    }

    return {
        ducats,
        silver,
        kreutzer,
        heller
    }
}

function registerHelper(hbs) {
    hbs?.registerHelper('currency', (data) => {

        const {ducats, silver, kreutzer, heller} = currency(data)


        let str = `<span class='coins' data-tooltip="${ducats > 0 ? ducats + ' Dukaten ' : ''}${silver > 0 ? silver + ' Silbertaler ' : ''}${kreutzer > 0 ? kreutzer + ' Kreuzer ' : ''}${heller > 0 ? heller + ' Heller' : ''}">`
        if (ducats > 0) {
            str += ducats + "<i class='symbol ducat'></i>"
        }
        if (silver > 0) {
            str += silver + "<i class='symbol silver'></i>"
        }
        if (kreutzer > 0) {
            str += kreutzer + "<i class='symbol kreutzer'></i>"
        }
        if (heller > 0) {
            str += heller + "<i class='symbol heller'></i>"
        }
        str += "</span>"

        return new Handlebars.SafeString(str)
    })
}

export {
    currency,
    registerHelper
}