/**
 * Main application entry point for Vite
 * Imports all vendor dependencies and makes them available globally
 */

// Import CSS first
import '@fortawesome/fontawesome-free/css/all.css';
import 'bootstrap/dist/css/bootstrap.min.css';
import 'bootstrap-icons/font/bootstrap-icons.css';
import 'highlight.js/styles/github-dark.css';
import 'katex/dist/katex.min.css';
import '../css/styles.css';

// Import and setup core libraries
import { marked } from 'marked';
import io from 'socket.io-client';
import hljs from 'highlight.js';
import jsPDF from 'jspdf';
import html2canvas from 'html2canvas';
import DOMPurify from 'dompurify';
import markedKatex from 'marked-katex-extension';

// Import Bootstrap JavaScript
import * as bootstrap from 'bootstrap';

// Chart.js and plugins (imported but not attached to window by default)
import Chart from 'chart.js/auto';
import 'chartjs-adapter-date-fns';
import annotationPlugin from 'chartjs-plugin-annotation';

// Register Chart.js plugin
Chart.register(annotationPlugin);

// Make libraries available globally for existing code compatibility
window.marked = marked;
window.io = io;
window.Chart = Chart;
window.hljs = hljs;
window.jsPDF = jsPDF;
window.html2canvas = html2canvas;
window.bootstrap = bootstrap;
window.Chart = Chart;
window.DOMPurify = DOMPurify;

// Configure marked
marked.setOptions({
  headerIds: false,
  mangle: false,
  smartypants: false,
  highlight(code, lang) {
    if (lang && hljs.getLanguage(lang)) {
      try {
        return hljs.highlight(code, { language: lang }).value;
      } catch (err) {
        SafeLogger.error('Highlight error:', err);
      }
    }
    return code;
  }
});

marked.use(markedKatex({
  throwOnError: false,
  // Let the project's CSS variable (--error-color) govern error styling.
  // KaTeX otherwise sets inline `style="color:#cc0000"` which would override
  // any selector-based rule (and fail dark-theme WCAG AA contrast).
  errorColor: 'currentColor',
}));

// Initialize highlight.js when DOM is ready
document.addEventListener('DOMContentLoaded', () => {
  // Highlight all code blocks
  document.querySelectorAll('pre code').forEach((block) => {
    hljs.highlightElement(block);
  });

  // Initialize Bootstrap tooltips and popovers if present
  const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
  [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));

  const popoverTriggerList = document.querySelectorAll('[data-bs-toggle="popover"]');
  [...popoverTriggerList].map(popoverTriggerEl => new bootstrap.Popover(popoverTriggerEl));
});

// Log successful initialization
SafeLogger.log('✨ LDR App initialized with Vite');

// Export for potential module usage
export {
  marked,
  io,
  hljs,
  jsPDF,
  html2canvas,
  bootstrap,
  Chart,
  DOMPurify
};
