/**
 * ui.jsx — small shared building blocks used by every section:
 * the icon set, the language hook, the section heading pattern and
 * the image placeholder. Keeping them here avoids repeating markup.
 */

/** Reads the language context provided in app.jsx.
 *  Returns { lang, setLang, t } where t is the active translation tree. */
function useLang() {
  return React.useContext(window.LangContext);
}

/**
 * Icon — single inline-SVG icon set (24×24, stroke-based).
 * One component instead of an icon font keeps the download dependency-free
 * and lets CSS color icons via `currentColor`.
 */
function Icon({ name, size = 22 }) {
  const paths = {
    pin: <path d="M12 21s-7-5.5-7-11a7 7 0 1 1 14 0c0 5.5-7 11-7 11zm0-8.5a2.5 2.5 0 1 0 0-5 2.5 2.5 0 0 0 0 5z" />,
    phone: <path d="M5 4h4l2 5-2.5 1.5a12 12 0 0 0 5 5L15 13l5 2v4a2 2 0 0 1-2 2A16 16 0 0 1 3 6a2 2 0 0 1 2-2z" />,
    mail: <path d="M4 6h16a1 1 0 0 1 1 1v10a1 1 0 0 1-1 1H4a1 1 0 0 1-1-1V7a1 1 0 0 1 1-1zm0 2l8 6 8-6" />,
    clock: <path d="M12 21a9 9 0 1 1 0-18 9 9 0 0 1 0 18zm0-14v5l3.5 2" />,
    key: <path d="M14 10a4 4 0 1 0-3.5 3.97L12 15.5l1.5.5.5 1.5 1.5.5.5 1.5 2 .5.5-2-4.03-4.03A4 4 0 0 0 14 10zm-3-1.5a1.5 1.5 0 1 1-3 0 1.5 1.5 0 0 1 3 0z" transform="rotate(45 12 12)" />,
    lock: <path d="M7 11V8a5 5 0 0 1 10 0v3m-12 0h14v9H5v-9zm7 3.5v2.5" />,
    shield: <path d="M12 3l8 3v6c0 4.5-3.5 7.5-8 9-4.5-1.5-8-4.5-8-9V6l8-3zm-3 9l2.2 2.2L15.5 10" />,
    car: <path d="M5 16l1.5-5.5A2 2 0 0 1 8.4 9h7.2a2 2 0 0 1 1.9 1.5L19 16m-14 0h14m-14 0v3h2.5v-1.5m11.5-1.5v3h-2.5v-1.5M7.5 13.5h.01m9 0h.01" />,
    safe: <path d="M4 5h16v14H4V5zm3 16v1m10-1v1M12 12m-3.5 0a3.5 3.5 0 1 0 7 0 3.5 3.5 0 1 0-7 0m3.5-5.5v2m0 7v2m-5.5-5.5h2m7 0h2" />,
    siren: <path d="M7 18v-5a5 5 0 0 1 10 0v5m-13 0h16v2.5H4V18zM12 3v2M4.5 6.5L6 8m13.5-1.5L18 8" />,
    star: <path d="M12 3l2.7 5.6 6.1.8-4.5 4.3 1.1 6-5.4-2.9-5.4 2.9 1.1-6L3.2 9.4l6.1-.8L12 3z" fill="currentColor" stroke="none" />,
    check: <path d="M5 13l4 4 10-10" />,
    chevron: <path d="M6 9l6 6 6-6" />,
    menu: <path d="M4 7h16M4 12h16M4 17h16" />,
    close: <path d="M6 6l12 12M18 6L6 18" />,
    arrow: <path d="M5 12h14m-6-6l6 6-6 6" />,
    camera: <path d="M4 8h3l2-3h6l2 3h3v11H4V8zm8 8.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7z" />,
    whatsapp: (
      <path
        d="M12 3a9 9 0 0 0-7.8 13.5L3 21l4.7-1.2A9 9 0 1 0 12 3zm4.2 12.2c-.2.6-1.2 1.1-1.7 1.1-.4.1-1 .1-1.6-.1-2.5-.8-4.6-2.9-5.5-5.1-.3-.7-.3-1.4 0-1.9.2-.4.6-1 1-1 .3 0 .7.6 1 1.2.2.4.4.9.3 1.1-.1.3-.5.7-.5 1s1 1.6 1.8 2.3c.7.6 1.6 1.1 1.9 1 .3 0 .7-.5 1-.7.3-.3.6-.2 1 0 .4.1 1.2.6 1.3.9.1.2.1.7 0 1.2z"
        fill="currentColor"
        stroke="none"
      />
    ),
  };

  return (
    <svg
      width={size}
      height={size}
      viewBox="0 0 24 24"
      fill="none"
      stroke="currentColor"
      strokeWidth="1.8"
      strokeLinecap="round"
      strokeLinejoin="round"
      aria-hidden="true"
    >
      {paths[name]}
    </svg>
  );
}

/** SectionHead — the repeated kicker/title/lead heading pattern. */
function SectionHead({ kicker, title, lead, center }) {
  return (
    <div className={"section-head" + (center ? " section-head--center" : "")}>
      <span className="kicker">{kicker}</span>
      <h2>{title}</h2>
      {lead && <p className="lead">{lead}</p>}
    </div>
  );
}

/**
 * Placeholder — a clearly-marked slot for the owner's real photos.
 * Swap each instance for an <img> with descriptive alt text before launch
 * (see README → "Replacing placeholders").
 */
function Placeholder({ label, variant = "" }) {
  return (
    <div className={"placeholder " + variant} role="img" aria-label={label}>
      <Icon name="camera" size={30} />
      <span>{label}</span>
    </div>
  );
}

/* Share with the other Babel-compiled files (each gets its own scope). */
Object.assign(window, { useLang, Icon, SectionHead, Placeholder });

/**
 * RevealObserver — hooks an IntersectionObserver that stays alive across
 * React re-renders (language switches, etc.). A MutationObserver watches
 * the document for new .reveal elements and feeds them in automatically.
 */
(function setupRevealObserver() {
  if (typeof IntersectionObserver === "undefined") return;

  var io = new IntersectionObserver(
    function (entries) {
      entries.forEach(function (entry) {
        if (entry.isIntersecting) {
          entry.target.classList.add("revealed");
          io.unobserve(entry.target);
        }
      });
    },
    { threshold: 0.12, rootMargin: "0px 0px -40px 0px" }
  );

  function scan() {
    document.querySelectorAll(".reveal:not(.revealed)").forEach(function (el) {
      io.observe(el);
    });
  }

  /* Watch for DOM mutations — React re-renders trigger new .reveal nodes */
  if (typeof MutationObserver !== "undefined") {
    var mo = new MutationObserver(function () { setTimeout(scan, 60); });
    var root = document.getElementById("root") || document.body;
    mo.observe(root, { childList: true, subtree: true });
  }

  /* Initial scan once React has rendered */
  if (document.readyState === "loading") {
    document.addEventListener("DOMContentLoaded", function () { setTimeout(scan, 200); });
  } else {
    setTimeout(scan, 200);
  }
  setTimeout(scan, 600);
})();
