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