// priority: 0
// Visit the wiki for more info - https://kubejs.com/
console.info('Hello, World! (Loaded AUTO FOOD TOOLTIPS.js)')


// Adds tooltip lines for edited food buffs based on global.mb_foodEffectDurationTweaks from startup script.
// Disable Farmer's Delight's own effect tooltip config, then this becomes the only display.

function formatMMSS(totalSeconds) {
    const s = Math.max(0, Math.floor(totalSeconds || 0));
    const mm = Math.floor(s / 60);
    const ss = s % 60;
    return String(mm).padStart(2, '0') + ':' + String(ss).padStart(2, '0');
}

// "namespace:path" -> "effect.namespace.path" (minecraft-style translation key)
function effectTranslateKey(effectId) {
    const parts = (effectId || '').split(':');
    const ns = parts[0] || 'minecraft';
    const path = parts[1] || effectId;
    return `effect.${ns}.${path}`;
}

// Optional: roman numerals for amplifier display (I, II, III...)
function romanNumeral(n) {
    const romans = ['I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'];
    return romans[n - 1] || String(n);
}

ItemEvents.tooltip(event => {
    const tweaks = global.mb_foodEffectDurationTweaks;

    // If this is undefined, check the browser/launcher console/log first
    if (!tweaks) {
        // Uncomment if you want a log line:
        // console.log('[MB] global.mb_foodEffectDurationTweaks was undefined in client tooltips!');
        return;
    }

    Object.entries(tweaks).forEach(([itemId, effects]) => {
        const lines = [];

        effects.forEach(e => {
            const time = formatMMSS(e.duration);

            // Build: "Comfort 15:00." (your exact style), but localized effect name.
            // If amplifier > 0, show "II", "III", etc.
            const amp = (e.amplifier ?? 0);
            const ampText = amp > 0 ? ` ${romanNumeral(amp + 1)}` : '';

            // This is a Text component, but event.add also accepts plain strings.
            // Keeping it as Text lets the effect name localize properly.
            lines.push(
                Text.blue(Text.translate(effectTranslateKey(e.effect)).append(`${ampText} (${time})`))
            );
        });

        // Add all lines at once for this item
        event.add(itemId, lines);
    });
});


// fin //