import{r as f}from"./index-C4inJ1rU.js";import{b as m,d as s}from"./maplibre-gl-ClZLWYTA.js";function g(t,e){if(!t)throw new Error(e)}let L=0;function S(t,e,a){if(t.style&&t.style._loaded){const n={...a};return delete n.id,delete n.children,t.addSource(e,n),t.getSource(e)}return null}function h(t,e,a){var i,c,d;g(e.id===a.id,"source id changed"),g(e.type===a.type,"source type changed");let n="",o=0;for(const y in e)y!=="children"&&y!=="id"&&!s(a[y],e[y])&&(n=y,o++);if(!o)return;const l=e.type;if(l==="geojson")t.setData(e.data);else if(l==="image")t.updateImage({url:e.url,coordinates:e.coordinates});else switch(n){case"coordinates":(i=t.setCoordinates)==null||i.call(t,e.coordinates);break;case"url":(c=t.setUrl)==null||c.call(t,e.url);break;case"tiles":(d=t.setTiles)==null||d.call(t,e.tiles);break;default:console.warn(`Unable to update <Source> prop: ${n}`)}}function E(t){const e=f.useContext(m).map.getMap(),a=f.useRef(t),[,n]=f.useState(0),o=f.useMemo(()=>t.id||`jsx-source-${L++}`,[]);f.useEffect(()=>{if(e){const i=()=>setTimeout(()=>n(c=>c+1),0);return e.on("styledata",i),i(),()=>{var c;if(e.off("styledata",i),e.style&&e.style._loaded&&e.getSource(o)){const d=(c=e.getStyle())==null?void 0:c.layers;if(d)for(const y of d)y.source===o&&e.removeLayer(y.id);e.removeSource(o)}}}},[e]);let l=e&&e.style&&e.getSource(o);return l?h(l,t,a.current):l=S(e,o,t),a.current=t,l&&f.Children.map(t.children,i=>i&&f.cloneElement(i,{source:o}))||null}function b(t,e,a,n){if(g(a.id===n.id,"layer id changed"),g(a.type===n.type,"layer type changed"),a.type==="custom"||n.type==="custom")return;const{layout:o={},paint:l={},filter:i,minzoom:c,maxzoom:d,beforeId:y}=a;if(y!==n.beforeId&&t.moveLayer(e,y),o!==n.layout){const u=n.layout||{};for(const r in o)s(o[r],u[r])||t.setLayoutProperty(e,r,o[r]);for(const r in u)o.hasOwnProperty(r)||t.setLayoutProperty(e,r,void 0)}if(l!==n.paint){const u=n.paint||{};for(const r in l)s(l[r],u[r])||t.setPaintProperty(e,r,l[r]);for(const r in u)l.hasOwnProperty(r)||t.setPaintProperty(e,r,void 0)}s(i,n.filter)||t.setFilter(e,i),(c!==n.minzoom||d!==n.maxzoom)&&t.setLayerZoomRange(e,c,d)}function x(t,e,a){if(t.style&&t.style._loaded&&(!("source"in a)||t.getSource(a.source))){const n={...a,id:e};delete n.beforeId,t.addLayer(n,a.beforeId)}}let k=0;function I(t){const e=f.useContext(m).map.getMap(),a=f.useRef(t),[,n]=f.useState(0),o=f.useMemo(()=>t.id||`jsx-layer-${k++}`,[]);if(f.useEffect(()=>{if(e){const i=()=>n(c=>c+1);return e.on("styledata",i),i(),()=>{e.off("styledata",i),e.style&&e.style._loaded&&e.getLayer(o)&&e.removeLayer(o)}}},[e]),e&&e.style&&e.getLayer(o))try{b(e,o,t,a.current)}catch(i){console.warn(i)}else x(e,o,t);return a.current=t,null}export{I as L,E as S};
