/* Shared UI primitives, icons, and reveal-on-scroll hook */

const { useState, useEffect, useRef, useMemo } = React;

/* ---------- Icons (line icons, premium feel) ---------- */
const Icon = ({ name, size = 22, stroke = 1.6 }) => {
  const props = {
    width: size, height: size, viewBox: "0 0 24 24",
    fill: "none", stroke: "currentColor", strokeWidth: stroke,
    strokeLinecap: "round", strokeLinejoin: "round",
  };
  const paths = {
    flag: <><path d="M4 21V4" /><path d="M4 4h13l-2.5 4.5L17 13H4" /></>,
    factory: <><path d="M3 21V11l5 3V11l5 3V7l8 6v8H3z" /><path d="M7 17h.01M11 17h.01M15 17h.01M19 17h.01" /></>,
    truck: <><path d="M1 7h13v10H1z" /><path d="M14 10h4l3 3v4h-7" /><circle cx="6" cy="19" r="2" /><circle cx="17" cy="19" r="2" /></>,
    building: <><rect x="4" y="3" width="16" height="18" rx="1" /><path d="M9 8h.01M9 12h.01M9 16h.01M15 8h.01M15 12h.01M15 16h.01" /></>,
    sliders: <><path d="M4 6h10M18 6h2M4 12h2M10 12h10M4 18h14M20 18h0" /><circle cx="16" cy="6" r="2" /><circle cx="8" cy="12" r="2" /><circle cx="18" cy="18" r="2" /></>,
    headset: <><path d="M4 14v-2a8 8 0 1116 0v2" /><path d="M4 14v3a3 3 0 003 3h1v-7H4zm12 0v7h1a3 3 0 003-3v-3h-4z" /></>,
    star: <path d="M12 2l3 7 7 .8-5.3 4.7 1.6 7L12 18l-6.3 3.5 1.6-7L2 9.8 9 9z" />,
    starFilled: <path d="M12 2l3 7 7 .8-5.3 4.7 1.6 7L12 18l-6.3 3.5 1.6-7L2 9.8 9 9z" fill="currentColor" />,
    starOfDavid: <><polygon points="12,3 18.93,15 5.07,15" /><polygon points="12,21 18.93,9 5.07,9" /></>,
    check: <path d="M5 13l4 4L19 7" />,
    arrow: <><path d="M14 7l-5 5 5 5" /></>,
    arrowL: <><path d="M10 7l5 5-5 5" /></>,
    phone: <path d="M5 4h4l2 5-2.5 1.5a11 11 0 005 5L15 13l5 2v4a2 2 0 01-2 2A16 16 0 013 6a2 2 0 012-2z" />,
    whatsapp: <path d="M20.5 3.5A10 10 0 003 18l-1 4 4-1a10 10 0 0014.5-8.5 10 10 0 00-3-9zM12 19a8 8 0 01-4-1l-3 1 1-3a8 8 0 117 3zm4-5c-.2 0-1.3-.6-1.5-.7s-.4-.1-.5.1l-.7 1c-.1.1-.3.2-.5.1A6 6 0 019 11.5c-.1-.2 0-.4.1-.5l.7-.7c.1-.1.2-.3.1-.5L9 8.4c-.1-.3-.3-.3-.5-.3h-.4c-.2 0-.5 0-.7.3a3 3 0 00-1 2.3c0 1.4 1 2.7 1.2 2.9a8 8 0 003.7 3c.5.2 1 .3 1.3.3.4.1.8 0 1.1-.1.3-.2 1-.4 1.1-.8.2-.4.2-.8.1-.9z" />,
    mail: <><rect x="3" y="5" width="18" height="14" rx="2" /><path d="M3 7l9 6 9-6" /></>,
    pin: <><path d="M12 21s-7-7-7-12a7 7 0 1114 0c0 5-7 12-7 12z" /><circle cx="12" cy="9" r="2.5" /></>,
    clock: <><circle cx="12" cy="12" r="9" /><path d="M12 7v5l3 2" /></>,
    shield: <path d="M12 3l8 3v6c0 4.5-3.5 8.5-8 9-4.5-.5-8-4.5-8-9V6l8-3z" />,
    award: <><circle cx="12" cy="9" r="6" /><path d="M9 14l-2 7 5-3 5 3-2-7" /></>,
    users: <><circle cx="9" cy="8" r="4" /><path d="M2 21a7 7 0 0114 0M17 11a4 4 0 000-8M22 21a7 7 0 00-6-7" /></>,
    spark: <path d="M12 3v4M12 17v4M5 12H1M23 12h-4M6 6l3 3M15 15l3 3M6 18l3-3M15 9l3-3" />,
    edit: <><path d="M11 4H4v16h16v-7" /><path d="M18 2l4 4-9 9H9v-4l9-9z" /></>,
    package: <><path d="M12 22L3 17V7l9-5 9 5v10l-9 5z" /><path d="M3 7l9 5 9-5M12 12v10" /></>,
    chat: <path d="M21 12a8 8 0 01-12 7l-5 1 1-4A8 8 0 1121 12z" />,
    chevron: <path d="M9 6l6 6-6 6" />,
    chevronD: <path d="M6 9l6 6 6-6" />,
    menu: <path d="M4 6h16M4 12h16M4 18h16" />,
    x: <path d="M6 6l12 12M18 6L6 18" />,
    fb: <path d="M18 2h-3a5 5 0 00-5 5v3H7v4h3v8h4v-8h3l1-4h-4V7a1 1 0 011-1h3z" />,
    ig: <><rect x="3" y="3" width="18" height="18" rx="5" /><circle cx="12" cy="12" r="4" /><circle cx="17.5" cy="6.5" r="0.5" fill="currentColor" /></>,
    yt: <><path d="M22 8a3 3 0 00-2-2c-2-.5-8-.5-8-.5s-6 0-8 .5a3 3 0 00-2 2c-.5 2-.5 4-.5 4s0 2 .5 4a3 3 0 002 2c2 .5 8 .5 8 .5s6 0 8-.5a3 3 0 002-2c.5-2 .5-4 .5-4s0-2-.5-4z" /><path d="M10 9v6l5-3-5-3z" fill="currentColor" /></>,
    linkedin: <><rect x="3" y="3" width="18" height="18" rx="2" /><path d="M8 10v8M8 7v.01M12 18v-5a2 2 0 014 0v5M12 13v5" /></>,
  };
  return <svg {...props}>{paths[name]}</svg>;
};

