/* ==========================================================================
   PSLPerfAnim.jsx  —  @role
   Home-page Performance-System rings illustration (Identity/Architecture/Capacity). Scroll-triggered: rings float, capacity cover rises, connector draws, PSL badge emerges. CSS keyframes + IntersectionObserver.
   ========================================================================== */
// ============================================================
//  PSLPerfAnim — the stacked-ring "Performance System" hero
//  animation (Identity base → Architecture → Capacity top).
//
//  The SVG + its CSS are lifted verbatim from the standalone
//  hero-animation file the brand supplied, then wrapped so it can
//  link to the three domain cards:
//   • hovering / clicking a ring calls onEnter/onLeave/onSelect
//     with a ring index (0 = identity, 1 = architecture, 2 = capacity)
//   • the `activeIndex` prop highlights the matching ring (and dims
//     the others) when a card is hovered on the right.
//
//  Rendered via dangerouslySetInnerHTML so the authored animation
//  keeps perfect fidelity; interactivity is wired in a useEffect.
// ============================================================

const PSL_ANIM_CSS = `
  .psl-perf-anim{
    --psl-navy:#0D0F2B; --psl-blue-dark:#2424A8; --psl-violet-deep:#5615CD;
    --psl-teal-deep:#0FB388; --psl-blue:#2563EB; --psl-line-strong:#CBD2DE;
    --psl-sans:"Mona Sans",-apple-system,BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Arial,sans-serif;
    width:100%; max-width:780px; aspect-ratio:700 / 540; margin:0 auto; position:relative;
  }
  .psl-perf-anim svg{ width:100%; height:100%; display:block; overflow:visible; }

  .psl-pos{transform-box:fill-box;transform-origin:center}
  .psl-ring{transform-box:fill-box;transform-origin:center}
  .psl-ring .face{stroke-width:1;fill-rule:evenodd;fill-opacity:.78}
  .psl-ring .wall{stroke-width:1;stroke-linejoin:round;fill-opacity:.82}
  .psl-ring .inner-wall{stroke-width:.6;stroke-linejoin:round;stroke-opacity:.35;fill-opacity:.88}

  .psl-ring--cap  .face, .psl-ring--cap  .wall      { fill:url(#pslGCapDark);  stroke:#2424A8 }
  .psl-ring--cap  .inner-wall{ fill:url(#pslGCapInner); stroke:#1818A0 }
  .psl-ring--arch .face, .psl-ring--arch .wall      { fill:url(#pslGArchDark); stroke:var(--psl-teal-deep) }
  .psl-ring--arch .inner-wall{ fill:url(#pslGArchInner); stroke:var(--psl-teal-deep) }
  .psl-ring--id   .face, .psl-ring--id   .wall      { fill:url(#pslGIdDark);   stroke:var(--psl-violet-deep) }
  .psl-ring--id   .inner-wall{ fill:url(#pslGIdInner);  stroke:var(--psl-violet-deep) }

  .psl-cap-cover-face{fill:url(#pslGCapDark);fill-opacity:.85;stroke:#2424A8;stroke-width:1}

  .psl-rim-label{font-family:var(--psl-sans);fill:#ffffff;dominant-baseline:middle}
  .psl-rim-label.lg{font-size:18px;font-weight:600;letter-spacing:6.5px}
  .psl-rim-label.md{font-size:16px;font-weight:600;letter-spacing:4.5px}
  .psl-rim-label.sm{font-size:14px;font-weight:700;letter-spacing:3.5px}
  .psl-rim-label tspan{opacity:0}

  .psl-cover-rim-text{font-family:var(--psl-sans);font-size:17px;font-weight:700;fill:#ffffff;letter-spacing:3px;dominant-baseline:middle}
  .psl-cover-rim-text .tm{font-size:12px}

  .psl-connector{ stroke:var(--psl-line-strong); stroke-width:1.2; fill:none; stroke-dasharray:100; stroke-dashoffset:100; }
  .psl-perf-anim.is-in .psl-connector{ animation: pslDrawConnector 2.5s cubic-bezier(.4,0,.2,1) .8s forwards; }
  @keyframes pslDrawConnector { to { stroke-dashoffset:0 } }

  .psl-arrow{opacity:0}
  .psl-arrow-left  { offset-path: path('M 240,410 L 190,295 L 150,160'); offset-rotate: auto }
  .psl-arrow-right { offset-path: path('M 460,410 L 510,295 L 550,160'); offset-rotate: auto }
  @keyframes pslTravelUp { from { offset-distance:0% } to { offset-distance:100% } }

  .psl-badge-group{transform-box:fill-box;transform-origin:center}
  .psl-badge-bg{fill:#ffffff}

  .psl-ring--id, .psl-ring--arch,
  .psl-ring--cap .face, .psl-ring--cap .inner-wall,
  .psl-arrow, .psl-badge-group{ opacity:0 }
  .psl-ring--cap .wall { opacity:1 }
  .psl-cap-cover-face, .psl-cover-rim-text{ opacity:0 }

  .psl-perf-anim.is-in .psl-ring--cap{ animation: pslFloatAll 4s ease-in-out 0s infinite }
  .psl-perf-anim.is-in .psl-cap-cover-face{ animation: pslCoverFaceLife 2.6s ease 0s forwards, pslFloatAll 4s ease-in-out 0s infinite; }
  @keyframes pslCoverFaceLife { 0%{opacity:0} 15%{opacity:1} 80%{opacity:1} 100%{opacity:0} }
  .psl-perf-anim.is-in .psl-cover-rim-text{ animation: pslCoverTextLife 2.4s ease 0s forwards, pslFloatAll 4s ease-in-out 0s infinite; }
  @keyframes pslCoverTextLife { 0%{opacity:0} 17%{opacity:1} 62%{opacity:1} 100%{opacity:0} }

  .psl-perf-anim.is-in .psl-ring--cap .face       { animation: pslFadeIn .45s ease 2.1s forwards }
  .psl-perf-anim.is-in .psl-ring--cap .inner-wall { animation: pslFadeIn .45s ease 2.1s forwards }

  .psl-perf-anim.is-in .psl-pos--cap{
    animation: pslCoverBounceIn .55s cubic-bezier(.34,1.4,.64,1) 0s forwards,
               pslCoverRise 2.5s cubic-bezier(.4,0,.2,1) .8s forwards;
  }
  @keyframes pslCoverBounceIn {
    0%   { opacity:0; transform: translateY(280px) scale(0.78) }
    65%  { opacity:1; transform: translateY(280px) scale(1.06) }
    100% { opacity:1; transform: translateY(280px) scale(1)    }
  }
  @keyframes pslCoverRise { from { transform: translateY(280px) scale(1) } to { transform: translateY(0) scale(1) } }

  .psl-perf-anim.is-in .psl-ring--id{ animation: pslFadeIn .45s ease 1.0s forwards, pslFloatAll 4s ease-in-out 0s infinite; }
  .psl-perf-anim.is-in .psl-ring--arch{ animation: pslFadeIn .45s ease 1.8s forwards, pslFloatAll 4s ease-in-out 0s infinite; }

  .psl-rim-label{opacity:1}

  .psl-perf-anim.is-in .psl-badge-group { animation: pslBadgeEmerge .8s cubic-bezier(.2,.8,.25,1) 2.8s forwards, pslBadgeBreath 4s ease-in-out 3.6s infinite; }
  @keyframes pslBadgeEmerge { 0%{opacity:0;transform:scale(.6)} 65%{opacity:1;transform:scale(1.05)} 100%{opacity:1;transform:scale(1)} }
  @keyframes pslBadgeBreath { 0%,100%{transform:translateY(0) scale(1)} 50%{transform:translateY(-5px) scale(1.03)} }

  .psl-perf-anim.is-in .psl-arrow.p1{ animation: pslFadeIn .4s ease 3.5s forwards, pslTravelUp 6s linear 3.5s infinite }
  .psl-perf-anim.is-in .psl-arrow.p2{ animation: pslFadeIn .4s ease 3.5s forwards, pslTravelUp 6s linear 1.5s infinite }
  .psl-perf-anim.is-in .psl-arrow.p3{ animation: pslFadeIn .4s ease 3.5s forwards, pslTravelUp 6s linear -0.5s infinite }

  @keyframes pslFloatAll { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-5px)} }
  @keyframes pslFadeIn   { to { opacity:1 } }

  /* ---- Card → ring link ----
     The graphic itself is NOT mouse-reactive (no hover, no cursor change).
     When a card on the right is clicked, React sets data-active and the
     matching ring glows softly in its own colour. Nothing else. */
  .psl-ring{ transition: filter .28s ease; }
  .psl-perf-anim[data-active="identity"]     .psl-ring--id  { filter: drop-shadow(0 0 16px rgba(137,54,255,0.65)); }
  .psl-perf-anim[data-active="architecture"] .psl-ring--arch{ filter: drop-shadow(0 0 16px rgba(46,224,176,0.65)); }
  .psl-perf-anim[data-active="capacity"]     .psl-ring--cap { filter: drop-shadow(0 0 16px rgba(79,79,255,0.7)); }

  @media (prefers-reduced-motion: reduce){
    .psl-perf-anim *, .psl-perf-anim *::before, .psl-perf-anim *::after{
      animation-duration:.01ms !important; animation-iteration-count:1 !important; transition:none !important;
    }
    .psl-rim-label tspan{ opacity:1 !important }
    .psl-connector{ stroke-dashoffset:0 !important }
  }
`;

