const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["assets/leaflet.markercluster-src-DGIDBsw0.js","assets/index-D4ZMoJFe.js","assets/index-DBNxJxvx.css"])))=>i.map(i=>d[i]);
import{e as B,u as ge,v as je,a as Ne,i as _e,r as l,aj as o,d as E,j as e,ak as ye,a3 as ve,a1 as Y,al as ee,X as te,am as ke}from"./index-D4ZMoJFe.js";import{c as we,l as Se,M as Ce,T as Te,L as S}from"./TileLayer-BRQ5IQYZ.js";import{b as ae}from"./formatters-jmx2pV9o.js";import{i as Ae}from"./trackingUtils-D2VhIKbr.js";import{P as Me,C as Ie,T as Re}from"./Tooltip-UyA4P4hO.js";import{M as ze}from"./map-pinned-C9hS4GhO.js";import{u as le}from"./hooks-Cn4iFLpH.js";/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const $e=B("Battery",[["rect",{width:"16",height:"10",x:"2",y:"7",rx:"2",ry:"2",key:"1w10f2"}],["line",{x1:"22",x2:"22",y1:"11",y2:"13",key:"4dh1rd"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const De=B("Crosshair",[["circle",{cx:"12",cy:"12",r:"10",key:"1mglay"}],["line",{x1:"22",x2:"18",y1:"12",y2:"12",key:"l9bcsi"}],["line",{x1:"6",x2:"2",y1:"12",y2:"12",key:"13hhkx"}],["line",{x1:"12",x2:"12",y1:"6",y2:"2",key:"10w3f3"}],["line",{x1:"12",x2:"12",y1:"22",y2:"18",key:"15g9kq"}]]);/**
 * @license lucide-react v0.469.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const Le=B("Phone",[["path",{d:"M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z",key:"foiqr5"}]]),Pe=we(function(h){return new Se.Control.Zoom(h)}),Oe=[36.7,3],Ee="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",Be="https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}",se=(s,h=Date.now())=>{if(!s)return"Jamais";const b=new Date(s),g=Math.max(0,Math.floor((h-b.getTime())/1e3));if(g<60)return`il y a ${g}s`;const d=Math.floor(g/60);if(d<60)return`il y a ${d} min`;const c=Math.floor(d/60);return c<24?`il y a ${c} h`:ae(s)},ne=s=>s==null?"—":`${s}%`,re=s=>(s==null?void 0:s.last_lat)!==null&&(s==null?void 0:s.last_lat)!==void 0&&(s==null?void 0:s.last_lng)!==null&&(s==null?void 0:s.last_lng)!==void 0?[Number(s.last_lat),Number(s.last_lng)]:null,Ge=s=>S.divIcon({className:"tracking-marker-shell",html:`<div class="tracking-device-marker ${s.ui_is_online?"online":"offline"} ${s.ui_is_online&&Number(s.last_speed||0)>2?"moving":""}">📱</div>`,iconSize:s.ui_is_online?[40,40]:[30,30],iconAnchor:s.ui_is_online?[20,20]:[15,15]});function Ve({devices:s,selectedDeviceId:h,onSelect:b,fitRequest:g}){const d=le(),c=l.useRef(null),_=l.useRef(new Map),y=l.useRef(!1),[j,A]=l.useState(!1);return l.useEffect(()=>{let m=!0;return globalThis.L=S,ke(()=>import("./leaflet.markercluster-src-DGIDBsw0.js").then(u=>u.l),__vite__mapDeps([0,1,2])).then(()=>{if(!m)return;const u=S.markerClusterGroup({showCoverageOnHover:!1,maxClusterRadius:50,spiderfyOnMaxZoom:!0,iconCreateFunction(v){return S.divIcon({html:`<div class="tracking-cluster">${v.getChildCount()}</div>`,className:"tracking-cluster-shell",iconSize:[46,46]})}});c.current=u,d.addLayer(u),A(!0)}),()=>{m=!1,_.current.clear(),c.current&&d.removeLayer(c.current),c.current=null,A(!1)}},[d]),l.useEffect(()=>{if(!j||!c.current)return;_.current.clear(),c.current.clearLayers();const m=s.map(u=>{const v=re(u);if(!v)return null;const x=S.marker(v,{icon:Ge(u),zIndexOffset:u.id===h?1e3:0});return x.bindTooltip(u.device_name,{direction:"top",offset:[0,-16]}),x.on("click",()=>b(u.id,!0)),_.current.set(u.id,x),x}).filter(Boolean);m.length&&c.current.addLayers(m),!y.current&&m.length&&(y.current=!0,d.fitBounds(S.featureGroup(m).getBounds(),{padding:[48,48]}))},[j,s,d,b,h]),l.useEffect(()=>{if(!j||!g||!c.current)return;const m=Array.from(_.current.values());m.length&&d.fitBounds(S.featureGroup(m).getBounds(),{padding:[48,48]})},[j,g,d]),null}function Fe({selectedDevice:s,focusRequest:h}){const b=le();return l.useEffect(()=>{if(!s)return;const g=re(s);g&&b.flyTo(g,Math.max(b.getZoom(),14),{duration:.7})},[h,b,s]),null}function qe(){const s=ge(),[h,b]=je(),{selectedCompanyId:g,selectedCompany:d,activeModule:c}=Ne(),{pushToast:_}=_e(),[y,j]=l.useState([]),[A,m]=l.useState(!0),[u,v]=l.useState(""),[x,M]=l.useState(null),[G,I]=l.useState(!1),[C,R]=l.useState([]),[V,ie]=l.useState(!1),[F,oe]=l.useState(!1),[ce,U]=l.useState(!1),[D,Z]=l.useState([]),[de,me]=l.useState(0),[ue,L]=l.useState(0),[z,xe]=l.useState(Date.now()),H=l.useRef(new Map),p=g||(d==null?void 0:d.id)||"",P=h.get("device"),K=async()=>{if(!p)return;const[t,a]=await Promise.all([E.get("/tracking/devices/live-states",{params:{company_id:p}}),E.get("/tracking/alerts",{params:{company_id:p,status:"active"}})]);j(Array.isArray(t.data.items)?t.data.items:[]),Z(Array.isArray(a.data.items)?a.data.items:[])};l.useEffect(()=>{if(c!=="TRACKING"||!p)return;let t=!1;return m(!0),v(""),K().catch(a=>{var i,w;t||v(((w=(i=a==null?void 0:a.response)==null?void 0:i.data)==null?void 0:w.message)||"Impossible de charger les positions en direct.")}).finally(()=>{t||m(!1)}),()=>{t=!0}},[c,p]),l.useEffect(()=>{const t=window.setInterval(()=>{xe(Date.now())},1e3);return()=>{window.clearInterval(t)}},[]),l.useEffect(()=>{if(c!=="TRACKING"||!p)return;o.connect(),o.emit("company:subscribe",Number(p));const t=n=>{Array.isArray(n)&&n.length&&j(n)},a=n=>{const k=H.current.get(n.device_id)||0;Date.now()-k<5e3||(H.current.set(n.device_id,Date.now()),j(N=>N.map(f=>Number(f.id)===Number(n.device_id)?{...f,is_online:!0,last_lat:n.lat,last_lng:n.lng,last_accuracy:n.accuracy,last_speed:n.speed,last_battery:n.battery,last_seen:n.timestamp}:f)),Number(x)===Number(n.device_id)&&R(N=>[...N.slice(-59),{id:`${n.device_id}-${n.timestamp}`,latitude:Number(n.lat),longitude:Number(n.lng),speed:n.speed,device_timestamp:n.timestamp}]))},i=({device_id:n,device_name:k})=>{j(N=>N.map(f=>Number(f.id)===Number(n)?{...f,is_online:!0,last_seen:new Date().toISOString()}:f)),_({title:"Téléphone en ligne",description:`${k} est en ligne.`,tone:"success"})},w=({device_id:n,device_name:k})=>{j(N=>N.map(f=>Number(f.id)===Number(n)?{...f,is_online:!1}:f)),_({title:"Téléphone hors ligne",description:`${k} est hors ligne.`,tone:"warning"})},X=({device_id:n,battery:k,timestamp:N})=>{j(f=>f.map($=>Number($.id)===Number(n)?{...$,is_online:!0,last_seen:N||new Date().toISOString(),last_battery:k??$.last_battery}:$))},q=n=>{Z(k=>[n,...k.filter(N=>Number(N.id)!==Number(n.id))])},Q=n=>{_({title:(n==null?void 0:n.title)||"Batterie faible",description:(n==null?void 0:n.message)||"Une batterie faible a été détectée.",tone:"warning"})};return o.on("devices:initial_state",t),o.on("location:update",a),o.on("device:online",i),o.on("device:offline",w),o.on("device:heartbeat",X),o.on("tracking:alert",q),o.on("alert:battery_low",Q),()=>{o.off("devices:initial_state",t),o.off("location:update",a),o.off("device:online",i),o.off("device:offline",w),o.off("device:heartbeat",X),o.off("tracking:alert",q),o.off("alert:battery_low",Q),o.emit("company:unsubscribe",Number(p))}},[c,p,_,x]),l.useEffect(()=>{if(!x||!p){R([]);return}let t=!1;return E.get(`/tracking/devices/${x}/live`,{params:{company_id:p}}).then(a=>{var i;t||R(((i=a.data.item)==null?void 0:i.trail)||[])}).catch(()=>{t||R([])}),()=>{t=!0}},[p,x]),l.useEffect(()=>{if(!P){M(null),I(!1);return}if(!y.length)return;const t=Number(P);if(!Number.isFinite(t)){M(null);return}y.some(i=>Number(i.id)===t)&&(M(t),I(!0),L(i=>i+1))},[y,P]);const T=l.useMemo(()=>y.map(t=>({...t,ui_is_online:Ae(t,z)})),[y,z]),O=l.useMemo(()=>[...T].sort((t,a)=>t.ui_is_online!==a.ui_is_online?t.ui_is_online?-1:1:new Date(a.last_seen||0).getTime()-new Date(t.last_seen||0).getTime()),[T]),r=l.useMemo(()=>T.find(t=>Number(t.id)===Number(x))||null,[T,x]),fe=T.filter(t=>t.ui_is_online).length,W=D.length,J=(t,a=!1)=>{M(t),I(!0);const i=new URLSearchParams(h);i.set("device",String(t)),b(i,{replace:!0}),a&&L(w=>w+1)},he=t=>{t==null||t.stopPropagation(),I(!1);const a=new URLSearchParams(h);a.delete("device"),b(a,{replace:!0})},pe=()=>{m(!0),v(""),K().catch(t=>{var a,i;v(((i=(a=t==null?void 0:t.response)==null?void 0:a.data)==null?void 0:i.message)||"Impossible de charger les positions en direct.")}).finally(()=>{m(!1)})},be=V?Be:Ee;return e.jsx("section",{className:"-m-4 h-[calc(100vh-64px)] overflow-hidden sm:-m-6",children:e.jsxs("div",{className:"relative h-full w-full tracking-map",children:[e.jsxs(Ce,{center:Oe,zoom:6,minZoom:4,zoomControl:!1,className:"h-full w-full",children:[e.jsx(Te,{attribution:"© OpenStreetMap contributors",url:be}),e.jsx(Pe,{position:"bottomright"}),e.jsx(Ve,{devices:O,selectedDeviceId:x,onSelect:J,fitRequest:de}),e.jsx(Fe,{selectedDevice:r,focusRequest:ue}),C.length>1?e.jsxs(e.Fragment,{children:[e.jsx(Me,{positions:C.map(t=>[t.latitude,t.longitude]),pathOptions:{color:"#2563eb",weight:4,opacity:.75}}),C.map((t,a)=>e.jsx(Ie,{center:[t.latitude,t.longitude],radius:a===C.length-1?5:3,pathOptions:{color:a===C.length-1?"#2563eb":"#93c5fd",fillColor:a===C.length-1?"#2563eb":"#dbeafe",fillOpacity:1,weight:1},children:e.jsx(Re,{direction:"top",offset:[0,-8],children:e.jsxs("div",{className:"text-xs",children:[e.jsx("div",{children:ae(t.device_timestamp)}),e.jsxs("div",{children:["Vitesse: ",t.speed??"—"," km/h"]})]})})},t.id||`${t.latitude}-${t.longitude}-${a}`))]}):null]}),e.jsxs("div",{className:"pointer-events-none absolute inset-0",children:[e.jsxs("div",{className:"pointer-events-auto absolute left-4 top-4 z-[500] flex flex-wrap gap-3",children:[e.jsxs("button",{type:"button",className:"tracking-control",onClick:()=>ie(t=>!t),children:[e.jsx(ye,{size:16}),V?"Vue OSM":"Vue satellite"]}),e.jsxs("button",{type:"button",className:"tracking-control",onClick:()=>me(t=>t+1),children:[e.jsx(ze,{size:16}),"Tous visible"]}),e.jsxs("button",{type:"button",className:"tracking-control",onClick:()=>U(t=>!t),children:[e.jsx(ve,{size:16}),W," alertes"]}),e.jsx("button",{type:"button",className:"tracking-control",onClick:()=>s("/tracking/settings"),children:e.jsx(Y,{size:16})})]}),e.jsxs("aside",{className:`pointer-events-auto absolute bottom-4 left-4 top-20 z-[450] flex w-[280px] flex-col overflow-hidden tracking-overlay transition-transform duration-300 ${F?"-translate-x-[236px]":"translate-x-0"}`,children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-slate-200 px-4 py-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"tracking-panel-title",children:"Téléphones"}),e.jsxs("h2",{className:"mt-2 text-lg font-semibold text-slate-950",children:[fe," en ligne / ",y.length]})]}),e.jsx("button",{type:"button",className:"rounded-xl border border-slate-200 px-2 py-1 text-xs font-semibold text-slate-600",onClick:()=>oe(t=>!t),children:F?">":"<"})]}),e.jsx("div",{className:"flex-1 overflow-y-auto px-3 py-3",children:A?e.jsx("div",{className:"space-y-3",children:Array.from({length:6}).map((t,a)=>e.jsxs("div",{className:"animate-pulse rounded-2xl bg-slate-100 p-4",children:[e.jsx("div",{className:"h-3 w-24 rounded bg-slate-200"}),e.jsx("div",{className:"mt-3 h-3 w-36 rounded bg-slate-200"}),e.jsx("div",{className:"mt-3 h-3 w-28 rounded bg-slate-200"})]},a))}):u?e.jsxs("div",{className:"rounded-2xl border border-red-200 bg-red-50 p-4 text-sm text-red-700",children:[e.jsx("p",{children:u}),e.jsx("button",{type:"button",className:"mt-3 btn-secondary",onClick:pe,children:"Réessayer"})]}):O.length?e.jsx("div",{className:"space-y-2",children:O.map(t=>e.jsx("button",{type:"button",onClick:()=>J(t.id),className:`w-full rounded-2xl border px-4 py-3 text-left transition ${Number(t.id)===Number(x)?"border-emerald-300 bg-emerald-50 shadow-sm":"border-slate-200 bg-white hover:border-emerald-200 hover:bg-emerald-50/50"}`,children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("span",{className:`inline-block h-2.5 w-2.5 rounded-full ${t.ui_is_online?"bg-emerald-500":"bg-slate-400"}`}),e.jsxs("p",{className:"truncate font-semibold text-slate-900",children:[t.owner_name,t.ui_is_online?"":" (Hors ligne)"]})]}),e.jsx("p",{className:"mt-1 truncate text-sm text-slate-600",children:t.device_name||"Téléphone"}),e.jsxs("p",{className:"mt-1 text-xs text-slate-500",children:["🔋 ",ne(t.last_battery)," · ",se(t.last_seen,z)]})]}),e.jsx("div",{className:"text-right text-xs font-medium text-slate-500",children:t.ui_is_online&&Number(t.last_speed||0)>0?`${Math.round(Number(t.last_speed||0))} km/h`:"—"})]})},t.id))}):e.jsxs("div",{className:"flex h-full flex-col items-center justify-center px-6 text-center",children:[e.jsx(ee,{className:"h-10 w-10 text-emerald-500"}),e.jsx("p",{className:"mt-4 text-lg font-semibold text-slate-900",children:"Aucun téléphone"}),e.jsx("p",{className:"mt-2 text-sm leading-6 text-slate-500",children:"Commencez par créer des comptes appareils dans le module tracking."}),e.jsx("button",{type:"button",className:"mt-5 btn-primary",onClick:()=>s("/tracking/devices/new"),children:"Ajouter un téléphone"})]})})]}),e.jsx("aside",{className:`pointer-events-auto absolute bottom-4 right-4 top-20 z-[460] w-[320px] overflow-hidden tracking-overlay transition-transform duration-300 ${r&&G?"translate-x-0":"translate-x-[360px]"}`,children:r&&G?e.jsxs("div",{className:"flex h-full flex-col",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-slate-200 px-5 py-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"tracking-panel-title",children:"Téléphone sélectionné"}),e.jsx("h2",{className:"mt-2 text-xl font-semibold text-slate-950",children:r.device_name})]}),e.jsx("button",{type:"button",className:"rounded-xl border border-slate-200 p-2 text-slate-500 transition hover:bg-slate-50",onClick:he,children:e.jsx(te,{size:18})})]}),e.jsx("div",{className:"flex-1 overflow-y-auto px-5 py-5",children:e.jsxs("div",{className:"space-y-5 text-sm text-slate-700",children:[e.jsxs("div",{className:"space-y-1",children:[e.jsxs("div",{className:"flex items-center gap-2 text-slate-900",children:[e.jsx(ee,{size:16,className:"text-emerald-600"}),e.jsx("span",{className:"font-semibold",children:r.device_model||r.device_name})]}),e.jsx("p",{className:"font-medium text-slate-900",children:r.owner_name}),e.jsx("p",{children:r.owner_phone||"Téléphone non renseigné"}),e.jsx("p",{children:r.owner_department||"Département non renseigné"})]}),e.jsxs("div",{className:"rounded-2xl border border-slate-200 bg-slate-50 p-4",children:[e.jsxs("div",{className:"flex items-center justify-between",children:[e.jsx("span",{className:"text-xs font-semibold uppercase tracking-[0.18em] text-slate-500",children:"Statut"}),e.jsx("span",{className:`rounded-full px-2 py-1 text-xs font-semibold ${r.ui_is_online?"bg-emerald-100 text-emerald-700":"bg-slate-200 text-slate-600"}`,children:r.ui_is_online?"En ligne":"Hors ligne"})]}),e.jsxs("div",{className:"mt-4 flex items-center justify-between text-slate-900",children:[e.jsxs("span",{className:"flex items-center gap-2",children:[e.jsx($e,{size:16,className:"text-emerald-600"}),"Batterie"]}),e.jsx("span",{className:"font-mono font-semibold",children:ne(r.last_battery)})]}),e.jsxs("div",{className:"mt-3 flex items-center justify-between text-slate-900",children:[e.jsx("span",{children:"Dernière mise à jour"}),e.jsx("span",{className:"font-medium",children:se(r.last_seen,z)})]})]}),e.jsxs("div",{children:[e.jsx("p",{className:"text-xs font-semibold uppercase tracking-[0.18em] text-slate-500",children:"Adresse actuelle"}),e.jsx("p",{className:"mt-2 leading-6 text-slate-900",children:r.last_address||"Adresse non encore résolue"})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"rounded-2xl border border-slate-200 bg-slate-50 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-slate-500",children:"Vitesse"}),e.jsx("p",{className:"mt-2 text-lg font-semibold text-slate-950",children:r.last_speed!==null&&r.last_speed!==void 0?`${Math.round(Number(r.last_speed))} km/h`:"—"})]}),e.jsxs("div",{className:"rounded-2xl border border-slate-200 bg-slate-50 p-4",children:[e.jsx("p",{className:"text-xs uppercase tracking-[0.18em] text-slate-500",children:"Précision GPS"}),e.jsx("p",{className:"mt-2 text-lg font-semibold text-slate-950",children:r.last_accuracy!==null&&r.last_accuracy!==void 0?`±${Math.round(Number(r.last_accuracy))} m`:"—"})]})]})]})}),e.jsx("div",{className:"border-t border-slate-200 p-4",children:e.jsxs("div",{className:"grid grid-cols-1 gap-2",children:[e.jsxs("button",{type:"button",className:"tracking-control justify-center",onClick:()=>s("/tracking/history"),children:[e.jsx(Le,{size:16}),"Voir l'historique"]}),e.jsxs("button",{type:"button",className:"tracking-control justify-center",onClick:()=>L(t=>t+1),children:[e.jsx(De,{size:16}),"Centrer sur ce téléphone"]}),e.jsxs("button",{type:"button",className:"tracking-control justify-center",onClick:()=>s("/tracking/devices"),children:[e.jsx(Y,{size:16}),"Paramètres"]})]})})]}):null}),ce?e.jsxs("aside",{className:"pointer-events-auto absolute right-4 top-20 z-[470] max-h-[420px] w-[340px] overflow-hidden tracking-overlay",children:[e.jsxs("div",{className:"flex items-center justify-between border-b border-slate-200 px-5 py-4",children:[e.jsxs("div",{children:[e.jsx("p",{className:"tracking-panel-title",children:"Alertes tracking"}),e.jsxs("h3",{className:"mt-2 text-lg font-semibold text-slate-950",children:[W," actives"]})]}),e.jsx("button",{type:"button",className:"rounded-xl border border-slate-200 p-2 text-slate-500",onClick:()=>U(!1),children:e.jsx(te,{size:18})})]}),e.jsx("div",{className:"max-h-[340px] overflow-y-auto px-4 py-4",children:D.length?e.jsx("div",{className:"space-y-3",children:D.map(t=>e.jsx("div",{className:"rounded-2xl border border-slate-200 bg-slate-50 p-4",children:e.jsxs("div",{className:"flex items-start justify-between gap-3",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-semibold text-slate-900",children:t.title}),e.jsx("p",{className:"mt-1 text-sm text-slate-600",children:t.message})]}),e.jsx("span",{className:"rounded-full bg-red-100 px-2 py-1 text-[11px] font-semibold text-red-700",children:"Active"})]})},t.id))}):e.jsx("div",{className:"py-10 text-center text-sm text-slate-500",children:"Aucune alerte active."})})]}):null]})]})})}export{qe as default};
