/**
 * Mementor Text to Speech - Debug Settings Script
 *
 * This file is always loaded and sets up the debug flag based on WordPress settings.
 * It ensures the debug flag is available to all other scripts, even if they load
 * before the main player script or if script localization fails.
 */
(function() {
    'use strict';
    
    // Set the global debug flag
    // First check for URL parameter (highest priority)
    if (window.location.href.indexOf('mementor_tts_debug=1') !== -1) {
        window.mementorTTSDebugEnabled = true;
        console.log('Mementor TTS: Debug mode enabled via URL parameter');
    } 
    // Then check for WordPress settings passed via localized script
    else if (typeof mementorTTSDebugSettings !== 'undefined' && 
             (mementorTTSDebugSettings.isDebug === true || 
              mementorTTSDebugSettings.isDebug === '1' || 
              mementorTTSDebugSettings.isDebug === 1)) {
        window.mementorTTSDebugEnabled = true;
        console.log('Mementor TTS: Debug mode enabled via WordPress settings');
    }
    // Add backwards compatibility for old debug check
    else if (typeof mementorTTSPlayer !== 'undefined' && 
             (mementorTTSPlayer.isDebug === true || 
              mementorTTSPlayer.isDebug === '1' || 
              mementorTTSPlayer.isDebug === 1)) {
        window.mementorTTSDebugEnabled = true;
        console.log('Mementor TTS: Debug mode enabled via player settings');
    }
    else {
        window.mementorTTSDebugEnabled = false;
    }
    
    // Create a custom debug logging function that only logs when debug mode is enabled
    window.mementorTTSDebug = function() {
        if (window.mementorTTSDebugEnabled === true) {
            console.log.apply(console, arguments);
        }
    };
    
    // Log initial debug status if enabled
    if (window.mementorTTSDebugEnabled) {
        console.log('Mementor TTS: Debug logging is active');

        // Add visibility to the settings objects to help with troubleshooting
        console.log('Debug Settings:', typeof mementorTTSDebugSettings !== 'undefined' ? mementorTTSDebugSettings : 'Not found');
        if (typeof mementorTTSPlayer !== 'undefined') {
            console.log('Player Settings:', mementorTTSPlayer);
        } else {
            console.log('Mementor TTS: Player script not loaded on this page. Expected if the post has no generated audio, the post type is not enabled in plugin settings, or the [mementor-tts] shortcode is not present in the content.');
        }
    }
})();