/**
 * @file wally-utils.js
 * @description Utility functions for Wally Monitor — escapeHtml, getImpactColor, showNotification.
 * @module WallyMonitor.Utils
 */
window.WallyMonitor = window.WallyMonitor || {};

(function($) {
    'use strict';

    WallyMonitor.Utils = WallyMonitor.Utils || {};

    /**
     * Escapa HTML para prevenir XSS
     * @param {string} str - Texto a ser escapado
     * @return {string} Texto escapado
     */
    WallyMonitor.Utils.escapeHtml = function(str) {
        if (typeof str !== 'string') {
            return str;
        }
        const map = {
            '&': '&amp;',
            '<': '&lt;',
            '>': '&gt;',
            '"': '&quot;',
            "'": '&#039;'
        };
        return str.replace(/[&<>"']/g, m => map[m]);
    };

    /**
     * Retorna a cor correspondente ao nível de impacto
     * @param {string} impact - Nível de impacto (critical, serious, moderate, minor)
     * @return {string} Cor hexadecimal
     */
    WallyMonitor.Utils.getImpactColor = function(impact) {
        const colors = { critical: '#e5484d', serious: '#e5793a', moderate: '#e5a537', minor: '#3b82f6' };
        return colors[impact] || '#5f6b7a';
    };

    /**
     * Mostra notificação temporária com design toast
     * @param {string} message - Mensagem a exibir
     * @param {string} type - Tipo: success, warning, error, info
     */
    WallyMonitor.Utils.showNotification = function(message, type) {
        type = type || 'info';
        const icons = {
            success: '✓',
            warning: '⚠',
            error: '✗',
            info: 'ℹ'
        };

        // Remove notificação anterior se existir
        $('.wally-toast').remove();

        const $toast = $('<div>')
            .attr('role', 'alert')
            .attr('aria-live', 'assertive')
            .addClass('wally-toast wally-toast-' + type)
            .append(
                $('<span>').addClass('wally-toast-icon').text(icons[type] || icons.info),
                $('<span>').text(' ' + message)
            )
            .appendTo('body');

        setTimeout(function() {
            $toast.addClass('wally-toast-out');
            setTimeout(function() {
                $toast.remove();
            }, 350);
        }, 3500);
    };

})(jQuery);
