﻿// Fonction pour vérifier si le debug est activé
function isDebugEnabled(): boolean {
  // Debug activé si explicitement forcé
  if (window.location?.search?.includes('debug=force')) {
    return true;
  }

  // Vérifier les paramètres de debug centralisés
  if (typeof window.pdfBuilderDebugSettings !== 'undefined' &&
      window.pdfBuilderDebugSettings &&
      typeof window.pdfBuilderDebugSettings === 'object') {
    return !!window.pdfBuilderDebugSettings.javascript;
  }

  // Fallback vers pdfBuilderCanvasSettings pour la compatibilité
  if (typeof window.pdfBuilderCanvasSettings !== 'undefined' &&
      window.pdfBuilderCanvasSettings &&
      typeof window.pdfBuilderCanvasSettings === 'object') {
    const debugSettings = (window.pdfBuilderCanvasSettings as any).debug;
    if (debugSettings && typeof debugSettings === 'object') {
      return !!debugSettings.javascript;
    }
  }

  return false;
}

// Extension de Window pour le debug
declare global {
  interface Window {
    PDFIB_VERBOSE?: boolean; // Set to true/false to control debug logging
    PDFIB_DEBUG_SAVE?: boolean; // Set to true to debug save operations
    pdfBuilderDebugSettings?: {
      javascript?: boolean;
      ajax?: boolean;
      performance?: boolean;
      settings_page?: boolean;
      pdf_editor?: boolean;
      javascript_verbose?: boolean;
    };
    pdfBuilderData?: {
      nonce?: string;
      ajaxUrl?: string;
      templateId?: string | number;
      isEditing?: boolean;
      auto_save_interval?: number;
      existingTemplate?: any;
      hasExistingData?: boolean;
      canvasSettings?: any;
      previewOrderData?: any;
      license?: {
        isPremium?: boolean;
        [key: string]: unknown;
      };
    };
    pdfBuilderCanvasSettings?: any; // Canvas settings from WordPress
  }
}

// Fonction de logging conditionnel
export function debugLog(...args: unknown[]) {
  if (isDebugEnabled()) {
    console.log(...args);
  }
}

// Fonction de debug pour les sauvegardes (activable séparément)
export function debugSave(...args: unknown[]) {
  if (isDebugEnabled()) {
    console.log('[SAVE DEBUG]', ...args);
  }
}

export function debugError(...args: unknown[]) {
  if (isDebugEnabled()) {
    console.error(...args);
  }
}

export function debugWarn(...args: unknown[]) {
  if (isDebugEnabled()) {
    console.warn(...args);
  }
}

// Keep an internal verbose flag in sync with window.pdfBuilderDebugSettings
if (typeof window !== 'undefined') {
  try {
    window.PDFIB_VERBOSE = !!(window.pdfBuilderDebugSettings && window.pdfBuilderDebugSettings.javascript);
  } catch (_e) {
    // ignore
  }
  // Listener to update verbose flag at runtime
  if (window.addEventListener) {
    window.addEventListener('pdfBuilder:debugSettingsChanged', (e: any) => {
      try {
        const detail = e && e.detail ? e.detail : window.pdfBuilderDebugSettings;
        window.PDFIB_VERBOSE = !!(detail && detail.javascript);
        if (typeof window.console !== 'undefined' && window.PDFIB_VERBOSE) {
          
        }
      } catch (_err) {
        if (typeof window.console !== 'undefined') {
          
        }
      }
    });
  }
}



