/* eslint-disable */
// AI Accelerators, version B: brief v4 §4 complete, in the site's card-stage
// language. Two ways in is deliberately just orientation — three sentences and
// a turn, no doors or CTAs; the nav and closer carry the routing. The one
// interactive device is the build-stories spotlight (navigation, not decoration).

// Neutral card accents. Same lightness and chroma, hues spread, so they read as
// three distinct cards without borrowing a pillar or accelerator colour.
const ACCB_TONES = ["oklch(0.52 0.036 258)", "oklch(0.52 0.036 152)", "oklch(0.52 0.036 62)"];

// ---------- Build stories: spotlight plate + rail ----------
function AccBSpot({ anon }) {
  const stories = window.ACC_STORIES || [];
  const [on, setOn] = React.useState(0);
  const s = stories[on] || {};
  return (
    <div className="accb-spot" data-reveal>
      <div className="accb-spot-rail" role="tablist" aria-label="Build stories">
        {stories.map((st, i) => (
          <button key={st.pending} type="button" role="tab" aria-selected={i === on}
            className={"accb-rail-b" + (i === on ? " is-on" : "")} style={{ ["--bt"]: ACCB_TONES[i % 3] }} onClick={() => setOn(i)}>
            <span className="accb-rail-n">0{i + 1}</span>
            <span className="accb-rail-w">
              <span className="accb-rail-s">{anon ? st.mvpSector : st.sector}</span>
              <span className="accb-rail-k">{st.kind}</span>
            </span>
          </button>
        ))}
      </div>
      <article className="accb-plate" style={{ ["--bt"]: ACCB_TONES[on % 3] }}>
        <div className="accb-plate-in" key={on}>
          <div className="acc-case-k"><span>{anon ? s.mvpSector : s.sector}</span><span className="acc-case-stage">{s.stage}</span></div>
          <p className="accb-plate-prob">{anon ? (s.mvpProblem || s.problem) : s.problem}</p>
          <p className="accb-plate-body">{s.body}</p>
          {!anon && (
            <div className="acc-case-f">
              <span className="build-cell-lg"><image-slot id={`accb-logo-spot-${s.pending}`} placeholder="Client logo" shape="rect" fit="contain" /></span>
              <span className="build-cell-o"><span className="build-cell-pend">Outcome pending</span><span className="build-cell-cs">{s.pending}</span></span>
            </div>
          )}
        </div>
      </article>
    </div>
  );
}

function AccBCards({ anon }) {
  const stories = window.ACC_STORIES || [];
  return (
    <div className="accb-cards" data-reveal="group">
      {stories.map((s, i) => (
        <article className="accb-card" key={s.pending} style={{ ["--bt"]: ACCB_TONES[i % 3] }}>
          <div className="acc-case-k"><span>{anon ? s.mvpSector : s.sector}</span><span className="acc-case-stage">{s.stage}</span></div>
          <p className="accb-card-prob">{anon ? (s.mvpProblem || s.problem) : s.problem}</p>
          <p className="accb-card-body">{s.body}</p>
          {!anon && (
            <div className="acc-case-f">
              <span className="build-cell-lg"><image-slot id={`accb-logo-${s.pending}`} placeholder="Client logo" shape="rect" fit="contain" /></span>
              <span className="build-cell-o"><span className="build-cell-pend">Outcome pending</span><span className="build-cell-cs">{s.pending}</span></span>
            </div>
          )}
        </article>
      ))}
    </div>
  );
}

