// Mean It — auto-demo helpers: virtual cursor, tour callouts, typed text.

function VirtualCursor({ x, y, clicking }) {
  return (
    <div style={{
      position: 'fixed',
      left: 0, top: 0,
      transform: `translate(${x}px, ${y}px)`,
      pointerEvents: 'none',
      zIndex: 99999,
      transition: 'transform 700ms cubic-bezier(.4,.1,.2,1)',
      width: 28, height: 32,
    }}>
      <svg viewBox="0 0 24 28" width="26" height="30" style={{
        transform: clicking ? 'scale(0.82)' : 'scale(1)',
        transformOrigin: '4px 4px',
        transition: 'transform 140ms ease-out',
        filter: 'drop-shadow(0 3px 6px rgba(0,0,0,0.35))',
        display: 'block',
      }}>
        <path d="M3 2 L3 22 L8 18 L11 26 L14 25 L11 17 L19 17 Z"
              fill="var(--t-ink)" stroke="var(--t-paper)" strokeWidth="1.5" strokeLinejoin="round"/>
      </svg>
      {clicking && (
        <span key={Math.random()} style={{
          position: 'absolute',
          left: 4, top: 4,
          width: 4, height: 4,
          borderRadius: '50%',
          background: 'transparent',
          animation: 'demo-click-pulse 520ms ease-out forwards',
          pointerEvents: 'none',
        }}/>
      )}
    </div>
  );
}

function TourCallout({ x, y, text, side = 'top' }) {
  // side: 'top' = callout above target (arrow on bottom)
  //       'bottom' = callout below target (arrow on top)
  const above = side === 'top';
  return (
    <div style={{
      position: 'fixed',
      left: 0, top: 0,
      transform: `translate(${x}px, ${y}px)`,
      pointerEvents: 'none',
      zIndex: 99998,
      transition: 'transform 600ms cubic-bezier(.4,.1,.2,1)',
    }}>
      <div key={text} style={{
        position: 'relative',
        transform: above
          ? 'translate(-50%, -100%) translateY(-14px)'
          : 'translate(-50%, 0%) translateY(14px)',
        animation: 'demo-callout-in 280ms cubic-bezier(.2,1.4,.4,1) both',
      }}>
        <div style={{
          position: 'relative',
          background: 'var(--t-ink)',
          color: 'var(--t-paper)',
          padding: '10px 14px',
          borderRadius: 6,
          fontFamily: 'var(--t-mono)',
          fontSize: 11,
          letterSpacing: '0.1em',
          textTransform: 'uppercase',
          whiteSpace: 'nowrap',
          boxShadow: '0 6px 18px rgba(0,0,0,0.22)',
        }}>
          {text}
          <span style={{
            position: 'absolute',
            left: '50%',
            [above ? 'bottom' : 'top']: -5,
            transform: 'translateX(-50%) rotate(45deg)',
            width: 10, height: 10,
            background: 'var(--t-ink)',
          }}/>
        </div>
      </div>
    </div>
  );
}

function TypedText({ text, speed = 12, startDelay = 0, onDone, className, style, showCaret = true }) {
  const [shown, setShown] = React.useState(0);
  React.useEffect(() => {
    let i = 0, timer;
    setShown(0);
    const tick = () => {
      i++;
      setShown(i);
      if (i < text.length) timer = setTimeout(tick, speed);
      else if (onDone) onDone();
    };
    const start = setTimeout(tick, startDelay);
    return () => { clearTimeout(start); clearTimeout(timer); };
  }, [text, speed, startDelay]);

  const done = shown >= text.length;
  const visible = text.slice(0, shown);
  const lines = visible.split('\n');

  return (
    <span className={className} style={style}>
      {lines.map((line, i) => (
        <React.Fragment key={i}>
          {line}
          {i < lines.length - 1 && <br/>}
        </React.Fragment>
      ))}
      {showCaret && !done && (
        <span style={{
          display: 'inline-block',
          width: 2,
          height: '0.95em',
          background: 'currentColor',
          verticalAlign: 'baseline',
          marginLeft: 2,
          animation: 'blink 1s infinite',
        }}/>
      )}
    </span>
  );
}

// helpers used by PickMoment etc.
function rectCenter(el) {
  if (!el) return null;
  const r = el.getBoundingClientRect();
  return { x: r.left + r.width / 2, y: r.top + r.height / 2 };
}
function rectTop(el, offset = 0) {
  if (!el) return null;
  const r = el.getBoundingClientRect();
  return { x: r.left + r.width / 2, y: r.top + offset };
}

Object.assign(window, { VirtualCursor, TourCallout, TypedText, rectCenter, rectTop });
