/* global React */
const { useState, useEffect, useRef, useMemo, useCallback } = React;

// ---------- Logo components (inline SVG so they can animate) ----------

function TelarGlyph({ size = 120, animate = false, color = "#e8dfd0", duration = 1.6 }) {
  // Glyph: 5 vertical lines, 2 outer @6pt, 3 inner @3pt; upper/lower thirds vertical + 4 diagonals converging on opposite midpoint.
  // viewBox 200x160
  const bodyRef = useRef(null);
  useEffect(() => {
    if (!animate || !bodyRef.current) return;
    // Replay on mount: set initial dashoffset and transition
    const paths = bodyRef.current.querySelectorAll("[data-anim]");
    paths.forEach((p) => {
      const len = p.getTotalLength ? p.getTotalLength() : 200;
      p.style.transition = "none";
      p.style.strokeDasharray = len;
      p.style.strokeDashoffset = len;
      p.style.opacity = 0;
      const delay = Number(p.dataset.delay || 0);
      // Kick
      requestAnimationFrame(() => {
        p.style.transition = `stroke-dashoffset ${duration}s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s, opacity 0.4s ease ${delay}s`;
        p.style.strokeDashoffset = 0;
        p.style.opacity = 1;
      });
    });
  }, [animate, duration]);
  return (
    <svg
      viewBox="0 0 200 160"
      width={size}
      height={size * (160 / 200)}
      xmlns="http://www.w3.org/2000/svg"
      role="img"
      aria-label="Telar"
    >
      <g
        ref={bodyRef}
        fill="none"
        stroke={color}
        strokeLinecap="square"
      >
        {/* outer borders — upper + lower thirds (vertical segments) */}
        <line data-anim data-delay="0" x1="20" y1="0" x2="20" y2="53.33" strokeWidth="6" />
        <line data-anim data-delay="0" x1="20" y1="106.67" x2="20" y2="160" strokeWidth="6" />
        <line data-anim data-delay="0" x1="180" y1="0" x2="180" y2="53.33" strokeWidth="6" />
        <line data-anim data-delay="0" x1="180" y1="106.67" x2="180" y2="160" strokeWidth="6" />
        {/* inner warp — 3pt */}
        <line data-anim data-delay="0.2" x1="60" y1="0" x2="60" y2="160" strokeWidth="3" />
        <line data-anim data-delay="0.28" x1="100" y1="0" x2="100" y2="160" strokeWidth="3" />
        <line data-anim data-delay="0.36" x1="140" y1="0" x2="140" y2="160" strokeWidth="3" />
        {/* diagonals */}
        <line data-anim data-delay="0.6" x1="20" y1="53.33" x2="180" y2="80" strokeWidth="6" />
        <line data-anim data-delay="0.72" x1="20" y1="106.67" x2="180" y2="80" strokeWidth="6" />
        <line data-anim data-delay="0.84" x1="180" y1="53.33" x2="20" y2="80" strokeWidth="6" />
        <line data-anim data-delay="0.96" x1="180" y1="106.67" x2="20" y2="80" strokeWidth="6" />
      </g>
    </svg>
  );
}

function TelarWordmark({ width = 140, color = "#e8dfd0" }) {
  return (
    <svg viewBox="0 0 480 160" width={width} height={width * (160 / 480)} xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Telar">
      <g fill="none" stroke={color} strokeLinecap="square">
        <line x1="20" y1="0" x2="20" y2="53.33" strokeWidth="6" />
        <line x1="20" y1="106.67" x2="20" y2="160" strokeWidth="6" />
        <line x1="20" y1="53.33" x2="180" y2="80" strokeWidth="6" />
        <line x1="20" y1="106.67" x2="180" y2="80" strokeWidth="6" />
        <line x1="180" y1="0" x2="180" y2="53.33" strokeWidth="6" />
        <line x1="180" y1="106.67" x2="180" y2="160" strokeWidth="6" />
        <line x1="180" y1="53.33" x2="20" y2="80" strokeWidth="6" />
        <line x1="180" y1="106.67" x2="20" y2="80" strokeWidth="6" />
        <line x1="60" y1="0" x2="60" y2="160" strokeWidth="3" />
        <line x1="100" y1="0" x2="100" y2="160" strokeWidth="3" />
        <line x1="140" y1="0" x2="140" y2="160" strokeWidth="3" />
      </g>
      <text x="210" y="112" fontFamily="'Space Grotesk', system-ui, sans-serif" fontWeight="500" fontSize="100" fill={color} letterSpacing="4">Telar</text>
    </svg>
  );
}

