(function () {
    const toastEl = document.getElementById('appToast');
    const bodyEl = document.getElementById('appToastBody');

    const variants = {
        success: 'text-bg-success',
        error: 'text-bg-danger',
        danger: 'text-bg-danger',
        warning: 'text-bg-warning',
        info: 'text-bg-info',
        dark: 'text-bg-dark'
    };

    function setVariant(el, variant) {
        Object.values(variants).forEach(v => el.classList.remove(v));
        el.classList.add(variants[variant] || variants.dark);
    }

    window.showToast = function ({ text = 'Gotowe.', variant = 'dark' } = {}) {
        if (bodyEl) bodyEl.textContent = text;
        setVariant(toastEl, variant);
        const t = new bootstrap.Toast(toastEl);
        t.show();
    };

    const flashNode = document.getElementById('flash-data');
    if (flashNode) {
        try {
            const flashes = JSON.parse(flashNode.dataset.flashes || '[]');
            flashes.forEach(([category, message]) => {
                window.showToast({ text: message, variant: category });
            });
        } catch (_) { }
    }
})();