import { createRoot } from "react-dom/client";
import App from "./App.tsx";
import "./index.css";

// Global interceptor: catch all external link clicks to bypass iframe security restrictions
// This prevents ERR_BLOCKED_BY_RESPONSE errors when the app runs inside preview iframes
document.addEventListener('click', (e) => {
  const anchor = (e.target as HTMLElement).closest('a[href]') as HTMLAnchorElement | null;
  if (!anchor) return;

  const href = anchor.getAttribute('href');
  if (!href) return;

  // Only intercept external links (http/https) that open in new tabs
  const isExternal = href.startsWith('http://') || href.startsWith('https://');
  const isNewTab = anchor.target === '_blank';

  if (isExternal && isNewTab) {
    e.preventDefault();
    e.stopPropagation();
    // Use window.open as the most reliable cross-context method
    const w = window.open(href, '_blank', 'noopener,noreferrer');
    if (!w) {
      // Fallback: programmatic anchor click
      const a = document.createElement('a');
      a.href = href;
      a.target = '_blank';
      a.rel = 'noopener noreferrer';
      document.body.appendChild(a);
      a.click();
      document.body.removeChild(a);
    }
  }
}, true); // Use capture phase to intercept before any other handlers

createRoot(document.getElementById("root")!).render(<App />);
