// components.jsx — small reusable components

const { useState, useEffect, useRef } = React;

// Logo mark — original mark for "Tu Abogado Miami"
function LogoMark({ size = 28, color = "currentColor" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
      {/* Stylized "TA" inside a column/serif gavel mark */}
      <rect x="3"  y="6"  width="26" height="2.5" rx="1" fill={color} />
      <rect x="3"  y="23.5" width="26" height="2.5" rx="1" fill={color} />
      <rect x="14.5" y="9" width="3" height="13" fill={color} />
      <rect x="9"  y="9"  width="14" height="2"  fill={color} />
      <circle cx="6.5"  cy="15.5" r="1.4" fill={color} />
      <circle cx="25.5" cy="15.5" r="1.4" fill={color} />
    </svg>
  );
}

// Phone icon
function IconPhone({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden="true">
      <path d="M2.5 3.2c.2-.7.8-1.2 1.5-1.2h1.4c.6 0 1.1.4 1.3 1l.6 2c.1.5 0 1-.4 1.3l-.9.7c.9 1.7 2.3 3.1 4 4l.7-.9c.3-.4.8-.5 1.3-.4l2 .6c.6.2 1 .7 1 1.3v1.4c0 .7-.5 1.3-1.2 1.5-5.6 1.4-11.4-4.4-10-10z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
    </svg>
  );
}

