1import{cQ as Ce,r as o,b as St,ik as $t,il as Dt,v as e,im as Pt,y as Tt,L as Me,co as Ae,io as rt,an as ot,a as it,aC as ce,aw as J,ip as Mt,bF as $e,bG as De,bH as xe,bI as he,cq as ge,B,bJ as me,aD as ze,X as Ie,ci as Ue,cj as He,ck as Qe,cl as qe,cm as Ge,cr as be,F as It,a3 as ne,iq as Et,at as Rt,ir as At,bL as Ke,S as Lt,P as Ft,T as Ot,dU as Bt,is as _t,it as Vt,dV as Ee,iu as Re,iv as Wt,iw as Ye,C as lt,bK as zt,O as Ut,aE as Ht,cy as Qt}from"./index-D37iq4ts.js";import{f as qt,g as Gt,B as Kt,h as Yt,P as Xt,j as Jt,l as Zt,u as es}from"./swipe-XDt9Bft6.js";import{g as ts,D as Xe,r as ss,s as ns}from"./fieldSettings-SBPPxZUJ.js";import{L as dt}from"./LassoProspectingModal-DgPbnufM.js";import{C as as}from"./clipboard-list-K1UEj5LZ.js";import{F as rs}from"./footprints-DfjCUAFO.js";import{C as ct}from"./copy-n0_KuWiH.js";import"./refresh-cw-CVM8nj_g.js";import"./pins-CmuW3aFh.js";import"./useTaskTypes-3fdZk0Oz.js";import"./popover-Dk3e6_He.js";import"./command-_ZbnER1B.js";import"./chevrons-up-down-BycGJGkT.js";const os=3;function is(t){const{mode:s,query:p,hasSelectedPlace:d,lastQuery:l}=t;return s!=="address"?"clear":d||p.length<os?"idle":l===p?"skip":"fetch"}const ls=new Set(["google.com","gmail.com","googlemail.com","outlook.com","hotmail.com","live.com","yahoo.com","icloud.com","aol.com","proton.me","protonmail.com"]);function Je(t,s){return!!(ls.has(t)||s&&t===s)}function ds(t){const s=t.lastIndexOf("@");if(s!==-1)return Ce(t.slice(s+1))}function ut(t,s,p,d,l){if(!t.length||!s.length)return[];const N=new Set;for(const v of p){const b=Ce(v.domain??void 0);b&&N.add(b)}const T=l?Ce(l):void 0,x=new Map;for(const v of s){const b=Ce(v.domain??void 0);if(!b||Je(b,T))continue;const g=x.get(b);g?g.push(v):x.set(b,[v])}const C=new Map;for(const v of t)for(const b of v.attendees){const g=ds(b.trim().toLowerCase());!g||N.has(g)||C.has(g)||Je(g,T)||x.has(g)&&C.set(g,{meetingTitle:v.title,meetingStart:v.start})}const c=new Set,u=[];for(const[v,b]of C.entries()){const g=x.get(v)??[];for(const L of g){const S=L.id?.trim()??"";!S||c.has(S)||d.has(S)||(c.add(S),u.push({choice:{p:L,val:S},...b}))}}return u}function cs(t,s,p,d,l){const N={};for(const x of t){const C=x.start.slice(0,10);(N[C]??=[]).push(x)}const T={};for(const x of s){const C=ut(N[x]??[],p,d,new Set,l);T[x]=new Set(C.map(c=>`prospect:${c.choice.val}`))}return T}function us(t){const s=new Date(t);return Number.isNaN(s.getTime())?"":s.toLocaleTimeString(void 0,{hour:"numeric",minute:"2-digit"})}function ms(t,s){const p=new Set(s),d={};for(const l of s)d[l]=[];for(const l of t){if(!l.dueDate)continue;const N=l.dueDate.slice(0,10);p.has(N)&&d[N].push(l)}return d}const ps=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"];function de(t){const s=new Date(t);s.setHours(0,0,0,0);const d=(s.getDay()+6)%7;return s.setDate(s.getDate()-d),s}function A(t){return`${t.getFullYear()}-${String(t.getMonth()+1).padStart(2,"0")}-${String(t.getDate()).padStart(2,"0")}`}function mt(t,s){const p=t.getMonth()===s.getMonth(),d=t.toLocaleDateString(void 0,{month:"short",day:"numeric"}),l=s.toLocaleDateString(void 0,p?{day:"numeric"}:{month:"short",day:"numeric"});return`${d} â ${l}`}function pt(t){return t.toLocaleDateString(void 0,{weekday:"short",month:"short",day:"numeric"})}const fs=/field[\s-]?sales|^field$/i,xs=/business[\s-]?dev|bdr|bdm|sales[\s-]?dev/i;function hs(t){const s=t.department?.trim()??"";return fs.test(s)||xs.test(s)}const Ze=["#f97316","#3b82f6","#10b981","#8b5cf6","#ec4899","#f59e0b","#06b6d4","#ef4444"];function gs(t){return Ze[t%Ze.length]}function bs({member:t,color:s,size:p=40}){const d=`${p}px`;return t.photoUrl?e.jsx("img",{src:t.photoUrl,alt:t.name,referrerPolicy:"no-referrer",className:"shrink-0 rounded-full object-cover",style:{width:d,height:d,outline:`2px solid ${s}`,outlineOffset:"1px"}}):e.jsx("span",{className:"flex shrink-0 items-center justify-center rounded-full font-bold text-white",style:{width:d,height:d,backgroundColor:s,fontSize:p*.4},children:(t.name?.[0]??"?").toUpperCase()})}function et({member:t,color:s}){return e.jsx("div",{className:"rounded-xl border border-border bg-card p-4",children:e.jsxs("div",{className:"flex items-start gap-3",children:[e.jsx(bs,{member:t,color:s}),e.jsx("div",{className:"min-w-0 flex-1",children:e.jsx("p",{className:"truncate font-semibold text-foreground",children:Ae(t.name)})})]})})}function tt({title:t,items:s,testid:p}){return e.jsxs("div",{className:"space-y-2","data-testid":p,children:[e.jsxs("h3",{className:"text-sm font-semibold text-foreground",children:[t," (",s.length,")"]}),s.length===0?e.jsxs("p",{className:"text-sm text-muted-foreground",children:["No ",t.toLowerCase()," with location data nearby."]}):e.jsx("ul",{className:"space-y-1.5",children:s.map(({record:d,distance:l},N)=>e.jsxs("li",{className:"flex items-start justify-between gap-3 rounded-md border border-border px-3 py-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("p",{className:"truncate text-sm font-medium text-foreground",children:d.name}),(d.city||d.address)&&e.jsx("p",{className:"truncate text-xs text-muted-foreground",children:d.city||d.address})]}),e.jsx("span",{className:"shrink-0 text-xs font-medium text-muted-foreground",children:qt(l)})]},d.id??`${t}-${N}`))})]})}function js({member:t,day:s,onClose:p,onSaved:d}){const{getToken:l}=it(),{toast:N}
1=ce(),[T,x]=o.useState(!1),[C,c]=o.useState([]),[u,v]=o.useState("prospect"),[b,g]=o.useState(""),[L,S]=o.useState(""),[$,_]=o.useState([]),[X,z]=o.useState(null),[D,M]=o.useState(!1),U=o.useRef(0),R=o.useRef(""),m=o.useRef(null),j=()=>(m.current||(m.current=crypto.randomUUID()),m.current),[Q,te]=o.useState([]),[Z,V]=o.useState(""),[G,H]=o.useState(!1),[I,n]=o.useState([]),[w,k]=o.useState("10"),[r,i]=o.useState("2"),[P,E]=o.useState("form"),[K,W]=o.useState(!1),[y,F]=o.useState(null),[ae,oe]=o.useState(""),[ee,ie]=o.useState([]),[je,we]=o.useState([]),[ve,ke]=o.useState([]),ue=o.useRef(null);o.useEffect(()=>{s&&(te(J().filter(a=>a.name?.trim())),v("prospect"),g(""),V(""),H(!1),n([]),S(""),_([]),z(null),m.current=null,R.current="",M(!1),k("1"),i("1"),E("form"),W(!1),F(null),oe(""),ie([]),we([]),x(!1),c([]),ue.current=null)},[s]),o.useEffect(()=>{if(!s)return;const a=A(s);if(ue.current===a)return;ue.current=a,ke([]);let f=!0;return(async()=>{try{const q=await l();if(!q)return;const O={Authorization:`Bearer ${q}`},Y=await fetch("/api/personal-calendar/status",{headers:O});if(!Y.ok)return;const se=await Y.json();if(!se.connected)return;const Te=se.connectedEmail??void 0,wt=Te?(()=>{const Ne=Te.lastIndexOf("@");return Ne!==-1?Te.slice(Ne+1):void 0})():void 0,vt=new URLSearchParams({from:a,to:a}),We=await fetch(`/api/personal-calendar/events?${vt.toString()}`,{headers:O});if(!We.ok)return;const kt=(await We.json()).events??[],yt=J().filter(Ne=>Ne.name?.trim()),Nt=ne(),Ct=ut(kt,yt,Nt,new Set,wt);f&&ke(Ct)}catch{}})(),()=>{f=!1}},[s,l]),o.useEffect(()=>{const a=L.trim(),f=is({mode:u,query:a,hasSelectedPlace:!!X,lastQuery:R.current});if(f==="clear"){U.current++,_([]),M(!1);return}if(f==="idle"){U.current++,_([]),M(!1),R.current="";return}if(f==="skip")return;R.current=a,M(!0);const h=++U.current,q=setTimeout(async()=>{try{const O=await l(),Y={"X-Place-Session-Token":j()};O&&(Y.Authorization=`Bearer ${O}`);const se=await Mt({input:a},{headers:Y});h===U.current&&_(se.suggestions)}catch{h===U.current&&_([])}finally{h===U.current&&M(!1)}},300);return()=>clearTimeout(q)},[L,u,X,l]);function le(a){S(a.description),z(a.placeId),_([]),F(null)}const Pe=o.useMemo(()=>Q.map((a,f)=>({p:a,val:a.id?.trim()||`idx-${f}`})),[Q]),ye=o.useMemo(()=>new Set(I.map(a=>a.val)),[I]),Le=o.useMemo(()=>{const a=Z.trim().toLowerCase();return(a?Pe.filter(({p:h})=>h.name?.toLowerCase().includes(a)||h.city?.toLowerCase().includes(a)):Pe).filter(h=>!ye.has(h.val)).slice(0,50)},[Pe,Z,ye]),Fe=o.useMemo(()=>ve.filter(a=>!ye.has(a.choice.val)),[ve,ye]);function Oe(a){n(f=>f.some(h=>h.val===a.val)?f:[...f,a]),g(a.val),V(""),H(!1),F(null)}function xt(a){n(f=>{const h=f.filter(q=>q.val!==a);return b===a&&g(h.length>0?h[h.length-1].val:""),h})}const Be=Array.from({length:20},(a,f)=>String(f+1)),ht=s!==null,_e=u==="prospect"?I.length>0:L.trim().length>0,re=u==="prospect"&&I.length>0?I[I.length-1].p:void 0;function Ve(a,f,h,q){return a.filter(O=>typeof O.lat=="number"&&typeof O.lng=="number"&&(!q||O.id!==q)).map(O=>({record:O,distance:Jt(f,[O.lat,O.lng])})).sort((O,Y)=>O.distance-Y.distance).slice(0,h)}async function gt(){if(!_e||K)return;W(!0),F(null);let a=null,f="";if(u==="prospect"&&re)f=re.name,typeof re.lat=="number"&&typeof re.lng=="number"&&(a=[re.lat,re.lng]);else if(u==="address"){if(f=L.trim(),!X){W(!1),F("Pick an address from the suggestions list so we can look up its exact coordinates.");return}try{const O=await l(),Y={};O&&(Y.Authorization=`Bearer ${O}`),m.current&&(Y["X-Place-Session-Token"]=m.current),m.current=null;const se=await Qt(X,Object.keys(Y).length?{headers:Y}:void 0);typeof se.lat=="number"&&typeof se.lng=="number"&&(a=[se.lat,se.lng])}catch{}}if(!a){W(!1),F(u==="prospect"?"This prospect doesn't have Google-derived coordinates yet. Enrich it with Google Maps data first.":"Couldn't determine coordinates for that address. Try picking a suggestion again.");return}const h=parseInt(w,10),q=parseInt(r,10);ie(Ve(J(),a,h,re?.id)),we(Ve(ne(),a,q)),oe(f),E("results"),W(!1)}function bt(){if(!s)return;const a=(h,q)=>h.record.id?{id:h.record.id,kind:q,name:h.record.name,subtitle:h.record.city||h.record.address||void 0}:null,f=[...ee.map(h=>a(h,"prospect")),...je.map(h=>a(h,"customer"))].filter(h=>h!==null);Re(t.id,A(s),f),d?.(),p()}function jt(){if(!s||I.length===0)return;const a=I.filter(f=>f.p.id).map(f=>({id:f.p.id,kind:"prospect",name:f.p.name,subtitle:f.p.city||f.p.address||void 0}));
1Re(t.id,A(s),a),d?.(),p()}return e.jsxs(e.Fragment,{children:[e.jsx($e,{open:ht,onOpenChange:a=>{a||p()},children:e.jsx(De,{className:"sm:max-w-md",children:P==="form"?e.jsxs("form",{onSubmit:a=>{a.preventDefault(),gt()},className:"space-y-4",children:[e.jsxs(xe,{children:[e.jsx(he,{children:"Pick starting point"}),e.jsxs(ge,{children:[Ae(t.name),s?` · ${s.toLocaleDateString(void 0,{weekday:"long",month:"short",day:"numeric"})}`:""]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-2",children:[e.jsx("button",{type:"button",onClick:()=>{v("prospect"),g(""),V(""),F(null)},"data-testid":"toggle-mode-prospect",className:`rounded-md border px-3 py-2 text-sm font-medium transition-colors ${u==="prospect"?"border-accent bg-accent/10 text-accent":"border-border text-muted-foreground hover:bg-muted"}`,children:"Select a prospect"}),e.jsx("button",{type:"button",onClick:()=>{v("address"),V(""),F(null)},"data-testid":"toggle-mode-address",className:`rounded-md border px-3 py-2 text-sm font-medium transition-colors ${u==="address"?"border-accent bg-accent/10 text-accent":"border-border text-muted-foreground hover:bg-muted"}`,children:"Enter an address"})]}),u==="prospect"&&Fe.length>0&&e.jsxs("div",{className:"space-y-1.5","data-testid":"calendar-suggestions-section",children:[e.jsx("p",{className:"text-xs font-medium text-muted-foreground uppercase tracking-wide",children:"From your calendar"}),e.jsx("ul",{className:"space-y-1",children:Fe.map(a=>{const f=us(a.meetingStart),h=a.meetingTitle?f?`${a.meetingTitle} · ${f}`:a.meetingTitle:f;return e.jsxs("li",{"data-testid":`calendar-suggestion-${a.choice.val}`,className:"flex items-center justify-between gap-2 rounded-md border border-border bg-muted/30 px-3 py-2",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("span",{className:"block truncate text-sm font-medium text-foreground",children:a.choice.p.name}),h&&e.jsx("span",{className:"block truncate text-xs text-muted-foreground",children:h})]}),e.jsx(B,{type:"button",size:"sm",variant:"outline","data-testid":`add-calendar-suggestion-${a.choice.val}`,onClick:()=>Oe(a.choice),className:"h-7 shrink-0 px-2 text-xs",children:"Add"})]},a.choice.val)})})]}),u==="prospect"?e.jsxs("div",{className:"space-y-2",children:[e.jsx(me,{htmlFor:"plan-prospect",children:I.length>0?`Prospects (${I.length} selected)`:"Prospect"}),e.jsxs("div",{className:"relative",children:[e.jsx(ze,{id:"plan-prospect","data-testid":"input-prospect",placeholder:"Search prospectsâ¦",autoComplete:"off",value:Z,onChange:a=>{V(a.target.value),H(!0),F(null)},onFocus:()=>H(!0),onBlur:()=>setTimeout(()=>H(!1),150)}),G&&e.jsx("ul",{"data-testid":"prospect-suggestions",className:"absolute z-50 mt-1 max-h-60 w-full overflow-auto rounded-md border border-border bg-popover py-1 shadow-md",children:Le.length===0?e.jsx("li",{className:"px-3 py-2 text-sm text-muted-foreground",children:Q.length===0?"No prospects available":I.length>0&&Z.trim()===""?"All matching prospects selected":"No matches"}):Le.map((a,f)=>e.jsx("li",{children:e.jsxs("button",{type:"button","data-testid":`prospect-suggestion-${f}`,onMouseDown:h=>h.preventDefault(),onClick:()=>Oe(a),className:"flex w-full flex-col items-start gap-0.5 px-3 py-2 text-left text-sm transition-colors hover:bg-accent hover:text-accent-foreground",children:[e.jsx("span",{className:"font-medium text-foreground",children:a.p.name}),a.p.city&&e.jsx("span",{className:"text-xs text-muted-foreground",children:a.p.city})]})},a.val))})]}),I.length>0&&e.jsx("ul",{"data-testid":"selected-prospects-list",className:"space-y-1",children:I.map(a=>e.jsxs("li",{"data-testid":`selected-prospect-${a.val}`,className:"flex items-center justify-between gap-2 rounded-md border border-border bg-muted/40 px-3 py-1.5",children:[e.jsxs("div",{className:"min-w-0",children:[e.jsx("span",{className:"block truncate text-sm font-medium text-foreground",children:a.p.name}),a.p.city&&e.jsx("span",{className:"block truncate text-xs text-muted-foreground",children:a.p.city})]}),e.jsx("button",{type:"button","data-testid":`remove-selected-prospect-${a.val}`,onClick:()=>xt(a.val),className:"shrink-0 rounded p-0.5 text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive","aria-label":`Remove ${a.p.name}`,children:e.jsx(Ie,{className:"h-3.5 w-3.5"})})]},a.val))})]}):e.jsxs("div",{className:"space-y-2",children:[e.jsx(me,{htmlFor:"plan-address",children:"Address"}),e.jsxs("div",{className:"relative",children:[e.jsx(ze,{id:"plan-address","data-testid":"input-address",placeholder:"Start typing an addressâ¦",autoComplete:"off",value:L,onChange:a=>{S(a.target.value),z(null),F(null)}}),D&&e.jsx(Me,{className:"absolute right-2 top-1/2 h-4 w-4 -translate-y-1/2 animate-spin text-muted-foreground"}),$.length>0&&e.jsx("ul",{"data-testid":"address-suggestions",className:"absolute z-50 mt-1 max-h-60 w-full overflow-auto rounded-md border border-border bg-popover py-1 shadow-md",children:$.map((a,f)=>e.jsx("li",{children:e.jsxs("button",{type:"button","data-testid":`address-suggestion-${f}`,onClick:()=>le(a),className:"flex w-full flex-col items-start gap-0.5 px-3 py-2 text-left text-sm transition-colors hover:bg-accent hover:text-accent-foreground",children:[e.jsx("span",{className:"font-medium text-foreground",children:a.primaryText??a.description}),a.secondaryText&&e.jsx("span",{className:"text-xs text-muted-foreground",children:a.secondaryText})]})},a.placeId??`${a.description}-${f}`))})]})]}),e.jsxs("div",{className:"grid grid-cols-2 gap-3",children:[e.jsxs("div",{className:"space-y-2",children:[e.jsx(me,{htmlFor:"plan-prospect-visits",children:"Number of Prospect Visits"}),e.jsxs(Ue,{value:w,onValueChange:k,children:[e.jsx(He,{id:"plan-prospect-visits","data-testid":"select-prospect-visits",children:e.jsx(Qe,{})}),e.jsx(qe,{className:"max-h-[240px]",children:Be.map(a=>e.jsx(Ge,{value:a,children:a},a))})]})]}),e.jsxs("div",{className:"space-y-2",children:[e.jsx(me,{htmlFor:"plan-customer-visits",children:"Number of Customer Visits"}),e.jsxs(Ue,{value:r,onValueChange:i,children:[e.jsx(He,{id:"plan-customer-visits","data-testid":"select-customer-visits",children:e.jsx(Qe,{})}),e.jsx(qe,{className:"max-h-[240px]",children:Be.map(a=>e.jsx(Ge,{value:a,children:a},a))})]})]})]}),y&&e.jsx("p",{className:"text-sm text-destructive","data-testid":"text-plan-error",children:y}
1),e.jsxs(be,{children:[e.jsx(B,{type:"button",variant:"outline",onClick:p,"data-testid":"button-cancel-plan",children:"Cancel"}),u==="prospect"&&I.length>0&&e.jsxs(e.Fragment,{children:[e.jsxs(B,{type:"button",variant:"outline",onClick:()=>{c(I.map(a=>a.p)),x(!0)},"data-testid":"button-create-tasks-selected",children:["Create tasks (",I.length,")"]}),e.jsxs(B,{type:"button",variant:"outline",onClick:jt,"data-testid":"button-add-selected-to-plan",children:["Add to Plan (",I.length,")"]})]}),e.jsx(B,{type:"submit",disabled:!_e||K,"data-testid":"button-confirm-plan",children:K?e.jsxs(e.Fragment,{children:[e.jsx(Me,{className:"h-4 w-4 animate-spin"}),"Findingâ¦"]}):"Continue"})]})]}):e.jsxs("div",{className:"space-y-4",children:[e.jsxs(xe,{children:[e.jsx(he,{children:"Recommended visits"}),e.jsxs(ge,{children:["Closest to ",ae||"your starting point"]})]}),e.jsxs("div",{className:"max-h-[55vh] space-y-5 overflow-y-auto pr-1",children:[e.jsx(tt,{title:"Prospects",items:ee,testid:"list-rec-prospects"}),e.jsx(tt,{title:"Customers",items:je,testid:"list-rec-customers"})]}),e.jsxs(be,{children:[e.jsxs(B,{type:"button",variant:"outline",onClick:()=>E("form"),"data-testid":"button-back-plan",children:[e.jsx(It,{className:"h-4 w-4"}),"Back"]}),ee.length>0&&e.jsxs(B,{type:"button",variant:"outline",onClick:()=>{c(ee.map(a=>a.record)),x(!0)},"data-testid":"button-create-tasks-recommended",children:["Create tasks (",ee.length,")"]}),e.jsx(B,{type:"button",onClick:bt,disabled:ee.length===0&&je.length===0,"data-testid":"button-add-to-plan",children:"Add to Plan"})]})]})})}),e.jsx(dt,{open:T,prospects:C,defaultAction:"walk_in",defaultDueDate:s?A(s):"",onClose:()=>x(!1),onSuccess:a=>{x(!1),N({title:`${a} task${a!==1?"s":""} created`,description:"Tasks are linked to each selected prospect."})}})]})}function st({member:t,onSelectProspect:s,onSelectCustomer:p,weekStart:d,onWeekStartChange:l,mode:N,onModeChange:T,showHeader:x=!0,dataVersion:C=0}){const{toast:c}=ce(),[u,v]=o.useState(()=>de(new Date)),b=d??u,g=r=>{l?l(r):v(r)},[L,S]=o.useState("week"),$=N??L,_=r=>{r!==$&&(r==="week"&&g(de(b)),T?T(r):S(r))}
1,[X,z]=o.useState(null),[D,M]=o.useState(null),[U,R]=o.useState(0),m=o.useRef(!0);o.useEffect(()=>{if(m.current){m.current=!1;return}R(r=>r+1)},[C]);const j=o.useMemo(()=>$==="day"?[new Date(b)]:Array.from({length:5},(r,i)=>{const P=new Date(b);return P.setDate(b.getDate()+i),P}),[b,$]),Q=o.useMemo(()=>{const r={},i=J(),P=ne();for(const E of j){const K=A(E);r[K]=_t(Vt(t.id,K),i,P)}return r},[j,t.id,U]),{getToken:te}=it(),[Z,V]=o.useState({}),[G,H]=o.useState({});o.useEffect(()=>{let r=!1;return(async()=>{try{const P=await te();if(!P){r||V({});return}const E={Authorization:`Bearer ${P}`},K=await fetch("/api/personal-calendar/status",{headers:E});if(!K.ok){r||V({});return}const W=await K.json();if(!W.connected){r||V({});return}const y=W.connectedEmail??void 0,F=y?(()=>{const le=y.lastIndexOf("@");return le!==-1?y.slice(le+1):void 0})():void 0,ae=A(j[0]),oe=A(j[j.length-1]),ee=new URLSearchParams({from:ae,to:oe}),ie=await fetch(`/api/personal-calendar/events?${ee.toString()}`,{headers:E});if(!ie.ok){r||V({});return}const we=(await ie.json()).events??[],ve=J(),ke=ne(),ue=cs(we,j.map(le=>A(le)),ve,ke,F);r||V(ue)}catch{r||V({})}})(),()=>{r=!0}},[j,te,U]),o.useEffect(()=>{if(!t.clerkId){H({});return}let r=!1;return(async()=>{try{const P=await te();if(!P){r||H({});return}const E=A(j[0]),K=A(j[j.length-1]),W=new URLSearchParams({assigneeClerkId:t.clerkId,dueDateFrom:E,dueDateTo:K}),y=await fetch(`/api/tasks?${W.toString()}`,{headers:{Authorization:`Bearer ${P}`}});if(!y.ok){r||H({});return}const F=await y.json();r||H(ms(F,j.map(ae=>A(ae))))}catch{r||H({})}})(),()=>{r=!0}},[j,t.clerkId,te]);const I=(r,i)=>{const P=r.kind==="prospect"?J().find(E=>E.id===r.id):ne().find(E=>E.id===r.id);if(!P){Ye(t.id,i,r.kind,r.id),R(E=>E+1),c({title:"Record no longer available",description:`${r.name} has been removed from this plan.`,variant:"destructive"});return}r.kind==="prospect"?s?.(P):p?.(P)},n=A(new Date),w=r=>{const i=new Date(b);i.setDate(b.getDate()+r*($==="day"?1:7)),g(i)},k=()=>{if($==="day"){const r=new Date;r.setHours(0,0,0,0),g(r)}else g(de(new Date))};return e.jsxs("div",{className:"mt-4 rounded-xl border border-border bg-card p-4",children:[x&&e.jsxs("div",{className:"mb-4 flex items-center justify-between gap-2",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground",children:$==="day"?pt(j[0]):`Week of ${mt(j[0],j[4])}`}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex overflow-hidden rounded-md border border-border text-xs",children:[e.jsx("button",{onClick:()=>_("week"),"data-testid":"button-mode-week","aria-pressed":$==="week",className:`px-2.5 py-1 font-medium transition-colors ${$==="week"?"bg-accent text-accent-foreground":"text-muted-foreground hover:bg-muted hover:text-foreground"}`,children:"Week"}),e.jsx("button",{onClick:()=>_("day"),"data-testid":"button-mode-day","aria-pressed":$==="day",className:`px-2.5 py-1 font-medium transition-colors ${$==="day"?"bg-accent text-accent-foreground":"text-muted-foreground hover:bg-muted hover:text-foreground"}`,children:"Day"})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:()=>w(-1),"data-testid":"button-week-prev","aria-label":$==="day"?"Previous day":"Previous week",className:"flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:e.jsx(rt,{className:"h-4 w-4"})}),e.jsx("button",{onClick:k,"data-testid":"button-week-today",className:"rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:"Today"}),e.jsx("button",{onClick:()=>w(1),"data-testid":"button-week-next","aria-label":$==="day"?"Next day":"Next week",className:"flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:e.jsx(ot,{className:"h-4 w-4"})})]})]})]}),e.jsx("div",{className:`grid grid-cols-1 gap-2 ${$==="week"?"sm:grid-cols-5":""}`,children:j.map(r=>{const i=A(r),P=i===n,E=Q[i]??[],W=(G[i]??[]).length;return e.jsxs("div",{className:`flex flex-col rounded-lg border p-2 ${P?"border-accent bg-accent/5":"border-border bg-background"}`,children:[e.jsxs("button",{type:"button",onClick:()=>M(r),"data-testid":`day-cell-${t.id}-${i}`,"aria-label":`View plan for ${r.toLocaleDateString(void 0,{weekday:"long",month:"long",day:"numeric"})}`,className:"mb-2 rounded-md py-0.5 text-center transition-colors hover:bg-accent/10",children:[e.jsx("p",{className:"text-[11px] font-medium uppercase tracking-wide text-muted-foreground",children:ps[(r.getDay()+6)%7]}),e.jsxs("div",{className:"flex items-center justify-center gap-1.5",children:[e.jsx("p",{className:`text-sm font-bold ${P?"text-accent":"text-foreground"}`,children:r.getDate()}),W>0&&e.jsxs("span",{"data-testid":`task-count-badge-${t.id}-${i}`,title:`${W} open ${W===1?"task":"tasks"} due`,className:"inline-flex items-center rounded-full bg-muted px-1.5 py-0.5 text-[10px] font-semibold leading-none text-muted-foreground",children:[W," ",W===1?"task":"tasks"]})]})]}),E.length>0&&e.jsx("ul",{className:"mb-2 space-y-1","data-testid":`plan-list-${t.id}-${i}`,children:E.map(y=>e.jsx("li",{children:e.jsxs("button",{type:"button",onClick:()=>I(y,i),title:y.subtitle?`${y.name} â ${y.subtitle}`:y.name,"data-testid":`plan-entry-${y.kind}-${y.id}`,className:"flex w-full items-center gap-1.5 rounded-md border border-border bg-card px-2 py-1 text-left text-xs transition-colors hover:border-accent hover:bg-accent/10",children:[e.jsx("span",{className:"h-1.5 w-1.5 shrink-0 rounded-full",style:{backgroundColor:vs(y,Z[i]??new Set)}}),e.jsx("span",{className:"truncate text-foreground",children:y.name})]})},`${y.kind}:${y.id}`))}),(G[i]??[]).length>0&&e.jsx("ul",{className:$==="day"?"mb-2 space-y-1":"mb-2 space-y-0.5","data-testid":`task-list-${t.id}-${i}`,children:(G[i]??[]).map(y=>{const F=y.dueTime?(()=>{const[ae,oe]=y.dueTime.split(":").map(Number),ee=(ae??0)>=12?"PM":"AM";return`${(ae??0)%12||12}:${String(oe??0).padStart(2,"0")} ${ee}`})():null;return $==="day"?e.jsxs("li",{"data-testid":`field-task-${y.id}`,className:"flex items-center gap-2 rounded-md border border-border bg-card px-2.5 py-1.5 text-sm text-foreground",children:[e.jsx(Ee,{className:"h-3.5 w-3.5 shrink-0"}),e.jsx("span",{className:"min-w-0 flex-1 truncate",children:y.title}),F&&e.jsx("span",{className:"shrink-0 text-xs tabular-nums text-muted-foreground",children:F})]},y.id):e.jsxs("li",{"data-testid":`field-task-${y.id}`,className:"flex w-full items-center gap-1 rounded px-1.5 py-0.5 text-[11px] text-muted-foreground",children:[e.jsx(Ee,{className:"h-2.5 w-2.5 shrink-0"}),e.jsx("span",{className:"min-w-0 flex-1 truncate",children:y.title}),F&&e.jsx("span",{className:"shrink-0 text-[10px] tabular-nums",children:F})]},y.id)})}),e.jsx(B,{onClick:()=>E.length>0?M(r):z(r),"data-testid":`button-create-plan-${t.id}-${i}`,size:"sm",className:"mt-auto w-full",children:E.length>
10?"View Plan":"Create Plan"})]},i)})}),e.jsx(js,{member:t,day:X,onClose:()=>z(null),onSaved:()=>R(r=>r+1)}),e.jsx(Ns,{day:D,entries:D?Q[A(D)]??[]:[],onClose:()=>M(null),onOpenEntry:r=>{D&&(I(r,A(D)),M(null))},onAddPlan:()=>{const r=D;M(null),z(r)},onRemoveEntry:r=>{D&&(Ye(t.id,A(D),r.kind,r.id),R(i=>i+1))},onRemoveAll:()=>{D&&(Wt(t.id,A(D)),R(r=>r+1))},onAddEntry:r=>{D&&(Re(t.id,A(D),[r]),R(i=>i+1))},meetingProspectIds:D?Z[A(D)]??new Set:new Set,tasks:D?G[A(D)]??[]:[]})]})}function ws(t){return new Zt.DivIcon({className:"",html:`<span style="display:block;width:14px;height:14px;border-radius:9999px;background:${t};border:2px solid #fff;box-shadow:0 0 0 1px rgba(0,0,0,0.25);"></span>`,iconSize:[14,14],iconAnchor:[7,7]})}const pe="#3b82f6",fe="#10b981",Se="#f59e0b";function vs(t,s){return t.kind==="customer"?fe:s.has(`prospect:${t.id}`)?Se:pe}function ks({points:t}){const s=es();return o.useEffect(()=>{t.length===1?s.setView(t[0],12):t.length>1&&s.fitBounds(t,{padding:[36,36]})},[s,t]),null}function ys({points:t,hasMeetingIds:s}){const p=o.useMemo(()=>t.map(l=>[l.lat,l.lng]),[t]),d=p.length>0?p[0]:[39.8283,-98.5795];return e.jsxs(Gt,{center:d,zoom:p.length>0?10:4,scrollWheelZoom:!1,className:"h-full w-full","data-testid":"day-plan-map",children:[e.jsx(Kt,{}),t.map(l=>{const N=`${l.entry.kind}:${l.entry.id}`,T=l.entry.kind==="prospect"?s.has(N)?Se:pe:fe;return e.jsx(Yt,{position:[l.lat,l.lng],icon:ws(T),children:e.jsxs(Xt,{children:[e.jsx("div",{className:"font-semibold",children:l.entry.name}),l.record.address&&e.jsx("div",{className:"text-xs text-gray-600",children:l.record.address}),e.jsx("div",{className:"flex items-center gap-1 text-xs font-medium",style:{color:T},children:l.entry.kind==="prospect"&&s.has(N)?"prospect · meeting":l.entry.kind})]})},N)}),e.jsx(ks,{points:p})]})}const nt=8;function Ns({day:t,entries:s,onClose:p,onOpenEntry:d,onAddPlan:l,onRemoveEntry:N,onRemoveAll:T,onAddEntry:x,meetingProspectIds:C,tasks:c}){const u=t!==null,v=t?t.toLocaleDateString(void 0,{weekday:"long",month:"long",day:"numeric"}):"",{toast:b}=ce(),[g,L]=o.useState(""),[S,$]=o.useState(!1),_=o.useRef(null),[X,z]=o.useState(!1),D=o.useMemo(()=>{if(!u)return[];const n=J(),w=new Map(n.map(r=>[r.id,r])),k=[];for(const r of s){if(r.kind!=="prospect")continue;const i=w.get(r.id);i&&k.push(i)}return k},[s,u]),M=o.useMemo(()=>{if(!u)return[];const n=ne(),w=new Map(n.map(r=>[r.id,r])),k=[];for(const r of s){if(r.kind!=="customer")continue;const i=w.get(r.id);i&&k.push(i)}return k},[s,u]),U=D.length+M.length,R=o.useMemo(()=>u?new Map(J().map(n=>[n.id,n.name??n.id])):new Map,[u]),m=o.useMemo(()=>u?new Map(ne().map(n=>[n.id,n.name??n.id])):new Map,[u]),j=o.useMemo(()=>u?new Map(Et().map(n=>[n.id,n.name??n.id])):new Map,[u]),Q=n=>{let w;n.linkedRecordType==="prospect"&&n.linkedRecordId?w=R.get(n.linkedRecordId):n.linkedRecordType==="customer"&&n.linkedRecordId&&(w=m.get(n.linkedRecordId));const k=n.linkedContactId?j.get(n.linkedContactId):void 0;if(w&&k)return`${w} · ${k}`;if(w)return w;if(k)return k},te=n=>{const w=n.dueTime?(()=>{const[r,i]=n.dueTime.split(":").map(Number),P=(r??0)>=12?"PM":"AM";return`${(r??0)%12||12}:${String(i??0).padStart(2,"0")} ${P}`})():null,k=Q(n);return e.jsxs("li",{"data-testid":`dialog-task-${n.id}`,className:"flex items-center gap-2 rounded-md border border-border bg-card px-2.5 py-1.5 text-sm text-foreground",children:[e.jsx(Ee,{className:"h-3.5 w-3.5 shrink-0"}),e.jsxs("span",{className:"min-w-0 flex-1",children:[e.jsx("span",{className:"block truncate",children:n.title}),k&&e.jsx("span",{"data-testid":`dialog-task-association-${n.id}`,className:"block truncate text-xs text-muted-foreground",children:k})]}),w&&e.jsx("span",{className:"shrink-0 text-xs tabular-nums text-muted-foreground",children:w})]},n.id)},Z=o.useMemo(()=>{if(!u)return[];const n=J(),w=ne(),k=[];for(const r of s){const i=r.kind==="prospect"?n.find(P=>P.id===r.id):w.find(P=>P.id===r.id);i&&typeof i.lat=="number"&&typeof i.lng=="number"&&k.push({entry:r,lat:i.lat,lng:i.lng,record:i})}return k},[s,u]),V=o.useMemo(()=>{if(!u||!C?.size)return new Set;const n=new Set;for(const w of s){if(w.kind!=="prospect")continue;
1const k=`prospect:${w.id}`;C.has(k)&&n.add(k)}return n},[s,C,u]),G=o.useMemo(()=>{const n={};if(!u||!t)return n;const w=t.getDay();for(const k of s){const r=`${k.kind}:${k.id}`,i=Rt(k.id);n[r]=i?At(i,w):null}return n},[s,u,t]),H=o.useMemo(()=>new Set(s.map(n=>`${n.kind}:${n.id}`)),[s]),I=o.useMemo(()=>{const n=g.trim().toLowerCase();if(!n)return[];const w=J(),k=ne(),r=[];for(const i of w)if(i.id&&!H.has(`prospect:${i.id}`)&&(i.name?.toLowerCase().includes(n)||i.city?.toLowerCase().includes(n))&&(r.push({id:i.id,kind:"prospect",name:i.name??i.id,subtitle:i.city??i.address??void 0}),r.length>=nt))return r;for(const i of k)if(i.id&&!H.has(`customer:${i.id}`)&&(i.name?.toLowerCase().includes(n)||i.city?.toLowerCase().includes(n))&&(r.push({id:i.id,kind:"customer",name:i.name??i.id,subtitle:i.city??i.address??void 0}),r.length>=nt))return r;return r},[g,H]);return o.useEffect(()=>{if(!S)return;function n(w){_.current&&!_.current.contains(w.target)&&$(!1)}return document.addEventListener("mousedown",n),()=>document.removeEventListener("mousedown",n)},[S]),e.jsxs(e.Fragment,{children:[e.jsx($e,{open:u,onOpenChange:n=>{n||p()},children:e.jsxs(De,{className:`flex flex-col max-h-[90svh] overflow-hidden${s.length>0||(c?.length??0)>0?" sm:max-w-3xl":""}`,children:[e.jsxs(xe,{children:[e.jsx(he,{children:v}),e.jsx(ge,{children:s.length>0?`${s.length} ${s.length===1?"visit":"visits"} planned${(c?.length??0)>0?` · ${c.length} ${c.length===1?"task":"tasks"} due`:""}`:(c?.length??0)>0?`${c.length} ${c.length===1?"task":"tasks"} due â no visits planned yet.`:"No visits planned for this day yet."})]}),e.jsxs("div",{className:"flex min-h-0 flex-1 flex-col gap-4 overflow-y-auto",children:[s.length>0&&V.size>0&&e.jsxs("div",{className:"flex flex-wrap items-center gap-3 text-xs text-muted-foreground",children:[e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"h-2 w-2 shrink-0 rounded-full",style:{backgroundColor:pe}}),"Prospect"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"h-2 w-2 shrink-0 rounded-full",style:{backgroundColor:Se}}),"Prospect with meeting"]}),e.jsxs("span",{className:"flex items-center gap-1.5",children:[e.jsx("span",{className:"h-2 w-2 shrink-0 rounded-full",style:{backgroundColor:fe}}),"Customer"]})]}),s.length>0&&e.jsxs("div",{className:"grid gap-4 md:grid-cols-2",children:[e.jsxs("div",{className:"flex flex-col gap-4 overflow-hidden",children:[e.jsx("ul",{className:"space-y-2 overflow-y-auto pr-1 md:max-h-[55vh]","data-testid":"day-plan-entries",children:s.map(n=>e.jsxs("li",{className:"flex items-center gap-1 rounded-lg border border-border bg-card transition-colors hover:border-accent hover:bg-accent/10",children:[e.jsxs("button",{type:"button",onClick:()=>d(n),"data-testid":`day-plan-entry-${n.kind}-${n.id}`,className:"flex min-w-0 flex-1 items-center gap-2.5 px-3 py-2.5 text-left",children:[e.jsx("span",{className:"h-2 w-2 shrink-0 rounded-full",style:{backgroundColor:V.has(`${n.kind}:${n.id}`)?Se:n.kind==="prospect"?pe:fe}}),e.jsxs("span",{className:"flex min-w-0 items-baseline gap-2",children:[e.jsx("span",{className:"truncate text-sm font-medium text-foreground",children:n.name}),n.subtitle&&e.jsx("span",{className:"truncate text-xs text-muted-foreground",children:n.subtitle})]}),G[`${n.kind}:${n.id}`]!=null&&e.jsxs("span",{"data-testid":`entry-open-status-${n.kind}-${n.id}`,className:`ml-auto inline-flex shrink-0 items-center gap-1 rounded-full border px-2 py-0.5 text-[10px] font-semibold ${G[`${n.kind}:${n.id}`]?"border-green-200 bg-green-50 text-green-700 dark:border-green-800 dark:bg-green-900/20 dark:text-green-400":"border-red-200 bg-red-50 text-red-600 dark:border-red-800 dark:bg-red-900/20 dark:text-red-400"}`,children:[e.jsx("span",{className:`h-1.5 w-1.5 shrink-0 rounded-full ${G[`${n.kind}:${n.id}`]?"bg-green-500":"bg-red-500"}`}),G[`${n.kind}:${n.id}`]?"Open":"Closed"]}),e.jsx("span",{className:`shrink-0 pl-2 text-[11px] font-medium uppercase tracking-wide text-muted-foreground ${G[`${n.kind}:${n.id}`]==null?"ml-auto":""}`,children:n.kind})]}),e.jsx("button",{type:"button",onClick:()=>N(n),"data-testid":`button-remove-entry-${n.kind}-${n.id}`,"aria-label":`Remove ${n.name} from plan`,className:"mr-1.5 flex h-7 w-7 shrink-0 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive",children:e.jsx(Ie,{className:"h-4 w-4"})})]},`${n.kind}:${n.id}`))}),(c?.length??0)>0&&e.jsxs("div",{className:"border-t border-border pt-2",children:[e.jsxs("p",{className:"mb-1.5 flex items-center gap-1.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground",children:[e.jsx(Ke,{className:"h-3.5 w-3.5"}),"Tasks due"]}),e.jsx("ul",{className:"space-y-1","data-testid":"day-plan-tasks",children:c.map(n=>te(n))})]})]}),e.jsx("div",{className:"relative h-64 overflow-hidden rounded-lg border border-border md:h-[55vh]","data-testid":"day-plan-map-container",children:Z.length>0?e.jsx(ys,{points:Z,hasMeetingIds:V}):e.jsx("div",{className:"flex h-full items-center justify-center p-4 text-center text-xs text-muted-foreground",children:"No mapped locations yet. Add coordinates to these venues to see them here."})})]}),s.length===0&&(c?.length??0)>0&&e.jsxs("div",{"data-testid":"day-plan-tasks-only",children:[e.jsxs("p",{className:"mb-1.5 flex items-center gap-1.5 text-[11px] font-semibold uppercase tracking-wide text-muted-foreground",children:[e.jsx(Ke,{className:"h-3.5 w-3.5"}),"Tasks due"]}),e.jsx("ul",{className:"space-y-1","data-testid":"day-plan-tasks",children:c.map(n=>te(n))})]})]}),e.jsxs("div",{ref:_,className:"relative","data-testid":"day-plan-stop-search",children:[e.jsxs("div",{className:"flex items-center gap-2 rounded-lg border border-border bg-white px-3 py-2 focus-within:border-ring focus-within:ring-1 focus-within:ring-ring dark:bg-card",children:[e.jsx(Lt,{className:"h-4 w-4 shrink-0 text-muted-foreground"}),e.jsx("input",{type:"text",placeholder:"Add a stopâ¦",value:g,onChange:n=>{L(n.target.value),$(!0)},onFocus:()=>{g.trim()&&$(!0)},"data-testid":"day-plan-stop-search-input",className:"min-w-0 flex-1 bg-transparent text-sm outline-none placeholder:text-muted-foreground"}),g&&e.jsx("button",{type:"button","aria-label":"Clear search",onClick:()=>{L(""),$(!1)},className:"shrink-0 text-muted-foreground transition-colors hover:text-foreground",children:e.jsx(Ie,{className:"h-3.5 w-3.5"})})]}),S&&I.length>0&&e.jsx("ul",{role:"listbox","data-testid":"day-plan-stop-results",className:"absolute bottom-full z-[2150] mb-1 max-h-56 w-full overflow-y-auto rounded-lg border border-border bg-popover shadow-md",children:I.map(n=>e.jsx("li",{role:"option","aria-selected":!1,children:e.jsxs("button",{type:"button","data-testid":`day-plan-stop-result-${n.kind}-${n.id}`,onMouseDown:w=>{w.preventDefault(),x(n),L(""),$(!1)},className:"flex w-full items-center gap-2.5 px-3 py-2.5 text-left transition-colors hover:bg-accent",children:[e.jsx("span",{className:"h-2 w-2 shrink-0 rounded-full",style:{backgroundColor:n.kind==="prospect"?pe:fe}}),e.jsxs("span",{className:"flex min-w-0 flex-1 items-baseline gap-2",children:[e.jsx("span",{className:"truncate text-sm font-medium text-foreground",children:n.name}),n.subtitle&&e.jsx("span",{className:"truncate text-xs text-muted-foreground",children:n.subtitle})]}),e.jsx("span",{className:"shrink-0 text-[11px] font-medium uppercase tracking-wide text-muted-foreground",children:n.kind}),e.jsx(Ft,{className:"h-3.5 w-3.5 shrink-0 text-muted-foreground"})]})},`${n.kind}:${n.id}`))})]}),e.jsxs(be,{className:"gap-2 sm:justify-between",children:[s.length>0?e.jsxs(B,{variant:"outline",onClick:T,"data-testid":"button-day-remove-all",className:"gap-1 text-destructive hover:text-destructive",children:[e.jsx(Ot,{className:"h-4 w-4"}),"Remove All"]}):e.jsx("span",{}),e.jsxs("div",{className:"flex gap-2",children:[U>0&&e.jsxs(B,{variant:"outline",onClick:()=>z(!0),"data-testid":"button-day-create-tasks",className:"gap-1",children:[e.jsx(Bt,{className:"h-4 w-4"}),"Create tasks (",U,")"]}),e.jsx(B,{onClick:l,"data-testid":"button-day-add-plan",children:s.length>0?"Add More":"Create Plan"})]})]})]})}),e.jsx(dt,{open:X,prospects:D,customers:M,defaultAction:"walk_in",defaultDueDate:t?A(t):"",onClose:()=>z(!1),onSuccess:n=>{z(!1),b({title:`${n} task${n!==1?"s":""} created`,description:"Tasks are linked to each selected stop."})}})]})}function Cs(t,s){if(!t)return!1;const p=t.trim().toLowerCase();return!!(s.clerkId&&s.clerkId.toLowerCase()===p||s.email&&s.email.toLowerCase()===p||s.name&&s.name.toLowerCase()===p)}function ft(t){const s=new Date(t);return Number.isNaN(s.getTime())?t:`${s.toLocaleDateString(void 0,{month:"short",day:"numeric",year:"numeric"})} · ${s.toLocaleTimeString(void 0,{hour:"numeric",minute:"2-digit"})}`}function at({member:t,dataVersion:s,onViewEmail:p,onSelectProspect:d}){const{toast:l}=ce(),[N,T]=o.useState(null);async function x(c){const u=c.walkIn.email?.trim()||c.walkIn.notes;if(!u){l({title:"Nothing to copy",description:"This walk-in has no email or notes yet."});return}try{await navigator.clipboard.writeText(u),T(c.walkIn.id),setTimeout(()=>T(v=>v===c.walkIn.id?null:v),1500)}catch{l({title:"Copy failed",variant:"destructive"})}}const C=o.useMemo(()=>{const c=[];
1for(const v of J())for(const b of v.walkIns??[])Cs(b.by,t)&&c.push({prospect:v,walkIn:b});const u=v=>{const b=new Date(v).getTime();return Number.isNaN(b)?0:b};return c.sort((v,b)=>u(b.walkIn.at)-u(v.walkIn.at))},[t,s]);return C.length===0?null:e.jsxs("div",{className:"mt-2 rounded-lg border border-border bg-card p-3","data-testid":`followups-${t.id}`,children:[e.jsxs("div",{className:"mb-2 flex items-center gap-1.5",children:[e.jsx(as,{className:"h-4 w-4 text-accent"}),e.jsx("h4",{className:"text-sm font-semibold text-foreground",children:"Follow-Ups"}),e.jsxs("span",{className:"text-xs text-muted-foreground",children:["(",C.length,")"]})]}),e.jsx("ul",{className:"space-y-1.5",children:C.map(c=>{const u=c.prospect.venueName||c.prospect.name||"Unknown venue";return e.jsxs("li",{"data-testid":`followup-${c.walkIn.id}`,className:"flex items-center gap-2 rounded-md border border-border bg-background px-3 py-2",children:[e.jsx(rs,{className:"h-4 w-4 shrink-0 text-muted-foreground"}),e.jsxs("div",{className:"min-w-0 flex-1",children:[e.jsxs("button",{type:"button",onClick:()=>d?.(c.prospect),className:"block truncate text-left text-sm font-medium text-foreground hover:text-accent",children:["Follow Up: ",u]}),e.jsxs("p",{className:"truncate text-xs text-muted-foreground",children:["Walk-in ",ft(c.walkIn.at)]})]}),e.jsxs(B,{variant:"outline",size:"sm",className:"shrink-0 gap-1",onClick:()=>x(c),"data-testid":`button-copy-email-${c.walkIn.id}`,children:[N===c.walkIn.id?e.jsx(lt,{className:"h-3.5 w-3.5"}):e.jsx(ct,{className:"h-3.5 w-3.5"}),N===c.walkIn.id?"Copied!":"Copy"]}),e.jsxs(B,{variant:"outline",size:"sm",className:"shrink-0 gap-1",onClick:()=>p(c),"data-testid":`button-view-email-${c.walkIn.id}`,children:[e.jsx(zt,{className:"h-3.5 w-3.5"}),"View email"]})]},c.walkIn.id)})})]})}function Ss({open:t,onClose:s}){const{toast:p}=ce(),[d,l]=o.useState("");o.useEffect(()=>{t&&l(ts())},[t]);const N=d.trim()===Xe.trim();function T(){ns(d),p({title:"Settings saved",description:"Your follow-up email prompt has been updated."}),s()}function x(){ss(),l(Xe),p({title:"Reset to default",description:"The follow-up prompt is back to the default."})}return e.jsx($e,{open:t,onOpenChange:C=>{C||s()},children:e.jsxs(De,{className:"flex sm:max-w-2xl max-h-[80dvh] flex-col overflow-hidden",children:[e.jsxs(xe,{className:"shrink-0",children:[e.jsx(he,{children:"Day Planner settings"}),e.jsx(ge,{children:"Customize how walk-in follow-up emails are written."})]}),e.jsxs("div",{className:"flex-1 space-y-2 overflow-y-auto",children:[e.jsxs("div",{className:"flex items-center justify-between gap-2",children:[e.jsx(me,{htmlFor:"followup-prompt",className:"text-sm font-semibold",children:"Follow-up email prompt"}),e.jsxs("button",{type:"button",onClick:x,disabled:N,"data-testid":"button-reset-followup-prompt",className:"flex items-center gap-1 text-xs font-medium text-muted-foreground transition-colors hover:text-foreground disabled:cursor-not-allowed disabled:opacity-40",children:[e.jsx(Ut,{className:"h-3 w-3"}),"Reset to default"]})]}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"These instructions guide the AI when drafting a follow-up email from a rep's walk-in notes. Edit the tone, length, or call-to-action to match your team."}),e.jsx(Ht,{id:"followup-prompt",value:d,onChange:C=>l(C.target.value),rows:10,className:"resize-y font-mono text-xs leading-relaxed","data-testid":"input-followup-prompt"})]}),e.jsxs(be,{className:"shrink-0",children:[e.jsx(B,{variant:"outline",onClick:s,"data-testid":"button-cancel-field-settings",children:"Cancel"}),e.jsx(B,{onClick:T,disabled:!d.trim(),"data-testid":"button-save-field-settings",children:"Save"})]})]})})}function $s({followUp:t,onClose:s}){const{toast:p}=ce(),[d,l]=o.useState(!1);o.useEffect(()=>{l(!1)},[t]);const N=t!==null,T=t?t.prospect.venueName||t.prospect.name||"venue":"",x=t?t.walkIn.email?.trim()||t.walkIn.notes:"";async function C(){if(x)try{await navigator.clipboard.writeText(x),l(!0),setTimeout(()=>l(!1),1500)}catch{p({title:"Copy failed",variant:"destructive"})}}return e.jsx($e,{open:N,onOpenChange:c=>{c||s()},children:e.jsxs(De,{className:"sm:max-w-2xl",children:[e.jsxs(xe,{children:[e.jsxs(he,{children:["Follow-Up Email â ",T]}),t&&e.jsxs(ge,{children:["Walk-in ",ft(t.walkIn.at)]})]}
1),t?.walkIn.email?e.jsx("pre",{className:"max-h-[55vh] overflow-y-auto whitespace-pre-wrap rounded-md border border-border bg-muted/30 p-4 text-sm font-sans leading-relaxed text-foreground",children:x}):e.jsxs("div",{className:"rounded-md border border-dashed border-border bg-muted/20 p-4 text-sm text-muted-foreground",children:["No email was generated for this walk-in. Notes:",e.jsx("pre",{className:"mt-2 whitespace-pre-wrap font-sans text-foreground",children:x||"â"})]}),e.jsx(be,{children:e.jsxs(B,{variant:"outline",onClick:C,className:"gap-1","data-testid":"button-copy-followup-email",children:[d?e.jsx(lt,{className:"h-4 w-4"}):e.jsx(ct,{className:"h-4 w-4"}),d?"Copied!":"Copy"]})})]})})}function Ws({onSelectProspect:t,onSelectCustomer:s,dataVersion:p=0}={}){const[d,l]=o.useState([]),[N,T]=o.useState(!0),[x,C]=o.useState("all"),[c,u]=o.useState(null),[v,b]=o.useState(!1),[g,L]=o.useState(()=>de(new Date)),[S,$]=o.useState("week"),_=o.useMemo(()=>Array.from({length:7},(m,j)=>{const Q=new Date(g);return Q.setDate(g.getDate()+j),Q}),[g]),X=m=>{const j=new Date(g);j.setDate(g.getDate()+m*(S==="day"?1:7)),L(j)},z=m=>{m!==S&&(m==="week"&&L(de(g)),$(m))},{data:D}=St();o.useEffect(()=>{if(D===void 0)return;let m=!1;return Promise.resolve($t(D)).then(j=>Dt(j)).then(j=>{m||l(j)}).catch(()=>{}).finally(()=>{m||T(!1)}),()=>{m=!0}},[D]);const M=o.useMemo(()=>d.filter(hs).sort((m,j)=>(m.name??"").localeCompare(j.name??"")),[d]),U=o.useMemo(()=>{const m={};return M.forEach((j,Q)=>{m[j.id]=gs(Q)}),m},[M]);o.useEffect(()=>{x!=="all"&&!M.some(m=>m.id===x)&&C("all")},[M,x]);const R=M.find(m=>m.id===x);return e.jsxs("div",{className:"w-full px-6 pt-6",children:[e.jsxs("div",{className:"mb-6 flex items-center gap-3",children:[e.jsx("div",{className:"flex h-10 w-10 items-center justify-center rounded-xl bg-accent/10",children:e.jsx(Pt,{className:"h-5 w-5 text-accent"})}),e.jsxs("div",{children:[e.jsx("h1",{className:"text-2xl font-bold tracking-tight text-foreground",children:"Day Planner"}),e.jsx("p",{className:"text-sm text-muted-foreground",children:"Account Executives and Business Development Reps"})]}),e.jsx("button",{onClick:()=>b(!0),"data-testid":"button-field-settings","aria-label":"Day Planner settings",title:"Day Planner settings",className:"ml-auto flex h-9 w-9 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:e.jsx(Tt,{className:"h-5 w-5"})})]}),N?e.jsxs("div",{className:"flex items-center justify-center gap-2 py-16 text-muted-foreground",children:[e.jsx(Me,{className:"h-5 w-5 animate-spin"}),e.jsx("span",{className:"text-sm",children:"Loading field teamâ¦"})]}):e.jsxs(e.Fragment,{children:[e.jsxs("div",{className:"mb-6 flex flex-nowrap items-center gap-1 overflow-x-auto border-b border-border [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden",children:[e.jsx("button",{onClick:()=>C("all"),"data-testid":"tab-field-all",className:`relative shrink-0 whitespace-nowrap px-4 py-2.5 text-sm font-medium transition-colors ${x==="all"?"text-foreground after:absolute after:bottom-0 after:left-0 after:right-0 after:h-0.5 after:bg-accent":"text-muted-foreground hover:text-foreground"}`,children:"All"}),M.map(m=>e.jsx("button",{onClick:()=>C(m.id),"data-testid":`tab-field-${m.id}`,className:`relative shrink-0 whitespace-nowrap px-4 py-2.5 text-sm font-medium transition-colors ${x===m.id?"text-foreground after:absolute after:bottom-0 after:left-0 after:right-0 after:h-0.5 after:bg-accent":"text-muted-foreground hover:text-foreground"}`,children:Ae(m.name)}
1,m.id))]}),M.length===0?e.jsxs("div",{className:"flex flex-col items-center gap-2 rounded-xl border border-dashed border-border bg-muted/20 px-6 py-10 text-center",children:[e.jsx("p",{className:"text-sm font-medium text-foreground",children:"No team members yet"}),e.jsx("p",{className:"text-xs text-muted-foreground",children:"Account Executives (Field Sales department) and Business Development Reps will appear here as tabs."})]}):x==="all"?e.jsxs("div",{children:[e.jsxs("div",{className:"mb-4 flex items-center justify-between gap-2",children:[e.jsx("h3",{className:"text-sm font-semibold text-foreground",children:S==="day"?pt(g):`Week of ${mt(_[0],_[6])}`}),e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsxs("div",{className:"flex overflow-hidden rounded-md border border-border text-xs",children:[e.jsx("button",{onClick:()=>z("week"),"data-testid":"button-all-mode-week","aria-pressed":S==="week",className:`px-2.5 py-1 font-medium transition-colors ${S==="week"?"bg-accent text-accent-foreground":"text-muted-foreground hover:bg-muted hover:text-foreground"}`,children:"Week"}),e.jsx("button",{onClick:()=>z("day"),"data-testid":"button-all-mode-day","aria-pressed":S==="day",className:`px-2.5 py-1 font-medium transition-colors ${S==="day"?"bg-accent text-accent-foreground":"text-muted-foreground hover:bg-muted hover:text-foreground"}`,children:"Day"})]}),e.jsxs("div",{className:"flex items-center gap-1",children:[e.jsx("button",{onClick:()=>X(-1),"data-testid":"button-all-week-prev","aria-label":S==="day"?"Previous day":"Previous week",className:"flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:e.jsx(rt,{className:"h-4 w-4"})}),e.jsx("button",{onClick:()=>{if(S==="day"){const m=new Date;m.setHours(0,0,0,0),L(m)}else L(de(new Date))},"data-testid":"button-all-week-today",className:"rounded-md px-2 py-1 text-xs font-medium text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:"Today"}),e.jsx("button",{onClick:()=>X(1),"data-testid":"button-all-week-next","aria-label":S==="day"?"Next day":"Next week",className:"flex h-7 w-7 items-center justify-center rounded-md text-muted-foreground transition-colors hover:bg-muted hover:text-foreground",children:e.jsx(ot,{className:"h-4 w-4"})})]})]})]}),e.jsx("div",{className:"space-y-4",children:M.map(m=>e.jsxs("div",{children:[e.jsx(et,{member:m,color:U[m.id]}),e.jsx(at,{member:m,dataVersion:p,onViewEmail:u,onSelectProspect:t}),e.jsx(st,{member:m,weekStart:g,onWeekStartChange:L,mode:S,onModeChange:z,showHeader:!1,onSelectProspect:t,onSelectCustomer:s,dataVersion:p})]},m.id))})]}):R?e.jsxs("div",{children:[e.jsx(et,{member:R,color:U[R.id]}),e.jsx(at,{member:R,dataVersion:p,onViewEmail:u,onSelectProspect:t},`fu-${R.id}`),e.jsx(st,{member:R,onSelectProspect:t,onSelectCustomer:s,dataVersion:p},R.id)]}):null]}),e.jsx($s,{followUp:c,onClose:()=>u(null)}),e.jsx(Ss,{open:v,onClose:()=>b(!1)})]})}export{js as CreatePlanDialog,Ns as DayPlanDialog,st as WeekCalendar,Ws as default,hs as isDayPlannerMember};
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.