function formatEur(value) {
    return value.toFixed(2).replace('.', ',') + ' \u20AC'
}

export function initCommissionCalculator() {
    const calculator = document.getElementById('commission-calculator')
    if (!calculator) return

    const priceInput = document.getElementById('billet_price')
    if (!priceInput) return

    const eticketRate = Number.parseFloat(calculator.dataset.eticketRate) || 0
    const stripeRate = Number.parseFloat(calculator.dataset.stripeRate) || 1.5
    const stripeFixed = Number.parseFloat(calculator.dataset.stripeFixed) || 0.25

    const calcPrice = document.getElementById('calc-price')
    const calcEticket = document.getElementById('calc-eticket')
    const calcStripe = document.getElementById('calc-stripe')
    const calcTotal = document.getElementById('calc-total')
    const calcNet = document.getElementById('calc-net')

    function update() {
        const price = Number.parseFloat(priceInput.value) || 0

        const eticketFee = price * (eticketRate / 100)
        const stripeFee = price > 0 ? price * (stripeRate / 100) + stripeFixed : 0
        const totalFee = eticketFee + stripeFee
        const net = price - totalFee

        calcPrice.textContent = formatEur(price)
        calcEticket.textContent = '- ' + formatEur(eticketFee)
        calcStripe.textContent = '- ' + formatEur(stripeFee)
        calcTotal.textContent = '- ' + formatEur(totalFee)
        calcNet.textContent = formatEur(Math.max(0, net))
    }

    priceInput.addEventListener('input', update)
    update()
}
