1import{j as e}from"./ui-vendor-B12AOeKi.js";import{e as k,f as O,L as Y,r as b}from"./react-vendor-DZt7wVAJ.js";import{ai as J,u as C,Q as S,bh as X,c4 as Z,aw as D,bz as U,aH as ee,F as ae,au as se,_ as te,C as re,a4 as v,ad as ne,ae as ie,B as c,bx as M,by as R,aC as oe,af as le,ag as ce,s as x,j as de,aP as me,a8 as y,aQ as he,aR as pe,c5 as xe,c6 as ue,c7 as fe,a1 as ge,a0 as je,a as Ne,p as ve,q as _,am as P,ap as be,g as N,n as _e,k as z,I as we}from"./index-BZo64Z7H.js";import{u as ye}from"./useChatRealtime-B1BwZt_n.js";import{T as ke,b as Ce,c as I,a as L}from"./tabs-BcL48lVI.js";import{A as Se,a as Te,b as Ae,c as qe,d as De,e as Me,g as Re,f as Pe}from"./alert-dialog-Diwn5aYx.js";import{A as F}from"./AppLogo-ziNTxn37.js";import{H as ze}from"./handshake-DvHNPF0E.js";import{G as Ie}from"./graduation-cap-DpjNw0JC.js";import{T as Le}from"./trophy-MDsLPZw1.js";import{G as Fe}from"./globe-CjSiEoEQ.js";import{T as Ee}from"./trending-down-C5bokXPm.js";import{L as We}from"./layout-grid-BAuwMpS1.js";import{B as Be}from"./BrandLogo-DkK18YJt.js";import{S as Oe}from"./sparkles-BkfsvibN.js";import{f as E}from"./formatDistanceToNow-DKph0Yj9.js";import"./charts-vendor-B6q1L48j.js";import"./supabase-vendor-dOy44Gxq.js";import"./animation-vendor-BAVy02gd.js";import"./useRealtimeSubscription-D-SMjhmY.js";import"./ai-dealer-360-logo-2026-TQeVXUxP.js";import"./constructNow-BCx5Dr-v.js";import"./endOfDay-CG6Ooo0n.js";import"./differenceInCalendarMonths-iM3p2kUt.js";import"./endOfMonth-Nvm-8vcq.js";import"./getRoundingMethod-D0I6Z7xl.js";import"./differenceInMilliseconds-BRR8NVfJ.js";/** 2 * @license lucide-react v0.462.0 - ISC 3 * 4 * This source code is licensed under the ISC license. 5 * See the LICENSE file in the root directory of this source tree. 6 */const Ge=J("MessagesSquare",[["path",{d:"M14 9a2 2 0 0 1-2 2H6l-4 4V4a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2z",key:"p1xzt8"}],["path",{d:"M18 9h2a2 2 0 0 1 2 2v11l-4-4h-6a2 2 0 0 1-2-2v-1",key:"1cx29u"}]]),W={"Lot Manager":{path:"/vehicles",icon:re,color:"text-blue-600 dark:text-blue-400"},"Special Order Parts":{path:"/sop",icon:te,color:"text-purple-600 dark:text-purple-400"},"Registration Tracker":{path:"/registration-tracker",icon:se,color:"text-green-600 dark:text-green-400"},"Quote Builder":{path:"/quote",icon:D,color:"text-emerald-600 dark:text-emerald-400"},"Pre-Write":{path:"/pre-write",icon:ae,color:"text-indigo-600 dark:text-indigo-400"},"Missed Opportunities":{path:"/missed-opps",icon:Ee,color:"text-orange-600 dark:text-orange-400"},"Vehicle Scraper":{path:"/vehicle-scraper",icon:Fe,color:"text-cyan-600 dark:text-cyan-400"},Messenger:{path:"/messenger",icon:ee,color:"text-pink-600 dark:text-pink-400"},"Website Chat":{path:"/chat",icon:Ge,color:"text-teal-600 dark:text-teal-400"},"Road to the Sale":{path:"/road-to-sale",icon:U,color:"text-amber-600 dark:text-amber-400"},Scoreboard:{path:"/scoreboard",icon:Le,color:"text-yellow-600 dark:text-yellow-400"},Commission:{path:"/commission",icon:D,color:"text-emerald-600 dark:text-emerald-400"}
6,Reconcile:{path:"/reconcile",icon:Z,color:"text-rose-600 dark:text-rose-400"},"Sales Trainer Pro":{path:"/sales-trainer",icon:Ie,color:"text-violet-600 dark:text-violet-400"},Acquire360:{path:"/acquire",icon:ze,color:"text-sky-500 dark:text-sky-400"}};function Ve(){const i=k(),o=O(),{currentUser:t}=C(),n=(t==null?void 0:t.roleCode)==="admin",{data:d}=S({queryKey:["user-app-access",t==null?void 0:t.id],queryFn:async()=>{if(!(t!=null&&t.id))return[];const{data:r,error:p}=await x.from("registration_user_app_access").select("*").eq("user_id",t.id);if(p)throw p;return r},enabled:!!t}),{data:h}=X(),g=(h||[]).filter(r=>!r.is_work_in_process).filter(r=>{const p=r.app_name;return W[p]?n?!0:(d==null?void 0:d.some(u=>u.app_name===p))||!1:!1}).map(r=>({id:r.app_id,name:r.app_name,...W[r.app_name]})),m=g.find(r=>o.pathname.startsWith(r.path));return g.length<=1?null:e.jsx(v,{componentId:"nav:app-switcher-dropdown",appName:"Platform",componentType:"dropdown",componentLabel:"App Switcher Dropdown",defaultRole:"user",children:e.jsxs(ne,{children:[e.jsx(ie,{asChild:!0,children:e.jsxs(c,{variant:"outline",size:"sm",className:"gap-1 md:gap-2 h-8 md:h-9 px-2 md:px-3 flex-shrink-0",children:[m?e.jsxs(e.Fragment,{children:[M(m.name)?e.jsx(F,{appName:m.name,sizeClassName:"h-4 w-4"}):e.jsx(m.icon,{className:`h-4 w-4 flex-shrink-0 ${m.color}`}),e.jsx("span",{className:"hidden md:inline whitespace-nowrap",children:R(m.name)})]}):e.jsxs(e.Fragment,{children:[e.jsx(We,{className:"h-4 w-4 flex-shrink-0"}),e.jsx("span",{className:"hidden md:inline whitespace-nowrap",children:"Apps"})]}),e.jsx(oe,{className:"h-3 w-3 opacity-50 flex-shrink-0"})]})}),e.jsx(le,{align:"start",className:"w-56 bg-popover z-50",children:g.map(r=>{const p=r.icon,u=o.pathname.startsWith(r.path),j=!!M(r.name);return e.jsxs(ce,{onClick:()=>i(r.path),className:`gap-2 cursor-pointer ${u?"bg-accent":""}`,children:[j?e.jsx(F,{appName:r.name,sizeClassName:"h-4 w-4"}):e.jsx(p,{className:`h-4 w-4 ${r.color}`}),e.jsx("span",{children:R(r.name)}),u&&e.jsx("span",{className:"ml-auto text-xs text-muted-foreground",children:"Current"})]},r.id)})})]})})}const He=()=>{const i=k(),{data:o=0}=S({queryKey:["release-notes-recent-count"],queryFn:async()=>{const t=new Date;t.setDate(t.getDate()-7);const{count:n,error:d}=await x.from("release_notes").select("*",{count:"exact",head:!0}).eq("is_published",!0).gte("release_date",t.toISOString().split("T")[0]);return d?0:n??0},staleTime:5*60*1e3});return e.jsxs(c,{onClick:()=>i("/whats-new"),variant:"outline",size:"icon",className:"relative h-8 w-8 md:h-9 md:w-9 flex-shrink-0",title:"What's New","aria-label":"What's New",children:[e.jsx(Oe,{className:"h-4 w-4"}),o>0&&e.jsx("span",{className:"absolute -top-1.5 -right-1.5 flex h-4 w-4 items-center justify-center rounded-full bg-primary text-[10px] font-bold text-primary-foreground",children:o})]})},B=()=>{const{isPlatformAdmin:i}=C(),{isAdmin:o}=de(),{theme:t,setTheme:n}=me(),d=O(),h=k();return["/registration-mobile","/reports/mobile","/user-management/mobile","/vehicles-mobile","/vehicles-mobile-manager"].some(r=>d.pathname.startsWith(r))?null:e.jsx(e.Fragment,{children:e.jsxs("div",{className:"flex items-center justify-between gap-1.5 md:gap-3 px-2 md:px-4 py-2 flex-wrap",style:{paddingTop:"env(safe-area-inset-top, 0px)"},children:[e.jsx(Y,{to:"/",className:"flex-shrink-0",children:e.jsx(Be,{className:"h-8 w-auto"})}),o&&!i&&e.jsxs(e.Fragment,{children:[e.jsx(c,{variant:"default",size:"icon",onClick:()=>h("/admin-control"),className:"h-9 w-9 flex-shrink-0 md:hidden",title:"Admin","aria-label":"Admin",children:e.jsx(y,{className:"h-4 w-4"})}),e.jsxs(c,{variant:"default",size:"sm",onClick:()=>h("/admin-control"),className:"gap-2 hidden md:flex flex-shrink-0",children:[e.jsx(y,{className:"h-4 w-4"}),"Admin"]})]}),i&&e.jsxs(e.Fragment,{children:[e.jsx(c,{variant:"default",size:"icon",onClick:()=>h("/system-admin"),className:"h-9 w-9 flex-shrink-0 md:hidden",title:"System Admin","aria-label":"System Admin",children:e.jsx(y,{className:"h-4 w-4"})}),e.jsxs(c,{variant:"default",size:"sm",onClick:()=>h("/system-admin"),className:"gap-2 hidden md:flex flex-shrink-0",children:[e.jsx(y,{className:"h-4 w-4"}),"System Admin"]})]}),e.jsxs("div",{className:"flex items-center gap-1.5 md:gap-3 flex-nowrap overflow-x-auto md:flex-wrap min-w-0",children:[e.jsx(Ve,{}),e.jsx(v,{componentId:"topbar:theme-toggle",appName:"Platform",componentType:"button",componentLabel:"Theme Toggle",defaultRole:"user",children:e.jsx(c,{variant:"outline",size:"icon",onClick:()=>n(t==="dark"?"light":"dark"),className:"h-8 w-8 md:h-9 md:w-9 flex-shrink-0",title:t==="dark"?"Switch to light mode":"Switch to dark mode",children:t==="dark"?e.jsx(he,{className:"h-4 w-4"}):e.jsx(pe,{className:"h-4 w-4"})})}),e.jsx(v,{componentId:"topbar:push-notifications",appName:"Platform",componentType:"button",componentLabel:"Push Notifications",defaultRole:"user",children:e.jsx(xe,{})}),e.jsx(v,{componentId:"topbar:mail-notifications",appName:"Platform",componentType:"button",componentLabel:"Mail Notifications",defaultRole:"user",children:e.jsx(ue,{})}),e.jsx(v,{componentId:"topbar:reconcile-notifications",appName:"Platform",componentType:"button",componentLabel:"Reconcile Notifications",defaultRole:"user",children:e.jsx(fe,{})}),e.jsx(v,{componentId:"topbar:whats-new",appName:"Platform",componentType:"button",componentLabel:"What's New",defaultRole:"user",children:e.jsx(He,{})}),e.jsx(ge,{variant:"compact"}),e.jsx(je,{})]})]})})};function Na(){const{selectedDealership:i}=Ne(),{currentUser:o}=C(),{toast:t}
6=ve(),[n,d]=b.useState(null),[h,g]=b.useState(!1),[m,r]=b.useState(""),[p,u]=b.useState(!1),[j,T]=b.useState(!1),{messages:G,session:s,loading:V}=ye(n),{data:w=[],refetch:H}=S({queryKey:["chat-sessions-mobile",i==null?void 0:i.code,h],queryFn:async()=>{if(!i)return[];let a=x.from("chat_sessions").select("*").eq("dealership_code",i.code).order("last_message_at",{ascending:!1});h?a=a.eq("current_status","closed"):a=a.neq("current_status","closed");const{data:l,error:f}=await a;if(f)throw f;return l},enabled:!!i,refetchInterval:5e3}),A=async a=>{const{error:l}=await x.from("chat_sessions").update({current_status:"human_active",human_assigned:(o==null?void 0:o.id)??null}).eq("id",a);l?t({title:"Error",description:l.message,variant:"destructive"}):(t({title:"Success",description:"You've taken over this chat"}),H())},Q=async()=>{if(!n)return;const{error:a}=await x.from("chat_sessions").update({current_status:"bot_active",human_assigned:null}).eq("id",n);t(a?{title:"Error",description:a.message,variant:"destructive"}:{title:"Success",description:"Chat returned to bot"})},K=async()=>{if(!n||!s)return;const{error:a}=await x.from("chat_sessions").update({current_status:"closed",ended_at:new Date().toISOString()}).eq("id",n);if(a)t({title:"Error",description:a.message,variant:"destructive"});else{t({title:"Success",description:"Chat closed"});const l=(s==null?void 0:s.lead_name)&&s.lead_name.trim(),f=(s==null?void 0:s.lead_email)&&s.lead_email.trim()||(s==null?void 0:s.lead_phone)&&s.lead_phone.trim();l&&f&&u(!0)}},q=async()=>{if(n){T(!0),u(!1);try{const{data:a,error:l}=await x.functions.invoke("send-chat-to-crm-email",{body:{sessionId:n}});if(l)throw l;if(a.success)t({title:"Success",description:"Lead information sent to CRM email successfully"});else throw new Error(a.error||"Failed to send to CRM email")}catch(a){console.error("Send to CRM email error:",a),t({title:"Error",description:a.message||"Failed to send to CRM email",variant:"destructive"})}finally{T(!1)}}},$=async a=>{if(a.preventDefault(),!n)return;const l=m.trim();if(!l)return;r("");const{error:f}=await x.from("chat_messages").insert({session_id:n,sender_type:"human",sender_id:(o==null?void 0:o.id)??null,message:l});f?t({title:"Error",description:f.message,variant:"destructive"}):await x.from("chat_sessions").update({last_message_at:new Date().toISOString()}).eq("id",n)};return i?n&&s?e.jsxs(e.Fragment,{children:[e.jsx(Se,{open:p,onOpenChange:u,children:e.jsxs(Te,{children:[e.jsxs(Ae,{children:[e.jsx(qe,{children:"Send Lead to CRM?"}),e.jsxs(De,{children:["This chat contains lead information. Would you like to send it to your CRM email address?",e.jsxs("div",{className:"mt-3 p-3 bg-muted rounded-md text-sm",children:[e.jsxs("p",{children:[e.jsx("strong",{children:"Name:"})," ",s==null?void 0:s.lead_name]}),(s==null?void 0:s.lead_email)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Email:"})," ",s.lead_email]}),(s==null?void 0:s.lead_phone)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Phone:"})," ",s.lead_phone]}),(s==null?void 0:s.inquiry_type)&&e.jsxs("p",{children:[e.jsx("strong",{children:"Inquiry:"})," ",s.inquiry_type]})]})]})]}),e.jsxs(Me,{children:[e.jsx(Re,{children:"Skip"}),e.jsxs(Pe,{onClick:q,disabled:j,children:[e.jsx(P,{className:"h-4 w-4 mr-2"}),j?"Sending...":"Send to CRM"]})]})]})}),e.jsxs("div",{className:"flex flex-col h-screen bg-background",children:[e.jsxs("div",{className:"border-b p-3 bg-card",children:[e.jsxs("div",{className:"flex items-center gap-3 mb-2",children:[e.jsx(c,{variant:"ghost",size:"icon",onClick:()=>d(null),className:"h-8 w-8",children:e.jsx(be,{className:"h-4 w-4"})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("h3",{className:"font-semibold truncate",children:s.lead_name||"Website Visitor"}),s.inquiry_type&&e.jsx(N,{variant:"secondary",className:"text-[10px] capitalize shrink-0",children:s.inquiry_type})]}),e.jsxs("div",{className:"text-xs text-muted-foreground",children:[s.lead_phone&&e.jsxs("p",{children:["Phone: ",s.lead_phone]}),s.lead_email&&e.jsxs("p",{children:["Email: ",s.lead_email]})]}),s.ended_at&&e.jsx(N,{variant:"outline",className:"mt-1 text-amber-600 border-amber-600 text-[10px]",children:"Customer ended chat"})]})]}),e.jsxs("div",{className:"flex gap-2 overflow-x-auto",children:[(s.current_status==="closed"||s.ended_at)&&s.lead_name&&(s.lead_email&&s.lead_email.trim()||s.lead_phone&&s.lead_phone.trim())&&e.jsxs(c,{size:"sm",onClick:q,disabled:j,className:"shrink-0",children:[e.jsx(P,{className:"h-4 w-4 mr-2"}),j?"Sending...":"Send to CRM"]}),!s.ended_at&&s.current_status!=="human_active"&&e.jsx(c,{size:"sm",onClick:()=>A(n),className:"shrink-0",children:"Take Over"}),!s.ended_at&&s.current_status==="human_active"&&e.jsx(c,{size:"sm",variant:"outline",onClick:Q,className:"shrink-0",children:"Return to Bot"}),!s.ended_at&&s.current_status!=="closed"&&e.jsx(c,{size:"sm",variant:"destructive",onClick:K,className:"shrink-0",children:"Close"})]})]}),e.jsx(_e,{className:"flex-1 p-4 bg-background",children:V?e.jsx("div",{className:"text-center text-sm text-muted-foreground",children:"Loading messages..."}):e.jsx("div",{className:"space-y-2",children:G.map(a=>e.jsx("div",{className:z("flex",a.sender_type==="customer"?"justify-start":"justify-end"),children:e.jsx("div",{className:z("rounded-xl px-4 py-2 max-w-[85%] text-sm whitespace-pre-wrap break-words",a.sender_type==="customer"?"bg-muted text-foreground":"bg-primary text-primary-foreground"),children:a.message})},a.id))})}),e.jsx("form",{onSubmit:$,className:"border-t p-3 bg-card",children:s.ended_at?e.jsx("div",{className:"text-center text-sm text-muted-foreground py-2",children:"Customer has ended this chat session"}):e.jsxs("div",{className:"flex gap-2",children:[e.jsx(we,{value:m,onChange:a=>r(a.target.value),placeholder:"Type your message...",disabled:s.current_status==="closed",className:"flex-1"}),e.jsx(c,{type:"submit",disabled:s.current_status==="closed",className:"shrink-0",children:"Send"})]})})]})]}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(B,{}),e.jsxs("div",{className:"p-4",children:[e.jsxs("div",{className:"mb-4",children:[e.jsx("h2",{className:"text-xl font-semibold mb-1",children:"Chat Management"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:["Dealership: ",i.name]})]}),e.jsxs(ke,{defaultValue:"active",onValueChange:a=>g(a==="archived"),children:[e.jsxs(Ce,{className:"grid w-full grid-cols-2 mb-4",children:[e.jsx(I,{value:"active",children:"Active Chats"}),e.jsx(I,{value:"archived",children:"Archived"})]}),e.jsx(L,{value:"active",className:"space-y-2 mt-0",children:w.length===0?e.jsx(_,{className:"p-6 text-center text-sm text-muted-foreground",children:"No active chats found"}):w.map(a=>e.jsxs(_,{className:"p-4 cursor-pointer hover:bg-accent/50 transition-colors",onClick:()=>d(a.id),children:[e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h4",{className:"font-semibold text-sm truncate",children:a.lead_name||"Website Visitor"}),a.inquiry_type&&e.jsx(N,{variant:"secondary",className:"text-[10px] capitalize shrink-0",children:a.inquiry_type})]}),e.jsxs("div",{className:"text-xs text-muted-foreground space-y-0.5",children:[a.lead_phone&&e.jsxs("p",{children:["Phone: ",a.lead_phone]}),a.lead_email&&e.jsxs("p",{children:["Email: ",a.lead_email]})]})]}),e.jsxs("div",{className:"text-right shrink-0",children:[e.jsx(N,{variant:"outline",className:"mb-1 text-[10px]",children:a.current_status==="awaiting_human"?"Needs attention":a.current_status}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:E(new Date(a.last_message_at),{addSuffix:!0})})]})]}),a.current_status==="awaiting_human"&&e.jsx(c,{size:"sm",className:"mt-2 w-full",onClick:l=>{l.stopPropagation(),A(a.id)},children:"Take over chat"})]},a.id))}),e.jsx(L,{value:"archived",className:"space-y-2 mt-0",children:w.length===0?e.jsx(_,{className:"p-6 text-center text-sm text-muted-foreground",children:"No archived chats found"}):w.map(a=>e.jsx(_,{className:"p-4 cursor-pointer hover:bg-accent/50 transition-colors",onClick:()=>d(a.id),children:e.jsxs("div",{className:"flex items-start justify-between gap-2",children:[e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-1",children:[e.jsx("h4",{className:"font-semibold text-sm truncate",children:a.lead_name||"Website Visitor"}),a.inquiry_type&&e.jsx(N,{variant:"secondary",className:"text-[10px] capitalize shrink-0",children:a.inquiry_type})]}),e.jsxs("div",{className:"text-xs text-muted-foreground space-y-0.5",children:[a.lead_phone&&e.jsxs("p",{children:["Phone: ",a.lead_phone]}),a.lead_email&&e.jsxs("p",{children:["Email: ",a.lead_email]})]})]}),e.jsxs("div",{className:"text-right shrink-0",children:[e.jsx(N,{variant:"outline",className:"mb-1 text-[10px]",children:"Closed"}),e.jsx("p",{className:"text-[10px] text-muted-foreground",children:E(new Date(a.last_message_at),{addSuffix:!0})})]})]})},a.id))})]})]})]}):e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx(B,{}),e.jsx("div",{className:"p-4",children:e.jsx(_,{className:"p-6",children:"Please select a dealership to view chats."})})]})}export{Na 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.