/* Pomona Berries — Mapa de operación (estados, sin direcciones). Exports window.POpsMap. */
(function () {
  const { useEffect, useRef } = React;
  const POINTS = [
    { type: 'farm', lat: 22.15, lng: -100.98, es: 'Ranchos · San Luis Potosí', en: 'Farms · San Luis Potosí' },
    { type: 'farm', lat: 19.57, lng: -101.71, es: 'Ranchos · Michoacán (berries, aguacate, mango)', en: 'Farms · Michoacán (berries, avocado, mango)' },
    { type: 'farm', lat: 19.36, lng: -99.72,  es: 'Ranchos · Estado de México', en: 'Farms · State of Mexico' },
    { type: 'farm', lat: 36.78, lng: -119.42, es: 'Ranchos · California, EE.UU.', en: 'Farms · California, USA' },
    { type: 'cold', lat: 19.37, lng: -99.09, es: 'Centro de frío · Ciudad de México', en: 'Cold storage · Mexico City' },
    { type: 'cold', lat: 19.98, lng: -102.28, es: 'Centro de frío · Zamora, Michoacán', en: 'Cold storage · Zamora, Michoacán' },
    { type: 'border', lat: 32.53, lng: -117.03, es: 'Cruce a EE.UU. · Tijuana, Baja California', en: 'US border crossing · Tijuana, Baja California' },
    { type: 'border', lat: 27.49, lng: -99.51, es: 'Cruce a EE.UU. · Nuevo Laredo, Tamaulipas', en: 'US border crossing · Nuevo Laredo, Tamaulipas' },
    { type: 'region', lat: 19.18, lng: -96.14, es: 'Abasto · Veracruz (limón, chayote, taro)', en: 'Sourcing · Veracruz (limes, chayote, taro)' },
    { type: 'region', lat: 17.07, lng: -96.72, es: 'Abasto · Oaxaca (limón persa)', en: 'Sourcing · Oaxaca (Persian lime)' },
    { type: 'region', lat: 19.12, lng: -104.0, es: 'Abasto · Colima (limón mexicano)', en: 'Sourcing · Colima (Mexican lime)' },
    { type: 'office', lat: 19.40, lng: -99.26, es: 'Oficinas · Ciudad de México', en: 'Offices · Mexico City' },
    { type: 'office', lat: 20.67, lng: -103.35, es: 'Oficinas · Guadalajara', en: 'Offices · Guadalajara' },
    { type: 'office', lat: 25.77, lng: -80.19, es: 'Oficinas · Miami', en: 'Offices · Miami' },
    { type: 'source', lat: 4.57, lng: -74.30, es: 'Importación · Colombia (golden berry)', en: 'Import · Colombia (golden berry)' },
    { type: 'source', lat: 27.99, lng: -81.76, es: 'Importación · Florida', en: 'Import · Florida' },
    { type: 'source', lat: 46.60, lng: 2.21, es: 'Importación · Francia', en: 'Import · France' },
    { type: 'market', lat: 50.85, lng: 4.35, es: 'Mercado · Europa', en: 'Market · Europe' },
    { type: 'market', lat: 39.0, lng: -98.0, es: 'Mercado · Estados Unidos', en: 'Market · United States' },
  ];
  const STYLE = {
    farm:   { color: '#2FA84F', es: 'Ranchos propios', en: 'Own farms' },
    cold:   { color: '#3D52C9', es: 'Centros de frío', en: 'Cold storage' },
    border: { color: '#8C6BC8', es: 'Cruces a EE.UU.', en: 'US border crossings' },
    region: { color: '#9BBE4C', es: 'Regiones de abasto en México', en: 'Sourcing regions in Mexico' },
    office: { color: '#2C171C', es: 'Oficinas', en: 'Offices' },
    source: { color: '#E8881C', es: 'Orígenes de importación', en: 'Import origins' },
    market: { color: '#E63B55', es: 'Mercados', en: 'Markets' },
  };
  function loadLeaflet() {
    if (window.L) return Promise.resolve(window.L);
    if (!document.getElementById('leaflet-css')) {
      const l = document.createElement('link'); l.id = 'leaflet-css'; l.rel = 'stylesheet';
      l.href = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.css'; document.head.appendChild(l);
    }
    if (window.__leafletLoading) return window.__leafletLoading;
    window.__leafletLoading = new Promise((res, rej) => {
      const s = document.createElement('script'); s.src = 'https://unpkg.com/leaflet@1.9.4/dist/leaflet.js';
      s.onload = () => res(window.L); s.onerror = rej; document.head.appendChild(s);
    });
    return window.__leafletLoading;
  }
  window.POpsMap = function POpsMap({ lang, height, zoom }) {
    const ref = useRef(null);
    useEffect(() => {
      let map;
      loadLeaflet().then((L) => {
        if (!ref.current || ref.current._leaflet_id) return;
        map = L.map(ref.current, { scrollWheelZoom: false, attributionControl: true });
        L.tileLayer('https://server.arcgisonline.com/ArcGIS/rest/services/Canvas/World_Light_Gray_Base/MapServer/tile/{z}/{y}/{x}', { attribution: 'Tiles &copy; Esri, HERE, Garmin, &copy; OpenStreetMap contributors', maxZoom: 10 }).addTo(map);
        const bounds = [];
        POINTS.forEach((p) => {
          const st = STYLE[p.type];
          const big = p.type === 'farm' || p.type === 'cold';
          L.circleMarker([p.lat, p.lng], { radius: big ? 9 : 6, color: '#fff', weight: 2, fillColor: st.color, fillOpacity: 0.95 })
            .addTo(map).bindTooltip(p[lang], { direction: 'top', offset: [0, -8] });
          if (p.type !== 'market' && p.type !== 'source') bounds.push([p.lat, p.lng]);
        });
        map.fitBounds(bounds, { padding: [30, 30], maxZoom: zoom || 5 });
      }).catch(() => {});
      return () => { if (map) map.remove(); };
    }, [lang]);
    return (
      <div className="p-opsmap">
        <div ref={ref} style={{ height: height || 440, width: '100%', borderRadius: 'var(--img-radius)', overflow: 'hidden', background: '#f3efe9' }} />
        <div className="p-opsmap-legend">
          {Object.keys(STYLE).map((k) => <span key={k}><i style={{ background: STYLE[k].color }} /> {STYLE[k][lang]}</span>)}
        </div>
      </div>
    );
  };
})();
