/* journify.ai — Recording: the one rule for every recording (journify/brand/README.md section 2). Uneven bars
   mean a bad call, a calm wave means a good call, tall bars go from oxblood into ember, and the height does
   not grow. HeroCall, S2, S7, HowItWorks and the pictures (components/Pictures.jsx) read their bar colours here; nothing else
   computes them. */

const REC_RGB = {};
function recRgb(name) {
  if (!REC_RGB[name]) {
    const h = getComputedStyle(document.documentElement).getPropertyValue(name).trim().replace('#', '');
    REC_RGB[name] = [0, 2, 4].map(i => parseInt(h.slice(i, i + 2), 16));
  }
  return REC_RGB[name];
}
function recMix(a, b, u) { return 'rgb(' + a.map((v, i) => Math.round(v + (b[i] - v) * u)).join(',') + ')'; }
const recCurve = (h, max) => Math.pow(Math.min(1, h / max), 2.2);
/* a bar of height h in a recording whose tallest bar is max: oxblood into ember */
function recHeat(h, max) { return recMix(recRgb('--action'), recRgb('--ember'), recCurve(h, max)); }
/* the same, for a call that went badly: grey into oxblood */
function recCold(h, max) { return recMix(recRgb('--hairline'), recRgb('--action'), Math.min(1, recCurve(h, max) * 1.6)); }

/* a buyer's voice that talks, then hedges: the last 30% of the bars get quieter and break into pauses.
   Seeded, so every visit draws the same recording. */
function recVoice(seed, n) {
  const out = []; let s = seed; const tail = Math.round(n * 0.7);
  for (let i = 0; i < n; i++) {
    s = (s * 9301 + 49297) % 233280; const r = s / 233280;
    let h = Math.round(6 + 24 * (0.35 + 0.65 * Math.abs(Math.sin((i + seed) / 4))) * (0.45 + 0.55 * r));
    if (i >= tail) { h = Math.max(2, Math.round(h * (1 - 0.8 * (i - tail) / (n - tail)))); if ((i + seed) % 3 === 0) h = 2; }
    out.push(Math.min(30, h));
  }
  return out;
}

/* the calm wave across the lower half of a card (the Sprint cards, the close card): one rise and fall at the card's
   width, bars 4px wide and 10px apart, oxblood at the nodes and ember at the peak, the same rule as the journey
   cards' row-wide wave. The card gives it a class with position, height and opacity. */
function RecWave({ width, className }) {
  if (!width) return null;
  const bars = [];
  for (let x = 5; x < width; x += 10) {
    const u = x / width, h2 = 0.5 + 0.42 * Math.sin(u * Math.PI * 2 - Math.PI / 2);
    const col = h2 < 0.62 ? recMix(recRgb('--hairline'), recRgb('--action'), Math.min(1, h2 * 1.8)) : recMix(recRgb('--action'), recRgb('--ember'), Math.min(1, (h2 - 0.62) * 2.4));
    bars.push(<i key={x} style={{ position: 'absolute', top: '50%', left: x - 2, width: 4, borderRadius: 2, transform: 'translateY(-50%)', height: Math.round(h2 * 92) + '%', background: col }} />);
  }
  return <div className={className} aria-hidden="true">{bars}</div>;
}
/* the glare at the bottom of a card, one colour per card (JC, 2026-11-09: yellow, red, orange, darker red; the yellow
   is its own token since 2026-11-10, journify/brand/tokens.css) */
const REC_GLARE = { yellow: 'var(--glare-yellow)', red: 'var(--hot)', orange: 'var(--ember)', deep: 'var(--band-deep)' };

/* a grey card with its glare and wave (styles/tokens.css, .j-gcard): the Sprint cards, the close card, and the cards of
   the offer pages, /tools and the case study. glare names a REC_GLARE colour. The card is lit (glare in full colour)
   while half of it is on screen, and gets is-in the first time, so a picture inside can play once and hold. */
function GlareCard({ glare, className, children, id, label }) {
  const ref = React.useRef(null);
  const [w, setW] = React.useState(0), [lit, setLit] = React.useState(false), [seen, setSeen] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current; if (!el) return undefined;
    const measure = () => setW(el.getBoundingClientRect().width);
    measure(); window.addEventListener('resize', measure);
    let io = null;
    if ('IntersectionObserver' in window) {
      /* half the card on screen, or, for a card taller than two screens, half the screen filled by it */
      io = new IntersectionObserver(es => es.forEach(e => { const on = e.isIntersecting && (e.intersectionRatio >= 0.5 || e.intersectionRect.height >= window.innerHeight * 0.5); setLit(on); if (on) setSeen(true); }), { threshold: [0, 0.25, 0.5, 0.75, 1] });
      io.observe(el);
    } else setSeen(true);
    return () => { window.removeEventListener('resize', measure); if (io) io.disconnect(); };
  }, []);
  return (
    <div ref={ref} id={id} data-screen-label={label} className={'j-gcard' + (className ? ' ' + className : '') + (lit ? ' is-lit' : '') + (seen ? ' is-in' : '')}>
      <div className="j-gcard-glare" aria-hidden="true" style={{ '--glare': REC_GLARE[glare] || REC_GLARE.deep }} />
      <RecWave width={w} className="j-gcard-rec" />
      {children}
    </div>
  );
}

Object.assign(window, { recRgb, recHeat, recCold, recVoice, RecWave, REC_GLARE, GlareCard });
