1import{j as e}from"./index-CWVhlzLh.js";import{g as E,k as R,b as a}from"./vendor-D4vAZRrx.js";import{s as v,L as P,C as b}from"./card-F3jmohpy.js";import{b as D,aM as q,B as A,M as B,an as F}from"./App-dsBIqK-t.js";import{r as J}from"./mapbox-gUQNAkoP.js";import{C as z}from"./circle-check-big-C0la5xLA.js";var U=J();const u=E(U),Y={};let m;async function O(){const x=Y?.VITE_MAPBOX_PUBLIC_TOKEN;if(x)return x;if(m!==void 0)return m;const g="https://mosthgmmpskwbbskbxsh.supabase.co",c="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Im1vc3RoZ21tcHNrd2Jic2tieHNoIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NjAyNzgyMjksImV4cCI6MjA3NTg1NDIyOX0.IYKNpR2VtCSBJGPKmGqNUDtxZvVYLCaSMbvoC7dKaYY";try{const f=await fetch(`${g}/functions/v1/get-mapbox-token`,{headers:{apikey:c,"Content-Type":"application/json"}});return f.ok?(m=(await f.json()).token??null,m):(m=null,m)}catch{return m=null,m}}const Q=()=>{const{id:x}=R(),g=a.useRef(null),c=a.useRef(null),f=a.useRef(null),_=a.useRef(null),[I,N]=a.useState(null),k=a.useRef(null),j=a.useRef(null),h=a.useRef(null),{data:t,isLoading:S}=D({queryKey:["delivery",x],queryFn:async()=>{const{data:n,error:i}=await v.from("deliveries").select("*, subscriptions(*), orders(*)").eq("id",x).single();if(i)throw i;return n},enabled:!!x}),[p,C]=a.useState(null);a.useEffect(()=>{if(!t?.id)return;(async()=>{const{data:r}=await v.from("driver_locations").select("*").eq("delivery_id",t.id).order("captured_at",{ascending:!1}).limit(1).maybeSingle();r&&C(r)})();const i=v.channel(`delivery-${t.id}-driver-tracking`).on("postgres_changes",{event:"*",schema:"public",table:"driver_locations",filter:`delivery_id=eq.${t.id}`},r=>{console.log(`ð Delivery ${t.id} driver location update:`,r),r.new&&C(r.new)}).subscribe(r=>{console.log(`ð Delivery ${t.id} driver subscription status:`,r)});return()=>{v.removeChannel(i)}},[t?.id]),a.useEffect(()=>{if(!(!g.current||!t)&&!c.current)return N(null),(async()=>{const n=await O();if(!n){N("Missing Mapbox public token");return}if(!/^pk\./.test(n)){N("Invalid Mapbox public token");return}u.accessToken=n;const i=t.lng?Number(t.lng):-95.7129,r=t.lat?Number(t.lat):37.0902;c.current=new u.Map({container:g.current,style:"mapbox://styles/mapbox/satellite-streets-v12",center:[i,r],zoom:16}),c.current.addControl(new u.NavigationControl,"top-right");const s=document.createElement("div");s.className="destination-marker",s.style.width="40px",s.style.height="40px",s.style.borderRadius="50%",s.style.backgroundColor="#22c55e",s.style.border="3px solid white",s.style.boxShadow="0 2px 8px rgba(0,0,0,0.3)",s.style.display="flex",s.style.alignItems="center",s.style.justifyContent="center",s.style.fontSize="20px",s.innerHTML="ð ",_.current=new u.Marker(s).setLngLat([i,r]).setPopup(new u.Popup({offset:25}).setDOMContent((()=>{const d=document.createElement("div");d.style.cssText="padding: 8px; font-family: sans-serif;";const l=document.createElement("h3");l.style.cssText="margin: 0 0 4px 0; font-weight: bold;",l.textContent="Delivery Address";const o=document.createElement("p");return o.style.cssText="margin: 0; color: #666;",o.textContent=t.address,d.appendChild(l),d.appendChild(o),d})())).addTo(c.current);const y=()=>{const d=f.current,l=j.current,o=k.current;if(d&&l&&o){const w=l.lat-o.lat,L=l.lng-o.lng;Math.sqrt(w*w+L*L)>1e-7&&(o.lat+=w*.12,o.lng+=L*.12,d.setLngLat([o.lng,o.lat]))}h.current=requestAnimationFrame(y)};h.current=requestAnimationFrame(y)})(),()=>{h.current&&(cancelAnimationFrame(h.current),h.current=null),c.current?.remove()}},[t]),a.useEffect(()=>{if(!c.current||!p)return;const n=Number(p.lng),i=Number(p.lat),r={lat:i,lng:n};if(f.current)j.current=r;else{const s=document.createElement("div");if(s.className="driver-marker",s.style.width="50px",s.style.height="50px",s.style.borderRadius="50%",s.style.backgroundColor="#3b82f6",s.style.border="3px solid white",s.style.boxShadow="0 2px 8px rgba(0,0,0,0.3)",s.style.display="flex",s.style.alignItems="center",s.style.justifyContent="center",s.style.fontSize="24px",s.style.willChange="transform",s.style.transform="translateZ(0)",s.innerHTML="ð",f.current=ne
1w u.Marker(s).setLngLat([n,i]).setPopup(new u.Popup({offset:25}).setHTML(` 2 <div style="padding: 8px; font-family: sans-serif;"> 3 <h3 style="margin: 0 0 4px 0; font-weight: bold;">Driver Location</h3> 4 <p style="margin: 0; color: #666;">Last updated: ${new Date(p.captured_at).toLocaleTimeString()}</p> 5 </div> 6 `)).addTo(c.current),k.current={...r},j.current={...r},t){const y=t.lng?Number(t.lng):-95.7129,d=t.lat?Number(t.lat):37.0902,l=new u.LngLatBounds;l.extend([n,i]),l.extend([y,d]),c.current.fitBounds(l,{padding:80,duration:1e3})}}},[p,t]);const{data:M}=D({queryKey:["delivery-photo",t?.verification_photo_url],queryFn:async()=>{if(!t?.verification_photo_url)return null;const{data:n}=v.storage.from("delivery-photos").getPublicUrl(t.verification_photo_url);return n.publicUrl},enabled:!!t?.verification_photo_url}),T=n=>{switch(n){case"pending":return"bg-yellow-500";case"in_transit":return"bg-blue-500";case"delivered":return"bg-green-500";default:return"bg-gray-500"}};return S?e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsx(P,{className:"h-8 w-8 animate-spin"})}):t?e.jsx("div",{className:"min-h-screen bg-background p-4",children:e.jsxs("div",{className:"max-w-4xl mx-auto space-y-4",children:[e.jsx("h1",{className:"text-3xl font-bold",children:"Track Your Delivery"}),e.jsxs(b,{className:"p-6",children:[e.jsxs("div",{className:"flex items-center justify-between mb-4",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(q,{className:"h-5 w-5"}),e.jsx("h2",{className:"text-xl font-semibold",children:"Delivery Status"})]}),e.jsx(A,{className:T(t.status||"pending"),children:t.status?.replace("_"," ").toUpperCase()})]}),e.jsxs("div",{className:"space-y-3",children:[e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(B,{className:"h-5 w-5 mt-0.5 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:"Delivery Address"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[t.address,", ",t.city,", ",t.state," ",t.zip]})]})]}),t.window_start&&t.window_end&&e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(F,{className:"h-5 w-5 mt-0.5 text-muted-foreground"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:"Delivery Window"}),e.jsxs("p",{className:"text-sm text-muted-foreground",children:[new Date(t.window_start).toLocaleString()," -"," ",new Date(t.window_end).toLocaleString()]})]})]}),t.delivered_at&&e.jsxs("div",{className:"flex items-start gap-2",children:[e.jsx(z,{className:"h-5 w-5 mt-0.5 text-green-500"}),e.jsxs("div",{children:[e.jsx("p",{className:"font-medium",children:"Delivered At"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:new Date(t.delivered_at).toLocaleString()})]})]})]})]}),e.jsxs(b,{className:"p-6",children:[e.jsx("h2",{className:"text-xl font-semibold mb-4",children:"Live Tracking"}),e.jsx("div",{ref:g,className:"w-full h-96 rounded-lg"}),I&&e.jsx("p",{className:"text-sm text-destructive mt-2",children:"Map unavailable: add a Mapbox public token to proceed."}),p&&e.jsxs("p",{className:"text-sm text-muted-foreground mt-2",children:["Last updated: ",new Date(p.captured_at).toLocaleString()]})]}),M&&e.jsxs(b,{className:"p-6",children:[e.jsx("h2",{className:"text-xl font-semibold mb-4",children:"Delivery Verification Photo"}),e.jsx("img",{src:M,alt:"Delivery verification",className:"w-full rounded-lg border"}),e.jsx("p",{className:"text-sm text-muted-foreground mt-2",children:"Photo taken by driver at delivery"})]}),t.customer_signature_data&&e.jsxs(b,{className:"p-6",children:[e.jsx("h2",{className:"text-xl font-semibold mb-4",children:"Your Signature"}),e.jsx("div",{className:"border rounded-lg p-4 bg-white",children:e.jsx("img",{src:t.customer_signature_data,alt:"Customer signature",className:"w-full max-w-md mx-auto"})}),e.jsxs("p",{className:"text-sm text-muted-foreground mt-2",children:["Signed at ",new Date(t.verified_at||"").toLocaleString()]})]})]})}):e.jsx("div",{className:"min-h-screen flex items-center justify-center",children:e.jsx("p",{children:"Delivery not found"})})};export{Q as default};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.