/* journify.ai — PickList: a list that picks a picture (journify/brand/README.md section 1, Motion). On the left the
   heading and the rows (a title and its words); on the right one grey panel with the picture of the active row
   (components/Pictures.jsx) and that row's foot (a line and a link). The active row is ink, the others are faded.
   Hover, focus or a tap picks a row; the first row is active at the start. Under 900px the panel sits under the rows.
   Built from the homepage's "Three reasons" section (sections/S3.jsx, JC 2026-11-09), which now draws itself with it.
   PickList({ headline, rows, stack }): rows are { title, body, picture, foot }, each a node or a string.
   stack (the Sprint pages, JC 2026-11-10): rows with long words. Above 900px the panel stays on screen while the rows
   scroll, and the row being read becomes active as it passes the middle of the screen; under 900px each row shows
   its own picture under its words, every row at full ink, and the panel is not drawn. */

const PICK_CSS = `
.j-pick { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); column-gap: 64px; align-items: start; }
.j-pick-h2 { max-width: 520px; }
.j-pick-rows { margin-top: 36px; list-style: none; padding: 0; display: flex; flex-direction: column; }
.j-pick-row { padding: 22px 0; border-top: 1px solid var(--hairline); cursor: default; opacity: .38; transition: opacity var(--t-reveal); outline: none; }
.j-pick-row:last-child { border-bottom: 1px solid var(--hairline); }
.j-pick-row.is-on { opacity: 1; }
.j-pick-row:focus-visible { outline: 2px solid var(--action); outline-offset: 4px; border-radius: 4px; }
.j-pick-row h3 { margin: 0; font-size: var(--fs-h3); line-height: var(--lh-h3); letter-spacing: var(--ls-h3); font-weight: var(--w-semibold); }
.j-pick-row p { margin: 8px 0 0; font-size: 15px; line-height: 1.55; color: var(--ink-2); max-width: 440px; }
.j-pick-row ul { margin: 12px 0 0; padding: 0 0 0 18px; font-size: 15px; line-height: 1.55; color: var(--ink-2); max-width: 440px; }
.j-pick-row li + li { margin-top: 6px; }
.j-pick-inline { display: none; }
.j-pick-panel { position: relative; min-height: 520px; padding: 40px 40px 32px; background: var(--block); border-radius: var(--r-block); overflow: hidden; display: flex; flex-direction: column; justify-content: center; }
.j-pick-pic { position: relative; max-width: 460px; margin: 0 auto; width: 100%; }
.j-pick-shot { position: absolute; inset: 0; opacity: 0; transform: translateY(10px); transition: opacity 360ms, transform 420ms; pointer-events: none; }
.j-pick-shot.is-on { position: relative; opacity: 1; transform: none; pointer-events: auto; }
.j-pick-foot { margin: 28px 0 0; font-size: 15px; line-height: 1.55; max-width: 520px; }
.j-pick-panel .j-link-standalone { display: inline-block; margin-top: 12px; }
.j-pick-glow { position: absolute; left: -10%; right: -10%; bottom: -8%; height: 70%; pointer-events: none; opacity: .7; filter: blur(18px);
  background: radial-gradient(60% 62% at 50% 100%, color-mix(in srgb, var(--ember) 40%, transparent) 0%, transparent 70%); }
.j-pick.is-stack .j-pick-panel { position: sticky; top: calc(var(--nav) + 24px); }
.j-pick.is-stack .j-pick-row p { max-width: 520px; }
@media (max-width: 899px) {
  .j-pick { grid-template-columns: minmax(0, 1fr); row-gap: 24px; }
  .j-pick-rows { margin-top: 24px; }
  .j-pick-row { padding: 16px 0; opacity: .5; }
  .j-pick-panel { min-height: 0; padding: 24px 20px 22px; }
  .j-pick.is-stack .j-pick-row { opacity: 1; padding: 20px 0 24px; }
  .j-pick.is-stack .j-pick-panel { display: none; }
  .j-pick.is-stack .j-pick-inline { display: block; margin-top: 16px; padding: 16px; background: var(--block); border-radius: var(--r-block); }
}
@media (prefers-reduced-motion: reduce) {
  .j-pick-row, .j-pick-shot { transition: none; }
}
`;

function PickList({ headline, rows, stack }) {
  const [on, setOn] = React.useState(0);
  const listRef = React.useRef(null);
  /* stack: the row being read is the last one whose top has passed the middle of the screen */
  React.useEffect(() => {
    if (!stack || !listRef.current) return undefined;
    let raf = 0;
    const pick = () => {
      raf = 0;
      if (window.innerWidth < 900) return;
      const items = Array.from(listRef.current.children); let k = 0;
      items.forEach((el, i) => { if (el.getBoundingClientRect().top < window.innerHeight * 0.5) k = i; });
      setOn(k);
    };
    const onScroll = () => { if (!raf) raf = requestAnimationFrame(pick); };
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => { window.removeEventListener('scroll', onScroll); cancelAnimationFrame(raf); };
  }, [stack, rows]);
  if (!rows || !rows.length) return null;
  const row = rows[on] || rows[0];
  const words = v => (typeof v === 'string' ? parseInline(v) : v);
  return (
    <div className={'j-pick' + (stack ? ' is-stack' : '')}>
      <style>{PICK_CSS}</style>
      <div>
        <h2 className="j-h2 j-pick-h2">{words(headline)}</h2>
        <ul className="j-pick-rows" ref={listRef}>
          {rows.map((r, i) => (
            <li key={i} tabIndex={0} className={'j-pick-row' + (i === on ? ' is-on' : '')}
                onMouseEnter={() => setOn(i)} onFocus={() => setOn(i)} onClick={() => setOn(i)}>
              <h3>{words(r.title)}</h3>
              {typeof r.body === 'string' ? <p>{parseInline(r.body)}</p> : r.body}
              {stack && r.picture && <div className="j-pick-inline" aria-hidden="true"><Pic p={r.picture} /></div>}
            </li>
          ))}
        </ul>
      </div>
      <div className="j-pick-panel">
        <div className="j-pick-glow" aria-hidden="true" />
        <div className="j-pick-pic">
          {rows.map((r, i) => (
            <div key={i} className={'j-pick-shot' + (i === on ? ' is-on' : '')} aria-hidden={i !== on}>
              <Pic p={r.picture} />
            </div>
          ))}
        </div>
        {row.foot}
      </div>
    </div>
  );
}

Object.assign(window, { PickList });
