/* Case-study page body — one component, four pages.

   Every page is laid out from its `bands` array, band by band (the Figma frames
   do not share one image rhythm). `size` / `sizes` / `positions` reproduce each
   photo's own crop in the frame. */
const { CaseStudyTitle, CtaBlock } = window.ApexAgencyDesignSystem_54b6ac;

function CaseBands({ bands, title }) {
  return bands.map((band, bi) => {
    const alt = (i) => `${title} — campaign artwork ${bi + 1}.${i + 1}`;
    if (band.type === 'full') {
      return (
        <Reveal
          key={bi} className="cs-full"
          style={{ backgroundImage: `url(${band.image})`, backgroundPosition: band.position || 'center', backgroundSize: band.size, height: `min(${band.height}, ${band.max}px)` }}
          role="img" aria-label={alt(0)}
        />
      );
    }
    const cls = band.type === 'pair' ? 'cs-pair' : 'cs-trio';
    const ground = { inverse: ' on-inverse', strip: ' on-strip', dim: ' on-dim', mid: ' on-mid', muted: ' on-muted' }[band.ground] || '';
    return (
      <div
        key={bi} className={cls + ground}
        style={{ gridTemplateColumns: band.ratio, gap: band.gap, paddingLeft: band.gutter, paddingRight: band.gutter, paddingTop: band.padTop, paddingBottom: band.padBottom }}
      >
        {band.images.map((src, i) => (band.videos && band.videos[i] ? (
          <Reveal key={src} delay={i * 70} className="cs-media" style={{ '--ar': band.aspects ? band.aspects[i] : undefined }}>
            <AutoVideo src={band.videos[i]} poster={src} label={alt(i)} />
          </Reveal>
        ) : (
          <Reveal
            key={src} delay={i * 70}
            style={{ backgroundImage: `url(${src})`, backgroundPosition: band.positions ? band.positions[i] : 'center', backgroundSize: band.sizes ? band.sizes[i] : undefined, '--ar': band.aspects ? band.aspects[i] : undefined }}
            role="img" aria-label={alt(i)}
          />
        )))}
      </div>
    );
  });
}

function CaseStudyPage({ id }) {
  const cs = CASE_STUDIES[id];
  const next = CASE_STUDIES[CASE_ORDER[(CASE_ORDER.indexOf(id) + 1) % CASE_ORDER.length]];
  return (
    <React.Fragment>
      <SiteNav active="Work" />
      <main>
        <Reveal immediate>
          <CaseStudyTitle
            data-screen-label={`Case study / ${cs.title}`}
            title={cs.title} lede={cs.lede} body={cs.body}
            client={cs.client} discipline={cs.discipline}
            clientLabel="Cliente: " nextLabel="Siguiente proyecto"
            onNext={(e) => { e.preventDefault(); window.location.href = next.href; }}
          />
        </Reveal>
        <CaseBands bands={cs.bands} title={cs.title} />
        {cs.video && (
          <div className="case-video">
            <Reveal><AutoVideo src={cs.video.src} poster={cs.video.poster} label={cs.video.label} /></Reveal>
          </div>
        )}
        <Reveal className="case-cta-wrap">
          <CtaBlock className="case-cta" label={'Let’s\nTalk'} href="Apex Lets Talk.html" />
          <img className="rocket" src={DECOR.rocket} alt="" aria-hidden="true" />
        </Reveal>
      </main>
      <Footer />
    </React.Fragment>
  );
}

Object.assign(window, { CaseStudyPage, CaseBands });
