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

// Brand mark SVG (logo shapes)
const BrandMark = ({ size = 28, className = "" }) => (
  <svg className={className} width={size} height={size} viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg">
    <polygon points="18,72 42,32 66,72" fill="#6FAF2A"/>
    <circle cx="55" cy="35" r="17" fill="#F5D938"/>
    <circle cx="72" cy="60" r="15" fill="#9ED4E2"/>
  </svg>
);

// Simple check icon
const Check = ({ color = "#6FAF2A", size = 16 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color} strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
    <polyline points="20 6 9 17 4 12"/>
  </svg>
);

// Small shape icons for brand mark decoration
const ShapeCircle = ({ color, size = 22 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24"><circle cx="12" cy="12" r="10" fill={color}/></svg>
);
const ShapeTri = ({ color, size = 22 }) => (
  <svg width={size} height={size} viewBox="0 0 24 24"><polygon points="12,3 22,20 2,20" fill={color}/></svg>
);

// Scroll reveal wrapper
const Reveal = ({ children, delay = 0, as: Tag = 'div', ...props }) => {
  const ref = useRef(null);
  const [shown, setShown] = useState(false);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting) { setShown(true); io.disconnect(); }
    }, { threshold: 0.12 });
    io.observe(el);
    return () => io.disconnect();
  }, []);
  return (
    <Tag ref={ref} className={`reveal ${shown ? 'in' : ''} ${props.className || ''}`}
      style={{ ...(props.style || {}), transitionDelay: delay + 'ms' }}>
      {children}
    </Tag>
  );
};

// Animated count-up
const Count = ({ to, suffix = '', duration = 1200, decimals = 0 }) => {
  const ref = useRef(null);
  const [val, setVal] = useState(0);
  const started = useRef(false);
  useEffect(() => {
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver(([e]) => {
      if (e.isIntersecting && !started.current) {
        started.current = true;
        const start = performance.now();
        const tick = (t) => {
          const p = Math.min(1, (t - start) / duration);
          const eased = 1 - Math.pow(1 - p, 3);
          setVal(to * eased);
          if (p < 1) requestAnimationFrame(tick);
          else setVal(to);
        };
        requestAnimationFrame(tick);
      }
    }, { threshold: 0.4 });
    io.observe(el);
    return () => io.disconnect();
  }, [to, duration]);
  const displayed = decimals > 0 ? val.toFixed(decimals) : Math.round(val).toLocaleString('en-US');
  return <span ref={ref}>{displayed}{suffix}</span>;
};

// Small line icons for pricing feature lists
const FEAT_ICON_PATHS = {
  cal: <><rect x="3" y="4" width="18" height="18" rx="3"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/></>,
  card: <><rect x="2" y="5" width="20" height="14" rx="3"/><line x1="2" y1="10" x2="22" y2="10"/></>,
  globe: <><circle cx="12" cy="12" r="9"/><path d="M3 12h18"/><path d="M12 3a13.5 13.5 0 0 1 0 18a13.5 13.5 0 0 1 0-18"/></>,
  key: <><circle cx="8" cy="15" r="4"/><path d="m10.8 12.2 8.2-8.2"/><path d="m15 5 3 3"/><path d="m17.5 7.5 2-2"/></>,
  link: <><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/></>,
  home: <><path d="M3 11.5 12 4l9 7.5"/><path d="M5 10v10h14V10"/><path d="M10 20v-6h4v6"/></>,
  chart: <><line x1="18" y1="20" x2="18" y2="10"/><line x1="12" y1="20" x2="12" y2="4"/><line x1="6" y1="20" x2="6" y2="14"/></>,
  users: <><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/></>,
  sparkle: <><path d="M12 3v4M12 17v4M3 12h4M17 12h4M5.6 5.6l2.8 2.8M15.6 15.6l2.8 2.8M5.6 18.4l2.8-2.8M15.6 8.4l2.8-2.8"/></>,
  heart: <><path d="M19 14c1.5-1.5 2-3.2 2-4.5A4.5 4.5 0 0 0 16.5 5c-1.8 0-3.4 1-4.5 2.6C10.9 6 9.3 5 7.5 5A4.5 4.5 0 0 0 3 9.5c0 1.3.5 3 2 4.5l7 7z"/></>,
};
const FeatIcon = ({ name, size = 16 }) => (
  <svg className="feat-icon" width={size} height={size} viewBox="0 0 24 24" fill="none"
    stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round">
    {FEAT_ICON_PATHS[name] || FEAT_ICON_PATHS.sparkle}
  </svg>
);

Object.assign(window, { BrandMark, Check, ShapeCircle, ShapeTri, Reveal, Count, FeatIcon });