function TelarColorVertical({ size = 160, animate = false }) {
  // Colored vertical logo from brand/telar-vertical-color-night.svg.
  // Inline so we can animate the strokes.
  const bodyRef = useRef(null);
  useEffect(() => {
    if (!animate || !bodyRef.current) return;
    const paths = bodyRef.current.querySelectorAll("[data-anim]");
    paths.forEach((p) => {
      const len = p.getTotalLength ? p.getTotalLength() : 200;
      p.style.transition = "none";
      p.style.strokeDasharray = len;
      p.style.strokeDashoffset = len;
      p.style.opacity = 0;
      const delay = Number(p.dataset.delay || 0);
      requestAnimationFrame(() => {
        p.style.transition = `stroke-dashoffset 1.4s cubic-bezier(0.22, 1, 0.36, 1) ${delay}s, opacity 0.4s ease ${delay}s`;
        p.style.strokeDashoffset = 0;
        p.style.opacity = 1;
      });
    });
    // Fade wordmark in last
    const word = bodyRef.current.querySelector("[data-word]");
    if (word) {
      word.style.opacity = 0;
      word.style.transition = "opacity 0.6s ease 1.4s";
      requestAnimationFrame(() => { word.style.opacity = 1; });
    }
  }, [animate]);
  const teal = "#4EB5B5", terra = "#E35A36", ink = "#e8dfd0";
  return (
    <svg viewBox="0 0 200 260" width={size} height={size * (260/200)} xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Telar">
      <g ref={bodyRef} fill="none" strokeLinecap="square">
        {/* outer borders (terracotta) */}
        <line data-anim data-delay="0" x1="20" y1="0" x2="20" y2="53.33" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0" x1="20" y1="106.67" x2="20" y2="160" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0" x1="180" y1="0" x2="180" y2="53.33" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0" x1="180" y1="106.67" x2="180" y2="160" stroke={terra} strokeWidth="6"/>
        {/* inner warp (teal) — full height so they read through the diagonals */}
        <line data-anim data-delay="0.2" x1="60" y1="0" x2="60" y2="160" stroke={teal} strokeWidth="3"/>
        <line data-anim data-delay="0.28" x1="100" y1="0" x2="100" y2="160" stroke={teal} strokeWidth="3"/>
        <line data-anim data-delay="0.36" x1="140" y1="0" x2="140" y2="160" stroke={teal} strokeWidth="3"/>
        {/* diagonals */}
        <line data-anim data-delay="0.6" x1="20" y1="53.33" x2="180" y2="80" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0.72" x1="20" y1="106.67" x2="180" y2="80" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0.84" x1="180" y1="53.33" x2="20" y2="80" stroke={terra} strokeWidth="6"/>
        <line data-anim data-delay="0.96" x1="180" y1="106.67" x2="20" y2="80" stroke={terra} strokeWidth="6"/>
      </g>
      <text data-word y="210" fontFamily="'Space Grotesk', system-ui, sans-serif" fontWeight="500" fontSize="40" fill={ink}>
        <tspan x="60" textAnchor="middle">T</tspan>
        <tspan x="80" textAnchor="middle">e</tspan>
        <tspan x="100" textAnchor="middle">l</tspan>
        <tspan x="120" textAnchor="middle">a</tspan>
        <tspan x="140" textAnchor="middle">r</tspan>
      </text>
    </svg>
  );
}

function LintelWordmarkSmall({ height = 28, color = "#c9c2b8", accent = "#b79b5a", atelierColor = "#8a8278" }) {
  // Small horizontal rendering: "LINTEL — ◇ — atelier" in a single row, muted
  return (
    <svg viewBox="0 0 320 42" height={height} xmlns="http://www.w3.org/2000/svg" role="img" aria-label="Lintel Atelier">
      <text x="0" y="20" fontFamily="Georgia, serif" fontSize="16" fill={color} letterSpacing="4">LINTEL</text>
      <line x1="85" y1="14" x2="195" y2="14" stroke={color} strokeWidth="0.8" />
      <rect x="135" y="10" width="6" height="6" fill={accent} transform="rotate(45 138 13)" />
      <text x="200" y="18" fontFamily="Georgia, serif" fontStyle="italic" fontSize="13" fill={atelierColor} letterSpacing="2">atelier</text>
    </svg>
  );
}

// ---------- Helpers / hooks ----------

function useI18n(lang) {
  return useMemo(() => window.I18N[lang] || window.I18N.en, [lang]);
}

function useRotatingPlaceholder(list, keyTurn) {
  return useMemo(() => {
    if (!list || !list.length) return "";
    return list[keyTurn % list.length];
  }, [list, keyTurn]);
}

function pickLoadingLine(strings, lastIndex) {
  // pseudo-random but don't repeat within last pick
  let idx;
  do {
    idx = Math.floor(Math.random() * strings.length);
  } while (strings.length > 1 && idx === lastIndex);
  return idx;
}

// ---------- Corner ticks container ----------
function Ticks() {
  return <div className="ticks" aria-hidden><span/></div>;
}

// ---------- Export ----------
Object.assign(window, {
  React, useState, useEffect, useRef, useMemo, useCallback,
  TelarGlyph, TelarWordmark, TelarColorVertical, LintelWordmarkSmall,
  useI18n, useRotatingPlaceholder, pickLoadingLine,
  Ticks,
});