// Card treatment: stacked deck, cards fan in 3D and shuffle on click.
function AccBFan({ anon }) {
  const stories = window.ACC_STORIES || [];
  const [order, setOrder] = React.useState([0, 1, 2]);
  const advance = () => setOrder((o) => [...o.slice(1), o[0]]);
  const bring = (i) => setOrder((o) => (o[0] === i ? [...o.slice(1), o[0]] : [i, ...o.filter((x) => x !== i)]));
  return (
    <TiltStage className="accb-fan" strength={1} data-reveal>
      {stories.map((s, i) => (
        <article key={s.pending} className={"accb-fan-c f" + order.indexOf(i)} style={{ ["--bt"]: ACCB_TONES[i % 3] }}
          role="button" tabIndex={0} aria-label={(anon ? s.mvpSector : s.sector) + ": " + s.kind}
          onClick={() => bring(i)} onKeyDown={(e) => { if (e.key === "Enter" || e.key === " ") { e.preventDefault(); bring(i); } }}>
          <div className="acc-case-k"><span>{anon ? s.mvpSector : s.sector}</span><span className="acc-case-stage">{s.stage}</span></div>
          <p className="accb-card-prob">{anon ? (s.mvpProblem || s.problem) : s.problem}</p>
          <p className="accb-card-body">{s.body}</p>
          {!anon && (
            <div className="acc-case-f">
              <span className="build-cell-lg"><image-slot id={`accb-logo-fan-${s.pending}`} placeholder="Client logo" shape="rect" fit="contain" /></span>
              <span className="build-cell-o"><span className="build-cell-pend">Outcome pending</span><span className="build-cell-cs">{s.pending}</span></span>
            </div>
          )}
        </article>
      ))}
      <p className="accb-fan-hint">Click a card to bring it forward.</p>
    </TiltStage>
  );
}

// Card treatment: the problem is the face, the build is revealed on hover or focus.
function AccBFlip({ anon }) {
  const stories = window.ACC_STORIES || [];
  const [open, setOpen] = React.useState(-1);
  return (
    <div className="accb-rev" data-reveal="group">
      {stories.map((s, i) => (
        <article className={"accb-rev-c" + (open === i ? " is-open" : "")} key={s.pending} style={{ ["--bt"]: ACCB_TONES[i % 3] }}>
          <div className="accb-rev-face">
            <div className="acc-case-k"><span>{anon ? s.mvpSector : s.sector}</span><span className="acc-case-stage">{s.stage}</span></div>
            <p className="accb-card-prob">{anon ? (s.mvpProblem || s.problem) : s.problem}</p>
            <button type="button" className="accb-rev-cue" aria-expanded={open === i} onClick={() => setOpen(open === i ? -1 : i)}>What we built</button>
          </div>
          <div className="accb-rev-back">
            <div className="accb-rev-in">
              <p className="accb-card-body">{s.body}</p>
              {!anon && (
                <div className="acc-case-f">
                  <span className="build-cell-lg"><image-slot id={`accb-logo-rev-${s.pending}`} placeholder="Client logo" shape="rect" fit="contain" /></span>
                  <span className="build-cell-o"><span className="build-cell-pend">Outcome pending</span><span className="build-cell-cs">{s.pending}</span></span>
                </div>
              )}
            </div>
          </div>
        </article>
      ))}
    </div>
  );
}

// Card treatment: wide tiles, one per row, the first oversized.
function AccBTiles({ anon }) {
  const stories = window.ACC_STORIES || [];
  return (
    <div className="accb-tiles" data-reveal="group">
      {stories.map((s, i) => (
        <article className={"accb-tile" + (i === 0 ? " is-lead" : "")} key={s.pending} style={{ ["--bt"]: ACCB_TONES[i % 3] }}>
          <div className="accb-tile-k">
            <div className="accb-tile-s">{anon ? s.mvpSector : s.sector}</div>
            <div className="accb-tile-kind">{s.kind}</div>
            <span className="acc-case-stage">{s.stage}</span>
          </div>
          <div className="accb-tile-b">
            <p className="accb-card-prob">{anon ? (s.mvpProblem || s.problem) : s.problem}</p>
            <p className="accb-card-body">{s.body}</p>
            {!anon && (
              <div className="acc-case-f">
                <span className="build-cell-lg"><image-slot id={`accb-logo-tile-${s.pending}`} placeholder="Client logo" shape="rect" fit="contain" /></span>
                <span className="build-cell-o"><span className="build-cell-pend">Outcome pending</span><span className="build-cell-cs">{s.pending}</span></span>
              </div>
            )}
          </div>
        </article>
      ))}
    </div>
  );
}