function IconArrow({ size = 14 }) {
  return (
    <svg className="arrow" width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden="true">
      <path d="M3 8h10M9 4l4 4-4 4" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
}

function IconCheck({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden="true">
      <path d="M3 8.5l3.2 3L13 4.5" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
}

function IconPin({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden="true">
      <path d="M8 14s5-4.5 5-8.5A5 5 0 0 0 3 5.5C3 9.5 8 14 8 14z" stroke="currentColor" strokeWidth="1.3" strokeLinejoin="round"/>
      <circle cx="8" cy="5.5" r="1.6" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  );
}

function IconGlobe({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="none" aria-hidden="true">
      <circle cx="8" cy="8" r="6" stroke="currentColor" strokeWidth="1.3"/>
      <path d="M2 8h12M8 2c2 1.8 2 10.2 0 12M8 2c-2 1.8-2 10.2 0 12" stroke="currentColor" strokeWidth="1.3"/>
    </svg>
  );
}

function IconStar({ size = 12 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 16 16" fill="currentColor" aria-hidden="true">
      <path d="M8 1.5l1.8 4 4.2.4-3.2 3 1 4.3L8 11l-3.8 2.3 1-4.3L2 5.9l4.2-.5z"/>
    </svg>
  );
}

function IconScale({ size = 26, color = "currentColor" }) {
  return (
    <svg width={size} height={size} viewBox="0 0 48 48" fill="none" aria-hidden="true">
      <line x1="24" y1="6" x2="24" y2="42" stroke={color} strokeWidth="1.4" />
      <line x1="14" y1="42" x2="34" y2="42" stroke={color} strokeWidth="1.4" />
      <line x1="8" y1="12" x2="40" y2="12" stroke={color} strokeWidth="1.4" />
      {/* left pan */}
      <path d="M8 12 L4 22 A6 6 0 0 0 16 22 L12 12" fill="none" stroke={color} strokeWidth="1.4" />
      {/* right pan */}
      <path d="M40 12 L36 22 A6 6 0 0 0 48 22 L44 12" fill="none" stroke={color} strokeWidth="1.4" transform="translate(-4 0)" />
    </svg>
  );
}

// Striped placeholder image
function Placeholder({ label, ratio = "4 / 5", style = {}, tone = "cream" }) {
  const bg = tone === "deep"
    ? { background: "var(--bg-deep)", color: "var(--paper)" }
    : tone === "brass"
      ? { background: "var(--brass)" }
      : {};
  return (
    <div className="ph" style={{ aspectRatio: ratio, ...bg, ...style }}>
      <div className="ph-label">{label}</div>
    </div>
  );
}

// Reveal-on-scroll wrapper — with viewport check + IO fallback
function Reveal({ children, delay = 0, as: As = "div", style, className = "", ...rest }) {
  const ref = useRef(null);
  const [seen, setSeen] = useState(false);
  useEffect(() => {
    if (!ref.current || seen) return;
    const el = ref.current;
    const inView = () => {
      const r = el.getBoundingClientRect();
      return r.top < (window.innerHeight || 0) - 40 && r.bottom > 0;
    };
    if (inView()) { setSeen(true); return; }
    let io;
    try {
      io = new IntersectionObserver((entries) => {
        if (entries.some((e) => e.isIntersecting)) { setSeen(true); io.disconnect(); }
      }, { threshold: 0.05, rootMargin: "0px 0px -40px 0px" });
      io.observe(el);
    } catch (_) {}
    const onScroll = () => { if (inView()) { setSeen(true); cleanup(); } };
    const cleanup = () => {
      window.removeEventListener("scroll", onScroll);
      window.removeEventListener("resize", onScroll);
      io?.disconnect();
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    window.addEventListener("resize", onScroll);
    // Final safety: ensure content is visible after 1.5s no matter what.
    const fallback = setTimeout(() => setSeen(true), 1500);
    return () => { cleanup(); clearTimeout(fallback); };
  }, [seen]);
  return (
    <As
      ref={ref}
      className={`reveal ${seen ? "in" : ""} ${className}`}
      style={{ transitionDelay: `${delay}ms`, ...style }}
      {...rest}
    >
      {children}
    </As>
  );
}

// Section heading block — eyebrow + display title + lead
function SectionHead({ eyebrow, title, sub, align = "left", maxw = 720, titleSize = "clamp(34px, 4.4vw, 64px)" }) {
  const ta = align === "center" ? "center" : "left";
  return (
    <Reveal style={{ textAlign: ta, maxWidth: maxw, marginLeft: align === "center" ? "auto" : 0, marginRight: align === "center" ? "auto" : 0 }}>
      {eyebrow && (
        <div className="eyebrow" style={{ marginBottom: 18 }}>
          <span style={{ display: "inline-block", width: 22, height: 1, background: "var(--ink-soft)", verticalAlign: "middle", marginRight: 10 }}></span>
          {eyebrow}
        </div>
      )}
      <h2 className="display" style={{ fontSize: titleSize, margin: 0 }}>{title}</h2>
      {sub && <p className="lead" style={{ marginTop: 18, maxWidth: 620, marginLeft: align === "center" ? "auto" : 0, marginRight: align === "center" ? "auto" : 0 }}>{sub}</p>}
    </Reveal>
  );
}

// Animated counter on view
function CountUp({ value, suffix = "", duration = 1400 }) {
  const ref = useRef(null);
  const [n, setN] = useState(0);
  useEffect(() => {
    if (!ref.current) return;
    let raf;
    const io = new IntersectionObserver(([e]) => {
      if (!e.isIntersecting) return;
      const start = performance.now();
      const tick = (t) => {
        const p = Math.min(1, (t - start) / duration);
        const eased = 1 - Math.pow(1 - p, 3);
        setN(Math.round(value * eased));
        if (p < 1) raf = requestAnimationFrame(tick);
      };
      raf = requestAnimationFrame(tick);
      io.disconnect();
    }, { threshold: 0.5 });
    io.observe(ref.current);
    return () => { io.disconnect(); cancelAnimationFrame(raf); };
  }, [value, duration]);
  return <span ref={ref}>{n.toLocaleString()}{suffix}</span>;
}

Object.assign(window, {
  LogoMark, IconPhone, IconArrow, IconCheck, IconPin, IconGlobe, IconStar, IconScale,
  Placeholder, Reveal, SectionHead, CountUp,
});