const PSL_ANIM_SVG = `
  <svg viewBox="0 0 700 540" preserveAspectRatio="xMidYMid meet" role="img" aria-label="PSL Performance System mark animation">
    <defs>
      <linearGradient id="pslGCapDark" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#4F4FFF"/><stop offset="1" stop-color="#2424A8"/></linearGradient>
      <linearGradient id="pslGCapInner" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#1F1F8A"/><stop offset="1" stop-color="#0F0F5C"/></linearGradient>
      <linearGradient id="pslGArchDark" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#2EE0B0"/><stop offset="1" stop-color="#0FB388"/></linearGradient>
      <linearGradient id="pslGArchInner" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#0E6E55"/><stop offset="1" stop-color="#054032"/></linearGradient>
      <linearGradient id="pslGIdDark" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#8936FF"/><stop offset="1" stop-color="#5615CD"/></linearGradient>
      <linearGradient id="pslGIdInner" x1="0" y1="0" x2="0" y2="1"><stop offset="0" stop-color="#3D0F8C"/><stop offset="1" stop-color="#270760"/></linearGradient>

      <g id="pslCometShape">
        <circle cx="-13" cy="0" r="1.2" fill="#2563EB" opacity=".15"/>
        <circle cx="-9"  cy="0" r="1.8" fill="#2563EB" opacity=".30"/>
        <circle cx="-5"  cy="0" r="2.4" fill="#2563EB" opacity=".55"/>
        <circle cx="0"   cy="0" r="3.4" fill="#2563EB"/>
      </g>

      <symbol id="pslBadgeSymbol" viewBox="0 0 529.1 600">
        <path fill="#050042" d="M183.7,381c2.5-2.4,3.7-5.8,3.7-10.2s-.5-5.4-1.6-7.3c-1.1-2-2.7-3.5-4.8-4.6-2.1-1.1-4.7-1.6-7.8-1.6h-33.5v27.4h33.5c4.5,0,8-1.2,10.5-3.7Z"/>
        <path fill="#050042" d="M509.1,136.4l-140.7-81.3c-4.2-2.4-9.5-1-11.9,3.2-2.4,4.2-1,9.5,3.2,11.9l140.7,81.3c7,4,11.4,11.6,11.4,19.7v259.1c0,8.1-4.4,15.6-11.4,19.7l-224.4,129.6c-7,4-15.7,4-22.7,0L28.8,450c-4.1-2.4-11.4-13.3-11.4-19.7V171.2c0-8.1,4.4-15.6,11.4-19.7l140.9-81.3c4.2-2.4,5.6-7.7,3.2-11.9-2.4-4.2-7.7-5.6-11.9-3.2L20.1,136.4C7.7,143.5,0,156.9,0,171.2v259.1C0,444.6,7.7,457.9,20.1,465.1l224.4,129.6c6.2,3.6,13.1,5.4,20.1,5.4s13.9-1.8,20.1-5.4l224.4-129.6c12.4-7.1,20.1-20.5,20.1-34.8V171.2c0-14.3-7.7-27.6-20.1-34.8Z"/>
        <path fill="#050042" d="M195.7,17.4h.3v123.4L38.6,298.5c-1.6,1.6-2.6,3.9-2.6,6.2v109.7c0,11.7,6.3,22.6,16.4,28.5l195.7,113.6c5.1,2.9,10.8,4.4,16.4,4.4s11.4-1.5,16.4-4.4l195.7-113.6c10.1-5.9,16.4-16.8,16.4-28.5v-109.7c0-2.3-.9-4.5-2.6-6.2l-157.7-157.6V17.4h.5c4.8,0,8.7-3.9,8.7-8.7s-3.9-8.7-8.7-8.7h-137.8c-4.8,0-8.7,3.9-8.7,8.7s3.9,8.7,8.7,8.7ZM117,435.9v-97h61.6c6.6,0,12.2,1.3,16.9,4,4.7,2.7,8.3,6.4,10.8,11.2,2.5,4.8,3.7,10.3,3.7,16.7s-1.3,11.9-3.9,16.7c-2.6,4.9-6.3,8.6-11,11.3-4.8,2.7-10.5,4-17.2,4h-38.3v33h-22.7ZM246.4,414.1c2,1.9,4.9,3.3,8.8,4.2,3.8.9,8.3,1.4,13.3,1.4s5-.1,7.4-.4c2.4-.3,4.6-.7,6.6-1.1,2-.5,3.7-1.1,5.2-2,1.5-.8,2.7-1.8,3.5-3,.8-1.1,1.3-2.4,1.3-4,0-2.4-1-4.4-3-5.9-2-1.5-4.6-2.7-7.8-3.7-3.2-1-6.9-1.9-11.1-2.6-4.1-.8-8.4-1.6-12.8-2.5-4.4-.9-8.6-2-12.7-3.5-4.1-1.4-7.8-3.2-11.1-5.4-3.3-2.2-5.9-4.9-7.8-8.3-1.9-3.3-2.9-7.4-2.9-12.2s1.1-8.8,3.2-12.4c2.2-3.6,5.3-6.5,9.3-8.8,4-2.3,8.8-4,14.4-5.2,5.6-1.2,11.7-1.8,18.4-1.8s12.6.6,18,1.8c5.4,1.2,10,3,13.9,5.4,3.9,2.4,6.8,5.4,9,8.9,2.1,3.5,3.2,7.7,3.2,12.5v1.8h-22.2v-1.4c0-2.3-.9-4.2-2.7-5.9-1.8-1.7-4.3-3-7.4-4-3.2-.9-6.8-1.4-11.1-1.4s-9.3.4-12.7,1.1c-3.4.8-5.9,1.8-7.6,3.2-1.6,1.4-2.5,3-2.5,4.8s1,3.9,2.9,5.3c1.9,1.4,4.5,2.5,7.8,3.4,3.3.9,7,1.7,11.1,2.5,4.1.8,8.3,1.6,12.7,2.5,4.4.9,8.6,2,12.7,3.4,4.1,1.4,7.8,3.1,11.1,5.3,3.3,2.2,5.9,4.9,7.8,8.1,1.9,3.2,2.9,7.2,2.9,11.8,0,7.5-2,13.6-5.9,18.2-4,4.6-9.4,8-16.4,10-7,2.1-15.1,3.1-24.2,3.1s-12.8-.5-18.6-1.6c-5.8-1-11-2.8-15.4-5.2-4.4-2.4-7.9-5.6-10.4-9.6-2.5-4-3.7-9-3.7-14.9v-1.1c0-.4,0-.7.1-1h22.4c0,.3-.1.7-.1,1.1v1.4c0,3,1,5.5,3,7.3ZM412.2,416.7v19.2h-78.1v-97h22.7v77.8h55.3ZM318,150.6l151.6,151.6H59.5l151.4-151.6c1.6-1.6,2.6-3.9,2.6-6.2V17.4h102v127c0,2.3.9,4.5,2.6,6.2Z"/>
        <path fill="#050042" d="M296.3,102c0-9.7-7.9-17.7-17.7-17.7s-17.7,7.9-17.7,17.7,7.9,17.7,17.7,17.7,17.7-7.9,17.7-17.7Z"/>
        <path fill="#050042" d="M287.1,254.1c0,13.5,11,24.5,24.5,24.5s24.5-11,24.5-24.5-11-24.5-24.5-24.5-24.5,11-24.5,24.5Z"/>
        <path fill="#050042" d="M233.2,172.6c-12,0-21.7,9.7-21.7,21.7s9.7,21.7,21.7,21.7,21.7-9.7,21.7-21.7-9.7-21.7-21.7-21.7Z"/>
      </symbol>

      <path id="pslRimCap"  d="M 170,175 A 180,48 0 0 0 530,175" fill="none"/>
      <path id="pslRimArch" d="M 206,308 A 144,38 0 0 0 494,308" fill="none"/>
      <path id="pslRimId"   d="M 251,421 A 99,26 0 0 0 449,421" fill="none"/>
    </defs>

    <path class="psl-connector" pathLength="100" d="M 240,410 L 190,295 L 150,160"/>
    <path class="psl-connector" pathLength="100" d="M 460,410 L 510,295 L 550,160"/>

    <use href="#pslCometShape" class="psl-arrow psl-arrow-left p1"/>
    <use href="#pslCometShape" class="psl-arrow psl-arrow-left p2"/>
    <use href="#pslCometShape" class="psl-arrow psl-arrow-left p3"/>
    <use href="#pslCometShape" class="psl-arrow psl-arrow-right p1"/>
    <use href="#pslCometShape" class="psl-arrow psl-arrow-right p2"/>
    <use href="#pslCometShape" class="psl-arrow psl-arrow-right p3"/>

    <g class="psl-pos psl-pos--id">
      <g class="psl-ring psl-ring--id">
        <path class="inner-wall" d="M 285,410 A 65,18 0 0 1 415,410 L 415,432 A 65,18 0 0 0 285,432 Z"/>
        <path class="wall" d="M 240,410 A 110,26 0 0 0 460,410 L 460,432 A 110,26 0 0 1 240,432 Z"/>
        <path class="face" d="M 240,410 A 110,26 0 1 0 460,410 A 110,26 0 1 0 240,410 Z M 285,410 A 65,18 0 1 0 415,410 A 65,18 0 1 0 285,410 Z"/>
        <text class="psl-rim-label psl-lbl-id sm" data-text="IDENTITY"><textPath href="#pslRimId" startOffset="50%" text-anchor="middle">IDENTITY</textPath></text>
      </g>
    </g>

    <g class="psl-pos psl-pos--arch">
      <g class="psl-ring psl-ring--arch">
        <path class="inner-wall" d="M 240,295 A 110,32 0 0 1 460,295 L 460,321 A 110,32 0 0 0 240,321 Z"/>
        <path class="wall" d="M 190,295 A 160,38 0 0 0 510,295 L 510,321 A 160,38 0 0 1 190,321 Z"/>
        <path class="face" d="M 190,295 A 160,38 0 1 0 510,295 A 160,38 0 1 0 190,295 Z M 240,295 A 110,32 0 1 0 460,295 A 110,32 0 1 0 240,295 Z"/>
        <text class="psl-rim-label psl-lbl-arch md" data-text="ARCHITECTURE"><textPath href="#pslRimArch" startOffset="50%" text-anchor="middle">ARCHITECTURE</textPath></text>
      </g>
    </g>

    <g class="psl-badge-group">
      <polygon class="psl-badge-bg" points="350,262 374,275.75 374,304.25 350,318 326,304.25 326,275.75"/>
      <use href="#pslBadgeSymbol" x="320" y="256" width="60" height="68"/>
    </g>

    <g class="psl-pos psl-pos--cap" transform="translate(0,280)">
      <g class="psl-ring psl-ring--cap">
        <path class="inner-wall" d="M 210,160 A 140,36 0 0 1 490,160 L 490,190 A 140,36 0 0 0 210,190 Z"/>
        <path class="wall" d="M 150,160 A 200,48 0 0 0 550,160 L 550,190 A 200,48 0 0 1 150,190 Z"/>
        <path class="face" d="M 150,160 A 200,48 0 1 0 550,160 A 200,48 0 1 0 150,160 Z M 210,160 A 140,36 0 1 0 490,160 A 140,36 0 1 0 210,160 Z"/>
        <text class="psl-rim-label psl-lbl-cap lg" data-text="CAPACITY"><textPath href="#pslRimCap" startOffset="50%" text-anchor="middle">CAPACITY</textPath></text>
      </g>
      <ellipse class="psl-cap-cover-face" cx="350" cy="160" rx="200" ry="48"/>
      <text class="psl-cover-rim-text"><textPath href="#pslRimCap" startOffset="50%" text-anchor="middle">PERFORMANCE SYSTEM<tspan class="tm" dy="-4" dx="1">™</tspan></textPath></text>
    </g>
  </svg>
`;

