/**
 * Mensajes flash de sesión (success, error, …) como SweetAlert2.
 * El layout define window.__rtDashboardFlash y window.__rtDashboardFlashI18n.
 */
(function () {
    'use strict';

    document.addEventListener('DOMContentLoaded', function () {
        if (typeof Swal === 'undefined' || !window.__rtDashboardFlash) {
            return;
        }

        var f = window.__rtDashboardFlash;
        var i18n = window.__rtDashboardFlashI18n || {};
        var accept = i18n.accept || 'OK';
        var understood = i18n.understood || 'OK';

        var priority = ['error', 'warning', 'success', 'info', 'status'];
        var key = priority.find(function (k) {
            return Object.prototype.hasOwnProperty.call(f, k) && f[k];
        });

        if (!key) {
            return;
        }

        var msg = String(f[key]);
        var icon =
            key === 'error'
                ? 'error'
                : key === 'warning'
                  ? 'warning'
                  : key === 'info'
                    ? 'info'
                    : 'success';

        var confirmBtn = key === 'error' ? understood : accept;

        Swal.fire({
            icon: icon,
            title: msg,
            confirmButtonText: confirmBtn,
            confirmButtonColor: '#b91830',
            cancelButtonColor: '#6c757d',
            buttonsStyling: true,
            customClass: {
                popup: 'rt-dashboard-flash-swal rounded-lg text-start',
                confirmButton: 'px-4 font-weight-bold',
            },
            width: 'min(28rem, 92vw)',
        });
    });
})();
