import{t as A,r as S}from"./index-BuFgUdF6.js";import{p as T}from"./payload-detail-C-jD4akC.js";function E(t){const r=[];for(const n of t){if(n.is_outgoing)continue;const o=T(n.text);o&&r.push({lat:o.lat,lon:o.lon,timestampS:n.timestamp,senderName:n.sender_name??"Unknown",pathHops:n.path_hops,pathSnr:n.path_snr,pathRssi:n.path_rssi,msgSnr:n.msg_snr})}return r.sort((n,o)=>n.timestampS-o.timestampS),r}function v(t,r){if(t.length===0)return null;if(r<=t[0].timestampS)return{lo:0,hi:0,t:0};if(r>=t[t.length-1].timestampS){const i=t.length-1;return{lo:i,hi:i,t:0}}let n=0,o=t.length-1;for(;n<o-1;){const i=n+o>>1;t[i].timestampS<=r?n=i:o=i}const e=t[o].timestampS-t[n].timestampS,a=e>0?(r-t[n].timestampS)/e:0;return{lo:n,hi:o,t:a}}function x(t,r,n,o,e){const a=e*e,i=a*e;return .5*(2*r+(-t+n)*e+(2*t-5*r+4*n-o)*a+(-t+3*r-3*n+o)*i)}function I(t,r,n,o){const e=Math.PI/180,a=(o-r)*e,i=Math.sin(a)*Math.cos(n*e),u=Math.cos(t*e)*Math.sin(n*e)-Math.sin(t*e)*Math.cos(n*e)*Math.cos(a);return(Math.atan2(i,u)*180/Math.PI+360)%360}function b(t,r){if(t.length===0)return null;const n=v(t,r);if(!n)return null;const{lo:o,hi:e,t:a}=n;if(o===e)return{lat:t[o].lat,lon:t[o].lon,bearing:null};const i=Math.max(0,o-1),u=o,h=e,g=Math.min(t.length-1,e+1),s=t[i],c=t[u],f=t[h],d=t[g];let m,p;i===u&&h===g?(m=c.lat+(f.lat-c.lat)*a,p=c.lon+(f.lon-c.lon)*a):(m=x(s.lat,c.lat,f.lat,d.lat,a),p=x(s.lon,c.lon,f.lon,d.lon,a));const P=I(c.lat,c.lon,f.lat,f.lon);return{lat:m,lon:p,bearing:P}}function $(t,r){const n=[];for(const e of t){if(e.timestampS>r)break;n.push([e.lon,e.lat])}const o=b(t,r);if(o&&n.length>0){const e=n[n.length-1];(Math.abs(e[0]-o.lon)>1e-7||Math.abs(e[1]-o.lat)>1e-7)&&n.push([o.lon,o.lat])}return n}function R(t,r){if(t.length===0)return null;if(t.length===1)return{lat:t[0].lat,lon:t[0].lon,bearing:null};const n=t.length-1,o=Math.max(0,Math.min(n,r)),e=Math.floor(o),a=Math.min(e+1,n),i=o-e;if(e===a)return{lat:t[e].lat,lon:t[e].lon,bearing:null};const u=Math.max(0,e-1),h=Math.min(n,a+1),g=t[u],s=t[e],c=t[a],f=t[h];let d,m;u===e&&a===h?(d=s.lat+(c.lat-s.lat)*i,m=s.lon+(c.lon-s.lon)*i):(d=x(g.lat,s.lat,c.lat,f.lat,i),m=x(g.lon,s.lon,c.lon,f.lon,i));const p=I(s.lat,s.lon,c.lat,c.lon);return{lat:d,lon:m,bearing:p}}function F(t,r){if(t.length===0)return[];const n=[],o=R(t,r);o&&n.push([o.lon,o.lat]);const e=Math.ceil(Math.max(0,r));for(let a=e;a<t.length;a++)n.push([t[a].lon,t[a].lat]);return n}function C(t){const r=Math.max(0,Math.round(t)),n=Math.floor(r/3600),o=Math.floor(r%3600/60),e=r%60;return n>0?`${n}:${String(o).padStart(2,"0")}:${String(e).padStart(2,"0")}`:`${o}:${String(e).padStart(2,"0")}`}const K=[2,5,10,20],M={isActive:!1,ownerKey:null,samples:[],isPlaying:!1,speed:5,currentIdx:0,currentTimeS:0,timeRange:{start:0,end:0}},l=A((t,r)=>({...M,startRewind:(n,o)=>{if(n.length<2){t({...M});return}const e=n.length-1,a={start:n[0].timestampS,end:n[e].timestampS};t({isActive:!0,ownerKey:o,samples:n,isPlaying:!0,speed:5,currentIdx:e,currentTimeS:a.end,timeRange:a})},togglePlay:()=>{var a;const{isPlaying:n,currentIdx:o,samples:e}=r();if(n)t({isPlaying:!1});else if(o<=0){const i=e.length-1;t({isPlaying:!0,currentIdx:i,currentTimeS:((a=e[i])==null?void 0:a.timestampS)??0})}else t({isPlaying:!0})},pause:()=>t({isPlaying:!1}),setSpeed:n=>t({speed:n}),seek:n=>{const{samples:o}=r();if(o.length<2)return;const e=o.length-1,a=Math.max(0,Math.min(e,n*e));t({currentIdx:a,currentTimeS:y(o,a)})},stopRewind:()=>t({...M}),tick:n=>{const{isPlaying:o,speed:e,currentIdx:a,samples:i}=r();if(!o||i.length<2)return;const u=e*(n/1e3),h=a-u;h<=0?t({currentIdx:0,currentTimeS:i[0].timestampS,isPlaying:!1}):t({currentIdx:h,currentTimeS:y(i,h)})}}));function y(t,r){if(t.length===0)return 0;const n=t.length-1,o=Math.max(0,Math.min(n,r)),e=Math.floor(o),a=Math.min(e+1,n);if(e===a)return t[e].timestampS;const i=o-e;return t[e].timestampS+i*(t[a].timestampS-t[e].timestampS)}const L=()=>l(t=>t.isActive),W=()=>l(t=>t.ownerKey),k=()=>l(t=>t.isPlaying),q=()=>l(t=>t.currentIdx),B=()=>l(t=>t.currentTimeS),D=()=>l(t=>t.speed),H=()=>l(t=>t.timeRange),N=()=>l(t=>t.samples);function j(){const{isActive:t,samples:r,currentIdx:n}=l.getState();return!t||r.length===0?null:R(r,n)}function O(){var e;const{isActive:t,samples:r,currentIdx:n}=l.getState();if(!t||r.length===0)return;const o=Math.round(Math.max(0,Math.min(r.length-1,n)));return(e=r[o])==null?void 0:e.pathHops}function U(){const t=k(),r=S.useRef(0),n=S.useRef(0);S.useEffect(()=>{if(!t){n.current=0;return}const o=l.getState().tick,e=a=>{if(n.current>0){const i=Math.min(a-n.current,100);o(i)}n.current=a,r.current=requestAnimationFrame(e)};return r.current=requestAnimationFrame(e),()=>{cancelAnimationFrame(r.current),n.current=0}},[t])}export{K as R,k as a,B as b,D as c,H as d,l as e,C as f,E as g,j as h,O as i,N as j,U as k,W as l,q as m,R as p,F as r,$ as t,L as u};
