/**
 * Mementor TTS Notifications Handler
 *
 * Handles displaying notifications in the WordPress admin.
 * This is used by the plugin to show success/error messages.
 */

(function($) {
    'use strict';

    // When DOM is ready
    $(document).ready(function() {
        // Check if we have notification data from PHP
        if (typeof mementorTTSNotifications !== 'undefined') {
            // If settings were just saved, show a success notification
            if (mementorTTSNotifications.settingsSaved) {
                mementorTTSShowNotification(mementorTTSNotifications.settingsSavedMessage, 'success');
            }
            
            // If we have an admin notice from transient, show it
            if (mementorTTSNotifications.adminNotice) {
                mementorTTSShowNotification(
                    mementorTTSNotifications.adminNotice.message, 
                    mementorTTSNotifications.adminNotice.type
                );
            }
        }
    });

    /**
     * Show a notification to the user
     * 
     * @param {string} message - The message to display (can contain HTML)
     * @param {string} type - The notification type (success, error, warning, info)
     * @param {number} timeout - Timeout in ms before auto-dismissing, 0 for no auto-dismiss
     */
    window.mementorTTSShowNotification = function(message, type, timeout) {
        // Check if this is a third-party plugin notice and ignore it
        if (typeof message === 'string') {
            // List of patterns to ignore from other plugins
            const ignorePatterns = [
                'WP Rocket:',
                'wp-rocket',
                'Clear cache',
                'Dismiss this notice',
                'plugins have been enabled or disabled'
            ];
            
            // Check if message contains any ignored patterns
            for (let pattern of ignorePatterns) {
                if (message.toLowerCase().includes(pattern.toLowerCase())) {
                    // Don't show third-party plugin notices as modals
                    return;
                }
            }
        }
        
        // Default type to info if not provided
        type = type || 'info';
        
        // Default timeout to 5000ms (5 seconds) if not provided
        timeout = typeof timeout !== 'undefined' ? timeout : 5000;
        
        // Icon per type
        var icons = { success: '✓', error: '✕', warning: '!', info: 'i' };

        // Create notification elements
        var $icon = $('<span class="mementor-tts-notification-icon"></span>').text(icons[type] || icons.info);
        var $message = $('<div class="mementor-tts-notification-content"></div>').html(message);
        var $closeButton = $('<button class="mementor-tts-notification-close">×</button>');

        var $notification = $('<div class="mementor-tts-notification ' + type + '"></div>');
        $notification.append($icon).append($message).append($closeButton);
        $('body').append($notification);

        // Animate in
        setTimeout(function() {
            $notification.addClass('show');
        }, 10);

        // Auto dismiss after timeout, but only if timeout > 0
        if (timeout > 0) {
            setTimeout(function() {
                dismissNotification($notification);
            }, timeout);
        }

        // Close button functionality
        $closeButton.on('click', function() {
            dismissNotification($notification);
        });

        // Return the notification element in case caller wants to manipulate it
        return $notification;
    };
    
    /**
     * Helper function to dismiss a notification with animation
     * 
     * @param {jQuery} $notification - The notification element to dismiss
     */
    function dismissNotification($notification) {
        $notification.removeClass('show');

        // Remove from DOM after animation completes
        setTimeout(function() {
            $notification.remove();
        }, 400);
    }

})(jQuery); 