function ensurePerfAnimStyles() {
  if (typeof document === 'undefined') return;
  if (document.getElementById('__psl-perf-anim-style')) return;
  const s = document.createElement('style');
  s.id = '__psl-perf-anim-style';
  s.textContent = PSL_ANIM_CSS;
  document.head.appendChild(s);
}

// ring index ↔ data-ring string
const RING_IDS = ['identity', 'architecture', 'capacity'];

const PSLPerfAnim = ({ activeIndex = null, onPlay }) => {
  const rootRef = React.useRef(null);
  const onPlayRef = React.useRef(onPlay);
  onPlayRef.current = onPlay;

  ensurePerfAnimStyles();

  React.useEffect(() => {
    const root = rootRef.current;
    if (!root) return;
    const svg = root.querySelector('svg');
    if (!svg) return;

    // --- Build per-character tspans for the typewriter rim labels ---
    svg.querySelectorAll('.psl-rim-label').forEach((label) => {
      const textPath = label.querySelector('textPath');
      if (!textPath) return;
      const text = label.getAttribute('data-text') || textPath.textContent.trim();
      label.setAttribute('data-text', text);
      textPath.textContent = '';
      for (let i = 0; i < text.length; i++) {
        const ts = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
        ts.textContent = text[i];
        ts.style.opacity = '0';
        textPath.appendChild(ts);
      }
    });

    const timers = [];
    const runTypewriter = (label, perCharDelay) => {
      if (!label) return;
      label.querySelectorAll('tspan').forEach((ts, i) => {
        timers.push(setTimeout(() => { ts.style.opacity = '1'; }, i * perCharDelay));
      });
    };

    let fired = false;
    const trigger = () => {
      if (fired) return;
      fired = true;
      root.classList.add('is-in');
      if (onPlayRef.current) onPlayRef.current();
      timers.push(setTimeout(() => runTypewriter(svg.querySelector('.psl-lbl-id'), 80), 1000));
      timers.push(setTimeout(() => runTypewriter(svg.querySelector('.psl-lbl-arch'), 70), 1800));
      timers.push(setTimeout(() => runTypewriter(svg.querySelector('.psl-lbl-cap'), 80), 2100));
    };

    let io;
    if ('IntersectionObserver' in window) {
      io = new IntersectionObserver((entries) => {
        entries.forEach((e) => { if (e.isIntersecting) { trigger(); io.unobserve(e.target); } });
      }, { threshold: 0.05, rootMargin: '0px 0px 100px 0px' });
      io.observe(root);
    } else {
      trigger();
    }

    return () => {
      timers.forEach(clearTimeout);
      if (io) io.disconnect();
    };
  }, []);

  const dataActive = activeIndex != null ? RING_IDS[activeIndex] : undefined;

  return (
    <div
      className="psl-perf-anim"
      ref={rootRef}
      data-active={dataActive}
      dangerouslySetInnerHTML={{ __html: PSL_ANIM_SVG }} />
  );
};

Object.assign(window, { PSLPerfAnim });