function AccBAfterFigure({ kind }) {
  if (kind === "split") {
    return (
      <div className="accb-after-fig">
        <svg viewBox="0 0 300 260" role="img" aria-label="After a build, the path either continues with your team or stays managed by us">
          <path d="M22 130 H 132" fill="none" stroke="rgba(0,26,85,.3)" strokeWidth="1.8" />
          <path d="M132 130 C 190 130, 200 62, 262 62" fill="none" stroke="rgba(0,26,85,.28)" strokeWidth="1.8" />
          <path d="M132 130 C 190 130, 200 198, 262 198" fill="none" stroke="var(--ia-light-blue)" strokeWidth="2.2" />
          <circle cx="22" cy="130" r="5" fill="var(--ia-dark-blue)" />
          <circle cx="132" cy="130" r="4" fill="var(--ia-dark-blue)" />
          <circle cx="262" cy="62" r="6" fill="none" stroke="rgba(0,26,85,.5)" strokeWidth="1.8" />
          <circle cx="262" cy="198" r="6" fill="var(--ia-light-blue)" />
          <g fontFamily="Poppins" fontSize="14" fontWeight="500" fill="rgba(0,26,85,.55)">
            <text x="22" y="112">The build</text>
            <text x="262" y="40" textAnchor="end">Your team runs it</text>
            <text x="262" y="228" textAnchor="end" fill="var(--ia-dark-blue)">We keep managing it</text>
          </g>
        </svg>
      </div>
    );
  }
  if (kind === "continuing line") {
    // One line that keeps going: the build ends, the care doesn't.
    return (
      <div className="accb-after-fig">
        <svg viewBox="0 0 300 180" role="img" aria-label="The build finishes and the care continues">
          <path d="M18 118 H 118" fill="none" stroke="var(--ia-dark-blue)" strokeWidth="2.4" />
          <path d="M118 118 H 288" fill="none" stroke="var(--ia-light-blue)" strokeWidth="2.4" strokeDasharray="6 8" />
          <circle cx="18" cy="118" r="5" fill="var(--ia-dark-blue)" />
          <circle cx="118" cy="118" r="6" fill="#fff" stroke="var(--ia-dark-blue)" strokeWidth="2" />
          <g fontFamily="Poppins" fontSize="14" fontWeight="500" fill="rgba(0,26,85,.55)">
            <text x="18" y="98">The build</text>
            <text x="118" y="150" textAnchor="middle" fill="var(--ia-dark-blue)">Handover</text>
            <text x="288" y="98" textAnchor="end">Looked after</text>
          </g>
        </svg>
      </div>
    );
  }
  if (kind === "orbit") {
    // The build at the centre, the partnership circling it.
    return (
      <div className="accb-after-fig">
        <svg viewBox="0 0 300 260" role="img" aria-label="The build at the centre, monitored and improved around it">
          <circle cx="150" cy="130" r="88" fill="none" stroke="#E3E8F2" strokeWidth="2" strokeDasharray="6 8" />
          <rect x="112" y="112" width="76" height="36" rx="6" fill="#fff" stroke="var(--ia-dark-blue)" strokeWidth="1.8" />
          <circle cx="150" cy="42" r="7" fill="var(--ia-light-blue)" />
          <circle cx="226" cy="174" r="7" fill="var(--ia-light-blue)" />
          <circle cx="74" cy="174" r="7" fill="var(--ia-light-blue)" />
          <g fontFamily="Poppins" fontSize="14" fontWeight="500" fill="rgba(0,26,85,.55)">
            <text x="150" y="136" textAnchor="middle" fill="var(--ia-dark-blue)">The build</text>
            <text x="150" y="22" textAnchor="middle">Monitored</text>
            <text x="252" y="204" textAnchor="end">Improved</text>
            <text x="48" y="204">On hand</text>
          </g>
        </svg>
      </div>
    );
  }
  // "steps": the four stages of the arc, with the last one open-ended.
  return (
    <div className="accb-after-fig accb-after-fig--steps">
      <svg viewBox="0 0 300 210" role="img" aria-label="Scope, build, handover, then looked after">
        <g stroke="rgba(0,26,85,.18)" strokeWidth="1.4">
          <path d="M20 40 H 280" /><path d="M20 96 H 280" /><path d="M20 152 H 280" />
        </g>
        <circle cx="20" cy="40" r="4.5" fill="var(--ia-dark-blue)" />
        <circle cx="20" cy="96" r="4.5" fill="var(--ia-dark-blue)" />
        <circle cx="20" cy="152" r="6" fill="var(--ia-light-blue)" />
        <g fontFamily="Poppins" fontSize="14" fontWeight="500" fill="rgba(0,26,85,.55)">
          <text x="36" y="34">Built</text>
          <text x="36" y="90">Handed over</text>
          <text x="36" y="146" fill="var(--ia-dark-blue)">Looked after</text>
          <text x="36" y="186" fontSize="14" fill="rgba(0,26,85,.4)">no end date</text>
        </g>
      </svg>
    </div>
  );
}

