import React, { Suspense, Component, ReactNode } from 'react';
import ReactDOM from 'react-dom/client';

const App = React.lazy(() => import('./App'));

const LoadingSkeleton = () => (
  <div className="loading-skeleton">
    <div className="shimmer-wrapper">
      <div className="shimmer-box"></div>
      <div className="shimmer-box"></div>
      <div className="shimmer-box"></div>
    </div>
  </div>
);

class ErrorBoundary extends Component<
  { children: ReactNode },
  { hasError: boolean; error?: Error }
> {
  constructor(props: { children: ReactNode }) {
    super(props);
    this.state = { hasError: false };
  }

  static getDerivedStateFromError(error: Error) {
    return { hasError: true, error };
  }

  render() {
    if (this.state.hasError) {
      return (
        <div className="error-boundary">
          <h2>Something went wrong</h2>
          <p>{this.state.error?.message}</p>
        </div>
      );
    }
    return this.props.children;
  }
}

const root = ReactDOM.createRoot(document.getElementById('root')!);
root.render(
  <React.StrictMode>
    <ErrorBoundary>
      <Suspense fallback={<LoadingSkeleton />}>
        <App />
      </Suspense>
    </ErrorBoundary>
  </React.StrictMode>
);