/* ---------- Reveal on scroll ---------- */
function useReveal() {
  useEffect(() => {
    if (!('IntersectionObserver' in window)) return;
    document.documentElement.classList.add('js-reveal');
    const els = document.querySelectorAll('.reveal');
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting) {
          e.target.classList.add('in');
          io.unobserve(e.target);
        }
      });
    }, { threshold: 0.05, rootMargin: '40px' });
    els.forEach(el => io.observe(el));
    // Safety: force visible after 1.2s no matter what
    const t = setTimeout(() => {
      document.querySelectorAll('.reveal:not(.in)').forEach(el => el.classList.add('in'));
    }, 1200);
    return () => { io.disconnect(); clearTimeout(t); };
  }, []);
}

/* ---------- Logo (full lockup — overridden by layout.jsx CMS-aware version) ---------- */
const Logo = () => {
  const _g = window.__CMS__?.content;
  const name    = _g?.general?.companyName    || 'א. רובין דגלים';
  const tagline = _g?.general?.companyTagline || 'ייצור ומיתוג דגלים';
  const logoUrl = _g?.general?.logoUrl        || null;
  return (
    <a href="index.html" className="logo">
      <span className="logo-mark">
        {logoUrl
          ? <img src={logoUrl} alt={name} style={{ width: '100%', height: '100%', objectFit: 'contain' }} />
          : <BrandLogo size={44} />}
      </span>
      <span className="logo-text">
        <strong>{name}</strong>
        <small>{tagline}</small>
      </span>
    </a>
  );
};

/* ---------- Stars ---------- */
const Stars = ({ count = 5 }) => (
  <div className="t-stars" aria-label={`${count} כוכבים`}>
    {Array.from({ length: count }).map((_, i) => <Icon key={i} name="starFilled" size={16} />)}
  </div>
);

Object.assign(window, { Icon, useReveal, Logo, Stars });
