// Primitive components (icons, placeholders, reveal wrapper)
const { useEffect, useRef, useState } = React;

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

function Placeholder({ label, dark, style }){
  return (
    <div className={`ph ${dark ? "dark" : ""}`} style={style}>
      <div className="ph-label">{label}</div>
    </div>
  );
}

function WorkImage({ src, phLabel }){
  const showPlaceholders = window.TWEAKS?.showPlaceholders !== false;
  if(src) return <img src={src} alt="" loading="lazy" />;
  if(showPlaceholders) return <Placeholder label={phLabel || "施工写真"} />;
  return <Placeholder label={phLabel || "photo"} />;
}

// --- Icons for services ---
const icons = {
  roof: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 24 L24 10 L42 24" />
      <path d="M10 24 L10 40 L38 40 L38 24" />
      <path d="M20 40 L20 30 L28 30 L28 40" />
      <path d="M10 28 L38 28" strokeDasharray="2 3" opacity="0.4"/>
    </svg>
  ),
  paint: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <rect x="10" y="8" width="24" height="12" />
      <path d="M22 20 L22 26 L26 26 L26 38" />
      <rect x="20" y="38" width="8" height="6" />
      <path d="M10 14 L6 14 L6 8" />
    </svg>
  ),
  interior: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <path d="M6 40 L24 24 L42 40" />
      <path d="M10 38 L10 42 L38 42 L38 38" />
      <rect x="20" y="30" width="8" height="12" />
      <circle cx="26" cy="36" r="0.8" fill="currentColor"/>
    </svg>
  ),
  water: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <path d="M24 8 C30 18, 34 22, 34 30 a10 10 0 0 1 -20 0 C14 22, 18 18, 24 8 Z" />
      <path d="M20 28 C20 32, 22 34, 24 34" opacity="0.5"/>
    </svg>
  ),
  demolish: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <path d="M8 40 L14 14 L20 14 L22 34 L28 22 L34 30 L40 40" />
      <path d="M6 42 L42 42" />
      <path d="M16 18 L18 18 M20 22 L22 22 M26 28 L28 28" opacity="0.5"/>
    </svg>
  ),
  repair: (
    <svg viewBox="0 0 48 48" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round">
      <path d="M30 8 a8 8 0 0 0 -10 10 L10 28 a3 3 0 0 0 4 4 L24 22 a8 8 0 0 0 10 -10 L30 16 L26 12 Z"/>
      <path d="M28 22 L38 32 a3 3 0 0 1 -4 4 L24 26" />
    </svg>
  ),
};
const iconByIdx = ["roof","paint","interior","water","demolish","repair"];

const Phone = (p) => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round" {...p}>
    <path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3.1 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3.1-8.7A2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7 12.8 12.8 0 0 0 .7 2.8 2 2 0 0 1-.5 2.1L8 10a16 16 0 0 0 6 6l1.4-1.3a2 2 0 0 1 2.1-.5 12.8 12.8 0 0 0 2.8.7 2 2 0 0 1 1.7 2Z"/>
  </svg>
);

const Arrow = (p) => (
  <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1" strokeLinecap="round" strokeLinejoin="round" {...p}>
    <path d="M5 12h14"/><path d="M13 6l6 6-6 6"/>
  </svg>
);

Object.assign(window, { Reveal, Placeholder, WorkImage, icons, iconByIdx, Phone, Arrow });
