import{i as e}from"./chunk-aKtaBQYM.js";import{Vt as t,Y as n,zt as r}from"./mui-B6lh3XSP.js";import{A as i,Ct as a,I as o,P as s,Q as c,R as l,et as u,ht as d,j as f,k as p}from"./index-KVuxnkWj.js";var m=e(t(),1),h=({positions:e,onMapClick:t,onMarkerClick:h,showStatus:g,selectedPosition:_,titleField:v,disabled:y})=>{let b=(0,m.useId)(),x=`${b}-clusters`,S=`${b}-selected`,C=l(`iconScale`,a(n().breakpoints.up(`md`))?.75:1),w=r(e=>e.devices.items),T=r(e=>e.devices.selectedId),E=l(`mapCluster`,!0),D=l(`mapDirection`,`selected`),O=(0,m.useRef)(y);O.current=y;let k=(0,m.useCallback)((e,t,n)=>{let r=e[t.deviceId],i;switch(D){case`none`:i=!1;break;case`all`:i=t.course>0;break;default:i=n===t.id&&t.course>0;break}return{id:t.id,deviceId:t.deviceId,name:r.name,fixTime:c(t.fixTime,`seconds`),category:p(r.category),color:g?t.attributes.color||u(r.status):`neutral`,rotation:t.course,direction:i}},[D,g]),A=()=>o.getCanvas().style.cursor=`pointer`,j=()=>o.getCanvas().style.cursor=``,M=(0,m.useCallback)(e=>{if(!e.defaultPrevented&&t){let[n,r]=f(e.lngLat.lng,e.lngLat.lat);t(r,n)}},[t]),N=(0,m.useCallback)(e=>{if(O.current)return;e.preventDefault();let t=e.features[0];h&&h(t.properties.id,t.properties.deviceId)},[h]),P=d(async e=>{if(O.current)return;e.preventDefault();let t=o.queryRenderedFeatures(e.point,{layers:[x]}),n=t[0].properties.cluster_id,r=await o.getSource(b).getClusterExpansionZoom(n);o.easeTo({center:t[0].geometry.coordinates,zoom:r})},[x,b]);return(0,m.useEffect)(()=>(o.addSource(b,{type:`geojson`,data:{type:`FeatureCollection`,features:[]},cluster:E,clusterMaxZoom:14,clusterRadius:50}),o.addSource(S,{type:`geojson`,data:{type:`FeatureCollection`,features:[]}}),[b,S].forEach(e=>{o.addLayer({id:e,type:`symbol`,source:e,filter:[`!has`,`point_count`],layout:{"icon-image":`{category}-{color}`,"icon-size":C,"icon-allow-overlap":!0,"text-field":`{${v||`name`}}`,"text-allow-overlap":!0,"text-anchor":`bottom`,"text-offset":[0,-2*C],"text-font":i(o),"text-size":12,"symbol-sort-key":[`get`,`id`]},paint:{"text-halo-color":`white`,"text-halo-width":1}}),o.addLayer({id:`direction-${e}`,type:`symbol`,source:e,filter:[`all`,[`!has`,`point_count`],[`==`,`direction`,!0]],layout:{"icon-image":`direction`,"icon-size":C,"icon-allow-overlap":!0,"icon-rotate":[`get`,`rotation`],"icon-rotation-alignment":`map`}}),o.on(`mouseenter`,e,A),o.on(`mouseleave`,e,j),o.on(`click`,e,N)}),o.addLayer({id:x,type:`symbol`,source:b,filter:[`has`,`point_count`],layout:{"icon-image":`background`,"icon-size":C,"text-field":`{point_count_abbreviated}`,"text-font":i(o),"text-size":14}}),o.on(`mouseenter`,x,A),o.on(`mouseleave`,x,j),o.on(`click`,x,P),o.on(`click`,M),()=>{o.off(`mouseenter`,x,A),o.off(`mouseleave`,x,j),o.off(`click`,x,P),o.off(`click`,M),o.getLayer(x)&&o.removeLayer(x),[b,S].forEach(e=>{o.off(`mouseenter`,e,A),o.off(`mouseleave`,e,j),o.off(`click`,e,N),o.getLayer(e)&&o.removeLayer(e),o.getLayer(`direction-${e}`)&&o.removeLayer(`direction-${e}`),o.getSource(e)&&o.removeSource(e)})}),[E,x,N,P,M,C,b,S,v]),(0,m.useEffect)(()=>{[b,S].forEach(t=>{o.getSource(t)?.setData({type:`FeatureCollection`,features:e.filter(e=>w.hasOwnProperty(e.deviceId)).filter(e=>t===b?e.deviceId!==T:e.deviceId===T).map(e=>({type:`Feature`,geometry:{type:`Point`,coordinates:s(e.longitude,e.latitude)},properties:k(w,e,_&&_.id)}))})})},[E,x,h,P,w,e,_,k,b,S,T]),null};export{h as t};