/**
 * ProRank SEO Admin Entry Point
 *
 * This file is the main entry point for the admin JavaScript.
 */

// Initialize WordPress URL helpers immediately
import './utils/urls';
import setupApiFetch from './utils/setupApiFetch';

// WordPress URL helpers must be initialized first
// Global styles are imported in App.jsx

// WordPress dependencies
import domReady from '@wordpress/dom-ready';
import { createRoot } from '@wordpress/element';

// Import the App wrapper for lazy loading
import AppWrapper from './AppWrapper';

// Import media enhancements
import './media';

// Configure apiFetch early so child effects always have nonce middleware.
setupApiFetch();

// Initialize when DOM is ready
domReady(() => {
  // Find admin container - use ID if available, otherwise use class
  // This prevents duplicate initialization when an element has both class and ID
  let container = document.getElementById('prorank-admin-root');

  if (!container) {
    container = document.querySelector('.prorank-admin-container');
  }

  if (container) {
    // Check if root already exists to prevent duplicate creation
    if (!container._reactRoot) {
      const root = createRoot(container);
      root.render(<AppWrapper />);
      // Mark container to prevent duplicate initialization
      container._reactRoot = root;
    }
  }
});
