/**
 * Darlink ERP Phoenix P2.5.4 LTS
 * Gestion uniforme des notifications utilisateur.
 */
(function () {
    'use strict';

    const durations = {
        success: 3000,
        info: 4000,
        warning: 6000,
        error: 0
    };

    function closeNotification(notification) {
        if (!notification || notification.dataset.closing === '1') {
            return;
        }

        notification.dataset.closing = '1';
        notification.classList.add('darlink-notification-closing');

        window.setTimeout(function () {
            notification.remove();
        }, 300);
    }

    function initialiseNotification(notification) {
        const type = notification.dataset.notificationType || 'info';
        const duration = durations[type] ?? durations.info;
        const closeButton = notification.querySelector('[data-notification-close]');

        if (closeButton) {
            closeButton.addEventListener('click', function () {
                closeNotification(notification);
            });
        }

        // Les erreurs restent visibles jusqu'à fermeture manuelle.
        if (duration <= 0) {
            return;
        }

        const progress = notification.querySelector('.darlink-notification-progress');
        if (progress) {
            progress.style.setProperty('--darlink-notification-duration', duration + 'ms');
            progress.classList.add('darlink-notification-progress-running');
        }

        window.setTimeout(function () {
            closeNotification(notification);
        }, duration);
    }

    function initialiseAll() {
        document.querySelectorAll('[data-darlink-notification]').forEach(initialiseNotification);
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', initialiseAll, { once: true });
    } else {
        initialiseAll();
    }
})();
