/* Dexilion — case study: BrokerBroker.
   One page per case study; copy lives in SITE.caseStudies.brokerbroker. */

(function () {
  const { Button } = window;
  const {
    Ico,
    Grain,
    Grid,
    Eyebrow,
    Shell,
    tx,
    isTodo,
    SiteHeader,
    PageHero,
    ConversionBand,
    SiteFooter,
  } = window;

  const CS = () => window.SITE.caseStudies.brokerbroker;

  /* The mono particulars strip under the masthead — facts, not adjectives. */
  function Facts() {
    const c = CS();
    return (
      <div
        className="dx-case-facts"
        style={{
          marginTop: "38px",
          display: "grid",
          gridTemplateColumns: "repeat(4,auto)",
          justifyContent: "start",
          gap: "0 40px",
        }}
      >
        {c.facts.map((f, i) => (
          <div
            key={i}
            className="dx-case-fact"
            style={{
              paddingLeft: i ? "40px" : 0,
              borderLeft: i ? "1px solid rgba(255,255,255,.14)" : "none",
            }}
          >
            <div
              style={{
                font: "var(--text-eyebrow)",
                letterSpacing: "var(--ls-eyebrow)",
                textTransform: "uppercase",
                color: "var(--neutral-400)",
              }}
            >
              {f.label}
            </div>
            <div
              style={{
                marginTop: "8px",
                fontFamily: "var(--font-mono)",
                fontSize: "15px",
                color: "var(--white)",
              }}
            >
              {tx(f.value)}
            </div>
          </div>
        ))}
      </div>
    );
  }

  /* The recording sits directly under the masthead, then the framing prose at
     the brand's 720px measure. One band, because they are one thought. */
  function Opening() {
    const c = CS();
    const m = c.media;
    /* The surface is the same either way — a still and a recording should sit
       in the page identically. */
    const frame = {
      display: "block",
      width: "100%",
      height: "auto",
      borderRadius: "var(--radius-surface)",
      border: "1px solid var(--border-subtle)",
      background: "var(--white)",
    };
    return (
      <section
        style={{
          background: "var(--paper-50)",
          padding: "var(--section-y-compact) 0 var(--section-y)",
          position: "relative",
          overflow: "hidden",
        }}
      >
        <Grain />
        <Shell>
          <figure style={{ position: "relative", margin: 0 }}>
            {m.type === "video" ? (
              /* Plays through once and holds on its last frame — the poster is
                 that same frame, so nothing jumps before or after. No loop. */
              <video
                src={m.src}
                poster={m.poster}
                aria-label={m.alt}
                autoPlay
                muted
                playsInline
                style={frame}
                ref={(el) => {
                  if (!el) return;
                  el.muted = true;
                  el.play().catch(() => {});
                }}
              />
            ) : (
              <img src={m.src} alt={m.alt} style={frame} />
            )}
            <figcaption
              style={{
                marginTop: "14px",
                fontFamily: "var(--font-mono)",
                fontSize: "12px",
                color: "var(--text-muted)",
              }}
            >
              {tx(m.caption)}
            </figcaption>
          </figure>

          <div
            className="dx-indent"
            style={{
              position: "relative",
              maxWidth: "var(--container-narrow)",
              marginTop: "64px",
            }}
          >
            {c.intro.map((p, i) => (
              <p
                key={i}
                style={{
                  margin: i ? "22px 0 0" : 0,
                  fontSize: "var(--fs-body-lg)",
                  lineHeight: "var(--lh-body)",
                  color: i === 1 ? "var(--text-strong)" : "var(--text-body)",
                  fontWeight: i === 1 ? "var(--fw-medium)" : "inherit",
                  textWrap: "pretty",
                }}
              >
                {tx(p)}
              </p>
            ))}
          </div>
        </Shell>
      </section>
    );
  }

  /* The four constraints as a ruled list — the brand's default for "here are
     our N things". Numbered in mono, titled in serif, no card grid. */
  function Constraints() {
    const c = CS().constraints;
    return (
      <section
        style={{
          background: "var(--white)",
          padding: "var(--section-y) 0",
          borderTop: "1px solid var(--paper-200)",
          position: "relative",
          overflow: "hidden",
        }}
      >
        <Grid fade="linear-gradient(180deg,#000 0%,transparent 60%)" />
        <Shell>
          <div
            className="dx-services-grid"
            style={{
              position: "relative",
              display: "grid",
              gridTemplateColumns: "340px 1fr",
              gap: "56px",
              alignItems: "start",
            }}
          >
            <div
              className="dx-services-side"
              style={{ position: "sticky", top: "104px" }}
            >
              <Eyebrow>{c.eyebrow}</Eyebrow>
              <h2
                style={{
                  font: "var(--text-h1)",
                  letterSpacing: "var(--ls-heading)",
                  margin: "18px 0 0",
                  color: "var(--text-strong)",
                }}
              >
                {c.heading}
              </h2>
              <p
                style={{
                  margin: "18px 0 0",
                  fontSize: "var(--fs-body)",
                  lineHeight: "var(--lh-body)",
                  color: "var(--text-muted)",
                  maxWidth: "32ch",
                }}
              >
                {tx(c.lede)}
              </p>
            </div>
            <div>
              <div style={{ borderTop: "1px solid var(--paper-200)" }}>
                {c.items.map((item, i) => (
                  <div key={i} className="dx-rule-row">
                    <span className="dx-rule-num">
                      {String(i + 1).padStart(2, "0")}
                    </span>
                    <span
                      style={{
                        fontFamily: "var(--font-serif)",
                        fontSize: "24px",
                        fontWeight: "var(--fw-medium)",
                        letterSpacing: "var(--ls-heading)",
                        lineHeight: 1.3,
                        color: "var(--text-strong)",
                      }}
                    >
                      {tx(item)}
                    </span>
                  </div>
                ))}
              </div>
              <p
                style={{
                  margin: "28px 0 0",
                  maxWidth: "54ch",
                  fontSize: "var(--fs-body)",
                  lineHeight: "var(--lh-body)",
                  color: "var(--text-body)",
                }}
              >
                {tx(c.note)}
              </p>
            </div>
          </div>
        </Shell>
      </section>
    );
  }

  function Approach() {
    const a = CS().approach;
    return (
      <section
        style={{
          background: "var(--paper-50)",
          padding: "var(--section-y) 0",
          position: "relative",
          overflow: "hidden",
        }}
      >
        <Grain />
        <Shell>
          <div
            className="dx-split"
            style={{
              position: "relative",
              display: "grid",
              gridTemplateColumns: "340px 1fr",
              gap: "56px",
              alignItems: "start",
            }}
          >
            <div>
              <Eyebrow>{a.eyebrow}</Eyebrow>
              <h2
                style={{
                  font: "var(--text-h1)",
                  letterSpacing: "var(--ls-heading)",
                  margin: "18px 0 0",
                  color: "var(--text-strong)",
                }}
              >
                {a.heading}
              </h2>
            </div>
            <div style={{ maxWidth: "62ch" }}>
              {a.body.map((p, i) => (
                <p
                  key={i}
                  style={{
                    margin: i ? "22px 0 0" : 0,
                    fontSize: "var(--fs-body-lg)",
                    lineHeight: "var(--lh-body)",
                    color: "var(--text-body)",
                    textWrap: "pretty",
                  }}
                >
                  {tx(p)}
                </p>
              ))}
              <blockquote
                style={{
                  margin: "36px 0 0",
                  paddingLeft: "22px",
                  borderLeft: "3px solid var(--gold-400)",
                  font: "var(--text-quote)",
                  color: "var(--text-strong)",
                  maxWidth: "40ch",
                }}
              >
                {tx(a.quote)}
              </blockquote>
            </div>
          </div>
        </Shell>
      </section>
    );
  }

  function Outcome() {
    const o = CS().outcome;
    const para = {
      margin: "22px 0 0",
      maxWidth: "56ch",
      fontSize: "var(--fs-body-lg)",
      lineHeight: "var(--lh-body)",
      color: "var(--powder-500)",
      textWrap: "pretty",
    };
    return (
      <section
        className="dx-dark"
        style={{
          background: "var(--ink-900)",
          color: "var(--white)",
          padding: "var(--section-y) 0",
          position: "relative",
          overflow: "hidden",
        }}
      >
        <Grid dark fade="linear-gradient(0deg,#000 0%,transparent 90%)" />
        <Grain dark />
        <Shell>
          <div
            className="dx-indent"
            style={{
              position: "relative",
              maxWidth: "var(--container-narrow)",
            }}
          >
            <Eyebrow color="var(--gold-400)">{o.eyebrow}</Eyebrow>
            <h2
              style={{
                font: "var(--text-h1)",
                letterSpacing: "var(--ls-heading)",
                margin: "18px 0 0",
                color: "var(--white)",
                textWrap: "balance",
              }}
            >
              {o.heading}
            </h2>
            {o.body.map((p, i) => (
              <p key={i} style={para}>
                {tx(p)}
              </p>
            ))}
            {/* What the founder walked away with — hairline rows rather than
                bullets, so it reads as a ledger and not a feature list. */}
            {o.points && (
              <ul
                style={{
                  margin: "30px 0 0",
                  padding: 0,
                  listStyle: "none",
                  maxWidth: "56ch",
                  borderTop: "1px solid rgba(255,255,255,.12)",
                }}
              >
                {o.points.map((p, i) => (
                  <li
                    key={i}
                    style={{
                      display: "grid",
                      gridTemplateColumns: "24px 1fr",
                      gap: "14px",
                      alignItems: "baseline",
                      padding: "14px 0",
                      borderBottom: "1px solid rgba(255,255,255,.12)",
                      fontSize: "var(--fs-body)",
                      lineHeight: "var(--lh-body)",
                      color: "var(--powder-500)",
                    }}
                  >
                    <Ico n="check" s={16} color="var(--gold-400)" />
                    <span>{tx(p)}</span>
                  </li>
                ))}
              </ul>
            )}
            {o.close &&
              o.close.map((p, i) => (
                <p key={i} style={para}>
                  {tx(p)}
                </p>
              ))}
            <div
              style={{
                display: "flex",
                gap: "40px",
                flexWrap: "wrap",
                marginTop: "40px",
                paddingTop: "24px",
                borderTop: "1px solid rgba(255,255,255,.12)",
              }}
            >
              {o.stats.map((s, i) => (
                <div key={i}>
                  <div
                    style={{
                      fontFamily: "var(--font-mono)",
                      fontSize: "22px",
                      fontWeight: "var(--fw-semibold)",
                      color: "var(--gold-400)",
                    }}
                  >
                    {tx(s.value)}
                  </div>
                  <div
                    style={{
                      fontSize: "var(--fs-caption)",
                      color: "var(--neutral-400)",
                      marginTop: "4px",
                    }}
                  >
                    {tx(s.label)}
                  </div>
                </div>
              ))}
            </div>
            <div
              style={{
                display: "flex",
                gap: "16px",
                flexWrap: "wrap",
                alignItems: "center",
                marginTop: "40px",
              }}
            >
              <Button
                variant="secondary"
                onClick={() => {
                  window.location.href = "work.html";
                }}
                iconLeft={<Ico n="arrow-left" s={17} />}
                style={{
                  background: "transparent",
                  color: "var(--white)",
                  borderColor: "rgba(255,255,255,.28)",
                }}
              >
                {CS().backLabel}
              </Button>
              {/* Until the store URL is filled in, this stays a placeholder
                  rather than a button that goes nowhere. */}
              {o.link &&
                (isTodo(o.link.href) ? (
                  <span
                    style={{
                      fontSize: "var(--fs-body-sm)",
                      color: "var(--neutral-400)",
                    }}
                  >
                    {o.link.label} - {tx(o.link.href)}
                  </span>
                ) : (
                  <a
                    href={o.link.href}
                    target="_blank"
                    rel="noopener"
                    style={{
                      display: "inline-flex",
                      alignItems: "center",
                      gap: "8px",
                      fontSize: "var(--fs-body-sm)",
                      fontWeight: "var(--fw-semibold)",
                      color: "var(--gold-400)",
                      textDecoration: "none",
                    }}
                  >
                    {o.link.label}
                    <Ico n="arrow-up-right" s={16} color="var(--gold-400)" />
                  </a>
                ))}
            </div>
          </div>
        </Shell>
      </section>
    );
  }

  function Page() {
    const c = CS();
    return (
      <React.Fragment>
        <SiteHeader active="work" />
        <PageHero eyebrow={c.eyebrow} headline={c.headline}>
          <Facts />
        </PageHero>
        <Opening />
        <Constraints />
        <Approach />
        <Outcome />
        <ConversionBand />
        <SiteFooter />
      </React.Fragment>
    );
  }

  mountPage(<Page />);
})();