const ACCB_TYPE = [
  { t: "An agent, a tool, or a system. ", cls: "" },
  { t: "Whatever the shape,", cls: "accb-t-em" },
  { t: " it's ", cls: "" },
  { t: "engineered properly.", cls: "accb-t-b" },
];
const ACCB_TYPE_LEN = ACCB_TYPE.reduce((n, s) => n + s.t.length, 0);

function AccBTyped({ className = "" }) {
  const ref = React.useRef(null);
  const [n, setN] = React.useState(0);
  const [go, setGo] = React.useState(false);
  const reduced = typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (reduced) { setN(ACCB_TYPE_LEN); return; }
    const io = new IntersectionObserver((es) => {
      es.forEach((e) => { if (e.isIntersecting) { setGo(true); io.unobserve(e.target); } });
    }, { rootMargin: "0px 0px -30% 0px", threshold: 0.2 });
    io.observe(el);
    return () => io.disconnect();
  }, [reduced]);
  React.useEffect(() => {
    if (!go || n >= ACCB_TYPE_LEN) return;
    const id = setTimeout(() => setN((v) => v + 1), n === 0 ? 900 : 22);
    return () => clearTimeout(id);
  }, [go, n]);
  let used = 0;
  const done = n >= ACCB_TYPE_LEN;
  return (
    <p ref={ref} className={className + (done ? " is-done" : "")} aria-label={ACCB_TYPE.map((s) => s.t).join("")}>
      <span aria-hidden="true">
        {ACCB_TYPE.map((s, i) => {
          const take = Math.max(0, Math.min(s.t.length, n - used));
          used += s.t.length;
          if (!take) return null;
          return <span key={i} className={s.cls}>{s.t.slice(0, take)}</span>;
        })}
        {!done && <span className="accb-caret" />}
      </span>
    </p>
  );
}

const ACCB_SHAPES = ["An agent", "A tool", "A system"];

function AccBCycle({ className = "" }) {
  const ref = React.useRef(null);
  const [go, setGo] = React.useState(false);
  const [i, setI] = React.useState(0);
  const [n, setN] = React.useState(0);
  const [back, setBack] = React.useState(false);
  const reduced = typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;

  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (reduced) { setN(ACCB_SHAPES[0].length); return; }
    const io = new IntersectionObserver((es) => es.forEach((e) => setGo(e.isIntersecting)),
      { rootMargin: "0px 0px -25% 0px", threshold: 0.2 });
    io.observe(el);
    return () => io.disconnect();
  }, [reduced]);

  React.useEffect(() => {
    if (!go || reduced) return;
    const word = ACCB_SHAPES[i];
    let wait = back ? 34 : 62;
    if (!back && n === word.length) wait = 1500;      // hold the finished word
    if (back && n === 0) wait = 200;                  // beat before the next one
    const id = setTimeout(() => {
      if (!back) { n === word.length ? setBack(true) : setN(n + 1); }
      else if (n === 0) { setBack(false); setI((i + 1) % ACCB_SHAPES.length); }
      else setN(n - 1);
    }, wait);
    return () => clearTimeout(id);
  }, [go, i, n, back, reduced]);

  return (
    <p ref={ref} className={className + " accb-cyc"}>
      <span className="accb-cyc-line" aria-label={ACCB_SHAPES.join(", ") + "."}>
        <span aria-hidden="true">{ACCB_SHAPES[i].slice(0, n)}<span className="accb-caret" /></span>
      </span>
      <span className="accb-cyc-claim"><em>Whatever the shape,</em> it's <strong>engineered properly.</strong></span>
    </p>
  );
}

