// Astrail — App Splash Screen
// Uses LoaderPulse (lg) — spec: "waiting for external event that cannot be predicted"
// Connecting to blockchain networks fits this context precisely.
// No glow, no blur, no custom animations outside the loader system.

const NvxLoader = ({ onDone }) => {
  const [alive, setAlive]       = React.useState(true);
  const [entering, setEntering] = React.useState(false);
  const [exiting, setExiting]   = React.useState(false);
  const [msgIdx, setMsgIdx]     = React.useState(0);

  const MESSAGES = [
    'Connecting wallets…',
    'Syncing treasury…',
    'Ready',
  ];

  React.useEffect(() => {
    const T = (fn, ms) => setTimeout(fn, ms);
    const timers = [
      T(() => setEntering(true), 50),
      T(() => setMsgIdx(1), 1400),
      T(() => setMsgIdx(2), 2800),
      T(() => setExiting(true), 3500),
      T(() => { setAlive(false); onDone && onDone(); }, 3950),
    ];
    return () => timers.forEach(id => clearTimeout(id));
  }, []);

  if (!alive) return null;

  return (
    <div style={{
      position: 'fixed', inset: 0, zIndex: 9999,
      display: 'flex', flexDirection: 'column',
      alignItems: 'center', justifyContent: 'center',
      background: 'var(--col-bg, #F9F8F6)',
      gap: 32,
      opacity:   exiting ? 0 : entering ? 1 : 0,
      transform: exiting ? 'scale(1.015)' : entering ? 'scale(1)' : 'scale(0.98)',
      transition: 'opacity 440ms ease, transform 460ms ease',
    }}>
      {/* LoaderPulse lg — rings expand outward, core dot stays */}
      <LoaderPulse size="lg" />

      {/* Wordmark + status */}
      <div style={{
        display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 10,
        opacity:    entering ? 1 : 0,
        transition: 'opacity 500ms ease 280ms',
      }}>
        <div style={{
          fontFamily:    'Onest, sans-serif',
          fontSize:       11,
          fontWeight:     600,
          color:          'var(--col-text-3, #8A8480)',
          letterSpacing:  '0.17em',
          textTransform:  'uppercase',
        }}>
          Astrail
        </div>

        <div key={msgIdx} style={{
          fontFamily: 'Onest, sans-serif',
          fontSize:    13,
          fontWeight:  400,
          color:       'var(--col-text-3, #8A8480)',
          animation:   'nvxSplashFadeUp 320ms ease',
          textAlign:   'center',
        }}>
          {MESSAGES[msgIdx]}
        </div>
      </div>

      <style>{`
        @keyframes nvxSplashFadeUp {
          from { opacity: 0; transform: translateY(4px); }
          to   { opacity: 1; transform: none; }
        }
      `}</style>
    </div>
  );
};

Object.assign(window, { NvxLoader });
