/* Reco shared UI primitives. Exports: RecoButton, Confetti, GMapsGlyph */

// ── Primary / secondary / ghost button matching IndieVision craft, Reco green
function RecoButton({ children, variant = 'primary', icon, full = true, size = 'lg', onClick, disabled, style = {} }) {
  const base = {
    display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
    gap: 'var(--iv-space-2)', width: full ? '100%' : 'auto',
    height: size === 'lg' ? 54 : 44, padding: '0 var(--iv-space-5)',
    borderRadius: 'var(--reco-radius-btn, var(--iv-radius-m))',
    border: '1px solid transparent', cursor: 'pointer',
    fontFamily: 'var(--iv-font-sans)', fontWeight: 600,
    fontSize: size === 'lg' ? 17 : 15, letterSpacing: '-0.01em',
    transition: 'background var(--iv-dur-fast) var(--iv-ease-out), border-color var(--iv-dur-fast) var(--iv-ease-out), transform var(--iv-dur-micro) var(--iv-ease-out)',
    whiteSpace: 'nowrap', boxSizing: 'border-box',
  };
  const variants = {
    primary: { background: 'var(--reco-green)', color: '#fff', boxShadow: '0 8px 20px rgba(22,169,116,0.28)' },
    dark: { background: 'var(--reco-navy)', color: '#fff' },
    secondary: { background: 'var(--iv-paper)', color: 'var(--iv-ink)', borderColor: 'var(--iv-line-strong)' },
    ghost: { background: 'transparent', color: 'var(--reco-green-deep)' },
  };
  return (
    <button className="reco-btn" disabled={disabled} onClick={onClick}
      style={{ ...base, ...variants[variant], ...(disabled ? { opacity: 0.6, cursor: 'not-allowed' } : {}), ...style }}>
      {icon}{children}
    </button>
  );
}

// ── "המשך עם Google": the same button as the owner app's sign-up (onboard.html .jr-btn-secondary)
function GoogleButton({ onClick, disabled, style = {} }) {
  return (
    <button className="reco-btn" disabled={disabled} onClick={onClick}
      style={{ width: '100%', height: 56, borderRadius: 16, border: '1.5px solid var(--iv-line-strong)', background: '#fff', color: 'var(--reco-navy)',
               display: 'inline-flex', alignItems: 'center', justifyContent: 'center', gap: 10, cursor: disabled ? 'not-allowed' : 'pointer',
               fontFamily: 'var(--iv-font-sans)', fontSize: 17, fontWeight: 600, opacity: disabled ? 0.5 : 1, boxSizing: 'border-box', ...style }}>
      <GMapsGlyph size={20} /> המשך עם Google
    </button>
  );
}

// ── Google "G" glyph
function GMapsGlyph({ size = 20 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" style={{ flexShrink: 0 }}>
      <path fill="#4285F4" d="M45.12 24.5c0-1.56-.14-3.06-.4-4.5H24v8.51h11.84c-.51 2.75-2.06 5.08-4.39 6.64v5.52h7.11c4.16-3.83 6.56-9.47 6.56-16.17z"/>
      <path fill="#34A853" d="M24 46c5.94 0 10.92-1.97 14.56-5.33l-7.11-5.52c-1.97 1.32-4.49 2.1-7.45 2.1-5.73 0-10.58-3.87-12.31-9.07H4.34v5.7C7.96 41.07 15.4 46 24 46z"/>
      <path fill="#FBBC05" d="M11.69 28.18C11.25 26.86 11 25.45 11 24s.25-2.86.69-4.18v-5.7H4.34C2.85 17.09 2 20.45 2 24s.85 6.91 2.34 9.88l7.35-5.7z"/>
      <path fill="#EA4335" d="M24 10.75c3.23 0 6.13 1.11 8.41 3.29l6.31-6.31C34.91 4.18 29.93 2 24 2 15.4 2 7.96 6.93 4.34 14.12l7.35 5.7c1.73-5.2 6.58-9.07 12.31-9.07z"/>
    </svg>
  );
}

// ── Confetti — looping celebratory burst. intensity: subtle|playful|big
function Confetti({ intensity = 'playful', seed = 1, soft = false }) {
  const counts = { subtle: 14, playful: 34, big: 64 };
  const n = soft ? 16 : (counts[intensity] || 34);
  const colors = soft ? ['var(--reco-green-light)', '#BFE9D8', '#FFE08A', '#FFC2CB'] : ['var(--reco-green)', 'var(--reco-green-light)', 'var(--reco-navy)', '#FFC93C', '#FF7A8A'];
  const pieces = React.useMemo(() => Array.from({ length: n }, (_, i) => {
    const r = (a, b) => a + ((Math.sin(seed * 99 + i * 12.9898) * 43758.5453) % 1 + 1) % 1 * (b - a);
    const shape = i % 3;
    return {
      left: r(0, 100), size: soft ? r(5, 8) : r(6, 12), delay: r(0, soft ? 3 : 2.2), dur: soft ? r(4.5, 6.5) : r(2.4, 4.2),
      rot: r(0, 360), drift: r(-40, 40), color: colors[Math.floor(r(0, colors.length))],
      shape, round: shape === 1,
    };
  }), [n, seed, soft]);
  // soft: sits behind the content, fainter, and only plays twice
  return (
    <div aria-hidden style={{ position: 'absolute', inset: 0, overflow: 'hidden', pointerEvents: 'none', zIndex: soft ? 0 : 5 }}>
      {pieces.map((p, i) => (
        <span key={i} style={{
          position: 'absolute', top: -16, left: `${p.left}%`,
          width: p.size, height: p.shape === 2 ? p.size * 0.45 : p.size,
          background: p.color, borderRadius: p.round ? '50%' : 2,
          ['--drift']: `${p.drift}px`, ['--rot']: `${p.rot}deg`,
          animation: `reco-confetti ${p.dur}s linear ${p.delay}s ${soft ? 2 : 'infinite'}`,
          opacity: soft ? 0.55 : 0.95,
        }} />
      ))}
    </div>
  );
}

Object.assign(window, { GoogleButton, RecoButton, Confetti, GMapsGlyph });