function AccBLate({ className = "" }) {
  const ref = React.useRef(null);
  const [on, setOn] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) { setOn(true); return; }
    const io = new IntersectionObserver((es) => {
      es.forEach((e) => { if (e.isIntersecting) { setOn(true); io.unobserve(e.target); } });
    }, { rootMargin: "0px 0px -30% 0px", threshold: 0.2 });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return (
    <p ref={ref} className={className + " accb-late" + (on ? " is-on" : "")}>
      <span className="accb-late-rest">An agent, a tool, or a system. <em>Whatever the shape,</em> it's </span>
      <span className="accb-late-key"><strong>engineered properly.</strong></span>
    </p>
  );
}

function AccBIn({ as = "div", className = "", delay = 0, children, ...rest }) {
  const ref = React.useRef(null);
  const [inView, setInView] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) { setInView(true); return; }
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => { if (e.isIntersecting) { setInView(true); io.unobserve(e.target); } });
    }, { rootMargin: "0px 0px -32% 0px", threshold: 0.25 });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  const Tag = as;
  return (
    <Tag ref={ref} className={"accb-in " + className + (inView ? " is-in" : "")} style={{ transitionDelay: delay + "ms" }} {...rest}>
      {children}
    </Tag>
  );
}

function AcceleratorsBScreen({ go, t }) {
  const anon = !!(window.IA_MVP && window.IA_MVP());
  const faqs = window.ACC_FAQS || [];
  return (
    <div data-screen-label="03 AI Accelerators (B)">
      {/* HERO */}
      <section className="hero dot-grid acc-hero acc-hero--blend acc-hero--b">
        <div className="container">
          <Crumbs items={[{ label: "Home", to: "home" }, "AI Accelerators"]} go={go} />
          <div className="svc-hero">
            <div>
              <h1 className="h-display accb-hero-h" style={{ maxWidth: "19ch" }}>Agents and AI tools, <em>engineered for you.</em></h1>
              <p className="lede" style={{ maxWidth: "54ch" }}>
                Designed and built around how your organisation actually works, looked after by us,
                working for you. That's an Accelerator.
              </p>
              <div className="accb-hero-cta">
                <Button onClick={() => go("contact")}>Book a conversation</Button>
              </div>
            </div>
            <AccArc />
          </div>
          <p className="accb-orient" data-reveal>
            Most organisations find their accelerators through Foundations: the ideas surface once the
            groundwork is done. Others come to us knowing exactly what they need built.
            <strong> Both are welcome, and both get the same care.</strong>
          </p>
        </div>
      </section>

      {/* WHAT WE BUILD, AND HOW */}
      <section className="section section--white accb-wwb-sec">
        <div className="container">
          <AccBIn className="section-head">
            <h2 className="h-section">What we build, and how.</h2>
          </AccBIn>
          <div className="accb-wwb">
            {t.accStatement === "typewriter" ? <AccBTyped className="accb-wwb-stmt" />
              : t.accStatement === "cycling shape" ? <AccBCycle className="accb-wwb-stmt" />
              : <AccBLate className="accb-wwb-stmt" />}
            <AccBIn as="p" className="accb-wwb-p" delay={280}>A process your team is buried in, something your customers use directly, or hours of document work turned into minutes. Designed around how you actually work, connected safely to your systems, and set up with the guardrails to match. We don't build anything we wouldn't be comfortable governing.</AccBIn>
          </div>
        </div>
      </section>

      {/* BUILD STORIES — hidden in the MVP build; the case studies behind it are
          not ready to show. `anon` is the same IA_MVP flag used to anonymise the
          stories, so the section returns as soon as the build flag is off. */}
      {!anon && (
      <section className="section section--paper accb-stories-sec">
        <div className="container">
          <div className="section-head" data-reveal>
            <h2 className="h-section">Three builds, three sectors.</h2>
          </div>
          {t.accStories === "spotlight" ? <AccBSpot anon={anon} />
            : t.accStories === "fanned deck" ? <AccBFan anon={anon} />
            : t.accStories === "reveal" ? <AccBFlip anon={anon} />
            : t.accStories === "wide tiles" ? <AccBTiles anon={anon} />
            : <AccBCards anon={anon} />}
        </div>
      </section>
      )}

      {/* AFTER THE BUILD */}
      <section className="section section--white acc-after-sec">
        <div className="container">
          {t.accAfter === "ruled columns" ? (
            <React.Fragment>
              <div className="section-head" data-reveal>
                <h2 className="h-section">After the build.</h2>
              </div>
              <p className="acc-stmt accb-ab-stmt" data-reveal>
                Every build comes with a <strong>proper handover</strong>. And where you'd rather we stay
                close, our AI Partnership <em>keeps us managing it.</em>
              </p>
              <div className="accb-ab-paths" data-reveal="group">
                <div className="accb-ab-path">
                  <h3>Your team runs it</h3>
                  <p>Documented, handed over, and yours. We're on hand if something changes, but you own the day to day.</p>
                </div>
                <div className="accb-ab-path">
                  <h3>We keep managing it</h3>
                  <p>Monitoring how it's going, improving it as your business changes, and being there when questions come up.</p>
                </div>
              </div>
            </React.Fragment>
          ) : (
            <React.Fragment>
              <div className="section-head" data-reveal>
                <h2 className="h-section">After the build.</h2>
                <p className="lede">Every build comes with a proper handover. Where you'd rather we stay close, the partnership keeps us managing it.</p>
              </div>
              <div className="accb-ab-cards" data-reveal="group">
                <article className="accb-ab-card">
                  <h3>Your team runs it</h3>
                  <p>Documented, handed over, and yours. We're on hand if something changes, but you own the day to day.</p>
                </article>
                <article className="accb-ab-card">
                  <h3>We keep managing it</h3>
                  <p>Monitoring how it's going, improving it as your business changes, and being there when questions come up.</p>
                </article>
              </div>
            </React.Fragment>
          )}
          <div className="atb-links accb-ab-links" data-reveal>
            <LinkOut onClick={() => go("how-we-work")}>How the partnership works</LinkOut>
          </div>
          {/* CLOSER, folded into the same section so the pattern appears once */}
          <div className="accb-end" data-reveal>
            <p>And if you're not sure whether what you have in mind is an accelerator or a starting point, that's a fine conversation to have too.</p>
            <Button onClick={() => go("contact")}>Book a conversation</Button>
          </div>
        </div>
      </section>

      {/* FAQ */}
      <section className="section section--paper" style={{ paddingBottom: 96 }}>
        <div className="container">
          <div className="section-head">
            <h2 className="h-section">Accelerators, answered plainly.</h2>
            <p className="lede">The questions we're asked most about agents and builds.</p>
          </div>
          <div style={{ marginTop: 20 }}><FaqAccordion items={faqs} id="accelerators-b" /></div>
          <div style={{ marginTop: 20 }}><LinkOut onClick={() => go("faqs")}>See all FAQs</LinkOut></div>
        </div>
      </section>
    </div>
  );
}

window.AcceleratorsBScreen = AcceleratorsBScreen;
