/**
 * Client-side error overlay for Vite build errors that occur before the React app loads.
 * This script runs early in the page lifecycle to catch errors when the main app cannot render.
 * Uses React to render the same BuildErrorModal component used by the main app.
 */
export function getErrorOverlayScript() {
    return /*js*/ `
    // Only run in dev mode with HMR enabled
    if (import.meta.hot) {
      // Track if the React app has successfully loaded
      let reactAppLoaded = false;
      let reactRoot = null;
      let containerElement = null;
      let currentError = null;

      // Mark that React app has loaded (called from React)
      window.__SB_MARK_APP_LOADED = function() {
        reactAppLoaded = true;
        if (containerElement) {
          hideOverlay();
        }
      };

      // Listen for Vite error events
      import.meta.hot.on('vite:error', (payload) => {
        if (reactAppLoaded) return;
        showBuildErrorOverlay(payload);
      });

      async function showBuildErrorOverlay(error) {
        // Don't show if React app is already handling errors
        if (reactAppLoaded) return;

        // Parse error data
        const errorData = error?.err || error;
        const errorMessage = errorData?.message || String(errorData);
        const errorStack = errorData?.stack || '';

        // Parse error type
        let buildError;

        // Check for import errors
        if (errorData?.name === 'ImportError' || errorData?.importPath) {
          buildError = {
            errorType: 'import',
            error: errorMessage,
            stack: errorStack,
            importPath: errorData?.importPath || 'unknown',
          };
        } else if (errorMessage.includes('Failed to resolve import')) {
          // Fallback for non-custom import errors
          const importMatch = errorMessage.match(/Failed to resolve import ["'](.+?)["']/);
          buildError = {
            errorType: 'import',
            error: errorMessage,
            stack: errorStack,
            importPath: importMatch ? importMatch[1] : 'unknown',
          };
        } else {
          buildError = {
            errorType: 'general',
            error: errorMessage,
            stack: errorStack,
          };
        }

        currentError = buildError;

        // Create or update overlay
        if (!containerElement) {
          await createOverlay();
        } else {
          renderOverlay();
        }
      }

      async function createOverlay() {
        // Create container for React root
        containerElement = document.createElement('div');
        containerElement.id = 'sb-error-overlay-root';
        document.body.appendChild(containerElement);

        // Dynamically import React, ReactDOM, and BuildErrorModal
        try {
          const [{ default: React, createElement }, { createRoot }, { BuildErrorModal }] = await Promise.all([
            import('react'),
            import('react-dom/client'),
            import('@superblocksteam/library')
          ]);

          // Create React root and render
          reactRoot = createRoot(containerElement);
          renderOverlay(React, createElement, BuildErrorModal);
        } catch (err) {
          // Fallback: show a simple error message
          containerElement.innerHTML = '<div style="position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); background: white; padding: 20px; border-radius: 8px; box-shadow: 0 4px 6px rgba(0,0,0,0.1); z-index: 10000;">Build error occurred. Please check the console.</div>';
        }
      }

      function renderOverlay(React, createElement, BuildErrorModal) {
        if (!reactRoot || !currentError) return;

        reactRoot.render(createElement(BuildErrorModal, {
          isOpen: true,
          error: currentError,
          onClose: hideOverlay
        }));
      }

      function hideOverlay() {
        if (reactRoot) {
          reactRoot.unmount();
          reactRoot = null;
        }
        if (containerElement) {
          containerElement.remove();
          containerElement = null;
        }
        currentError = null;
      }
    }
  `;
}
//# sourceMappingURL=client-error-overlay.js.map