/* global React, useUI, useWishlist, Placeholder, HeartIcon, Arr, waLink */ const { useEffect, useState } = React; /* Descubre fotos secundarias igual que en ficha.jsx */ function useModalFotos(p) { const [fotos, setFotos] = useState([]); useEffect(() => { if (!p?.foto) { setFotos(p ? [] : []); return; } setFotos([p.foto]); const base = p.foto.replace(/\.[^.]+$/, "").replace(/_\d+$/, ""); const ext = p.foto.match(/\.([^.?]+)(\?|$)/)?.[1] || "jpg"; const checks = Array.from({ length: 6 }, (_, i) => { const url = `${base}_${i + 2}.${ext}`; return fetch(url, { method: "HEAD" }).then(r => r.ok ? url : null).catch(() => null); }); Promise.all(checks).then(results => { setFotos([p.foto, ...results.filter(Boolean)]); }); }, [p?.id]); return fotos; } function Modal() { const ui = useUI(); const wl = useWishlist(); const p = ui.modal; const [imgIdx, setImgIdx] = useState(0); const fotos = useModalFotos(p); useEffect(() => { setImgIdx(0); }, [p?.id]); useEffect(() => { if (!p) return; const onKey = (e) => { if (e.key === "Escape") ui.closeModal(); if (e.key === "ArrowRight") setImgIdx(i => Math.min(i + 1, fotos.length - 1)); if (e.key === "ArrowLeft") setImgIdx(i => Math.max(i - 1, 0)); }; window.addEventListener("keydown", onKey); document.body.style.overflow = "hidden"; return () => { window.removeEventListener("keydown", onKey); document.body.style.overflow = ""; }; }, [p, fotos.length]); if (!p) return null; const isOn = wl.has(p.id); const name = p.name || p.nombre || ""; const photo = fotos[imgIdx] || p.photo || p.foto || null; const price = p.price || (p.precio ? p.precio.toLocaleString("es-CO") : ""); const family = p.family || (p.categoria ? (window.CAT_LABELS && window.CAT_LABELS[p.categoria]) || p.categoria : ""); const pattern = p.pattern || (typeof detectarPatron === "function" ? detectarPatron(p.variante) : null) || ""; const variant = p.variante || ""; const wmsg = `Hola DAVA'S, me interesa la ${name}${pattern ? ` patrón ${pattern}` : ""}${price ? ` ($${price})` : ""}. ¿Me cuentan disponibilidad?`; return (