1import{r as n,j as e,t as Y,bf as J}from"./index-BTqgdxf7.js";import{l as Z,m as ee,c as te,n as se}from"./agent-api-B8XCNRjz.js";function O(s){return new Intl.NumberFormat("en-US",{style:"currency",currency:"USD",minimumFractionDigits:0,maximumFractionDigits:0}).format(s/100)}function ne(s){return new Date(s).toLocaleTimeString([],{hour:"numeric",minute:"2-digit"})}const V="rs_agent_conv_";function ae(s){try{return localStorage.getItem(`${V}${s}`)}catch{return null}}function re(s,o){try{localStorage.setItem(`${V}${s}`,o)}catch{}}function oe(){return e.jsxs("div",{className:"flex items-center gap-1 px-4 py-3","aria-label":"Agent is typing",children:[e.jsx("span",{className:"w-2 h-2 rounded-full bg-muted animate-bounce",style:{animationDelay:"0ms"}}),e.jsx("span",{className:"w-2 h-2 rounded-full bg-muted animate-bounce",style:{animationDelay:"150ms"}}),e.jsx("span",{className:"w-2 h-2 rounded-full bg-muted animate-bounce",style:{animationDelay:"300ms"}})]})}function ie({card:s,primaryColor:o}){return e.jsxs("div",{className:"rounded-lg border border-border bg-surface p-3 space-y-2 mt-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-6 h-6 rounded flex items-center justify-center shrink-0",style:{backgroundColor:o},children:e.jsx("svg",{className:"w-3.5 h-3.5 text-white",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"})})}),e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Repair Estimate"})]}),e.jsxs("div",{className:"text-center py-2",children:[e.jsx("p",{className:"text-xs text-muted mb-0.5",children:"Estimated Cost"}),e.jsxs("p",{className:"text-xl font-bold font-mono text-foreground",children:[O(s.totalMin)," â ",O(s.totalMax)]})]}),s.vehicle.make&&e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"text-muted",children:"Vehicle"}),e.jsxs("span",{className:"text-foreground",children:[s.vehicle.year?`${s.vehicle.year} `:"",s.vehicle.make," ",s.vehicle.model]})]}),s.damageAreas.length>0&&e.jsx("div",{className:"space-y-1 pt-1 border-t border-border",children:s.damageAreas.map((a,i)=>e.jsxs("div",{className:"flex justify-between text-xs",children:[e.jsx("span",{className:"text-muted",children:a.location}),e.jsxs("span",{className:"text-foreground capitalize",children:[a.type," (",a.severity,")"]})]},i))})]})}function le({card:s,primaryColor:o}){return e.jsxs("div",{className:"rounded-lg border border-border bg-surface p-3 space-y-2 mt-1",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx("div",{className:"w-6 h-6 rounded flex items-center justify-center shrink-0",style:{backgroundColor:o},children:e.jsx("svg",{className:"w-3.5 h-3.5 text-white",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"})})}),e.jsx("span",{className:"text-sm font-semibold text-foreground",children:"Appointment Confirmed"})]}),e.jsxs("div",{className:"space-y-1.5 text-sm",children:[e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted",children:"Date"}),e.jsx("span",{className:"text-foreground font-medium",children:s.date})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted",children:"Time"}),e.jsx("span",{className:"text-foreground font-medium",children:s.time})]}),e.jsxs("div",{className:"flex justify-between",children:[e.jsx("span",{className:"text-muted",children:"Location"}),e.jsx("span",{className:"text-foreground text-right max-w-[60%]",children:s.shopName})]}),s.address&&e.jsx("p",{className:"text-xs text-muted pt-1 border-t border-border",children:s.address})]})]})}function ce({url:s,onExpand:o}){return e.jsx("button",{type:"button",className:"block rounded-lg overflow-hidden mt-1 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2",onClick:()=>o(s),"aria-label":"Expand image",children:e.jsx("img",{src:s,alt:"Uploaded damage photo",className:"w-full max-w-[200px] h-auto rounded-lg object-cover",loading:"lazy"})})}function de({url:s,onClose:o}){return n.useEffect(()=>{function a(i){i.key==="Escape"&&o()}return document.addEventListener("keydown",a),()=>document.removeEventListener("keydown",a)},[o]),e.jsxs("div",{className:"fixed inset-0 z-50 flex items-center justify-center bg-black/80 p-4",onClick:o,role:"dialog","aria-label":"Expanded image",children:[e.jsx("img",{src:s,alt:"Expanded damage photo",className:"max-w-full max-h-full rounded-lg object-contain",onClick:a=>a.stopPropagation()}),e.jsx("button",{type:"button",className:"absolute top-4 right-4 w-10 h-10 rounded-full bg-surface/80 flex items-center justify-center text-foreground hover:bg-surface transition-colors",onClick:o,"aria-label":"Close",children:e.jsx("svg",{className:"w-5 h-5",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})})]})}function ue({replies:s,onSelect:o,primaryColor:a}){return e.jsx("div",{className:"flex flex-wrap gap-2 mt-2",children:s.map(i=>e.jsx("button",{type:"button",className:"px-3 py-1.5 rounded-full text-xs font-medium border transition-colors hover:opacity-90",style:{borderColor:a,color:a},onClick:()=>
1o(i.value),children:i.label},i.value))})}function me({file:s,previewUrl:o,onRemove:a,onSend:i,primaryColor:x}){return e.jsxs("div",{className:"p-3 border-t border-border bg-surface",children:[e.jsxs("div",{className:"relative inline-block",children:[e.jsx("img",{src:o,alt:`Preview: ${s.name}`,className:"w-20 h-20 rounded-lg object-cover"}),e.jsx("button",{type:"button",className:"absolute -top-1.5 -right-1.5 w-5 h-5 rounded-full bg-destructive flex items-center justify-center",onClick:a,"aria-label":"Remove photo",children:e.jsx("svg",{className:"w-3 h-3 text-white",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2.5,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 18L18 6M6 6l12 12"})})})]}),e.jsxs("div",{className:"flex items-center gap-2 mt-2",children:[e.jsx("span",{className:"text-xs text-muted truncate flex-1",children:s.name}),e.jsx("button",{type:"button",className:"px-3 py-1 rounded-md text-xs font-medium text-white transition-opacity hover:opacity-90",style:{backgroundColor:x},onClick:i,children:"Send Photo"})]})]})}function he({shopId:s,shopConfig:o}){const a=o.primaryColor||"#3B82F6",i=o.name||"Auto Repair",[x,c]=n.useState([]),[p,y]=n.useState(""),[N,S]=n.useState(null),[u,m]=n.useState(!1),[d,w]=n.useState(!1),[E,T]=n.useState(null),[h,L]=n.useState(null),[A,D]=n.useState(!1),[M,$]=n.useState(!0),P=n.useRef(null),U=n.useRef(null),B=n.useRef(null),I=n.useRef(null),W=n.useRef(null);n.useEffect(()=>{const t=typeof AudioContext<"u"?new AudioContext:null;return t&&(W.current=null),()=>{t==null||t.close()}},[]);const z=n.useCallback(()=>{if(M)try{const t=new AudioContext,r=t.createOscillator(),l=t.createGain();r.connect(l),l.connect(t.destination),r.frequency.value=800,r.type="sine",l.gain.setValueAtTime(.1,t.currentTime),l.gain.exponentialRampToValueAtTime(.001,t.currentTime+.3),r.start(t.currentTime),r.stop(t.currentTime+.3),setTimeout(()=>t.close(),500)}catch{}},[M]);n.useEffect(()=>{var t;(t=P.current)==null||t.scrollIntoView({behavior:"smooth"})},[x,d]),n.useEffect(()=>{let t=!1;async function r(){m(!0);const l=ae(s);if(l)try{const j=await te(l);if(!t&&j.data.conversation.status==="active"){S(l),c(j.data.messages.map(v=>({...v,isStreaming:!1}))),m(!1);return}}catch{}try{const j=await se(s);t||(S(j.data.id),re(s,j.data.id),c([{id:"welcome",role:"agent",content:`Hi! I'm ${i}'s AI assistant. I can help you get a repair estimate, schedule an appointment, or answer questions about our services. How can I help you today?`,quickReplies:[{label:"Get a repair estimate",value:"I need a repair estimate"},{label:"Schedule an appointment",value:"I want to schedule an appointment"},{label:"Ask a question",value:"I have a question about your services"}],createdAt:new Date().toISOString()}]))}catch{t||c([{id:"error",role:"agent",content:"Sorry, I am having trouble connecting right now. Please try again in a moment.",createdAt:new Date().toISOString()}])}t||m(!1)}return r(),()=>{t=!0}},[s,i]),n.useEffect(()=>()=>{var t;(t=I.current)==null||t.call(I)},[]);const C=n.useCallback((t,r)=>{if(!N||!t.trim()&&!(r!=null&&r.length)||d)return;const l={id:`cust-${Date.now()}`,role:"customer",content:t.trim(),imageUrls:r,createdAt:new Date().toISOString()};c(k=>[...k.map(g=>g.quickReplies?{...g,quickReplies:void 0}:g),l]),y(""),w(!0);let j="";const v=`agent-${Date.now()}`;I.current=Z(N,t.trim(),r,{onToken(k){j+=k,c(f=>f.find(b=>b.id===v)?f.map(b=>b.id===v?{...b,content:j}:b):[...f,{id:v,role:"agent",content:j,createdAt:new Date().toISOString(),isStreaming:!0}])},onToolCall(k,f){f==="started"&&c(g=>g.find(X=>X.id===v)?g:[...g,{id:v,role:"agent",content:"Analyzing...",createdAt:new Date().toISOString(),isStreaming:!0}])},onComplete(k){w(!1),I.current=null,c(f=>[...f.filter(b=>b.id!==v),{...k,isStreaming:!1}]),z()},onError(k){w(!1),I.current=null,c(f=>[...f.filter(b=>b.id!==v),{id:`err-${Date.now()}`,role:"agent",content:k||"Sorry, something went wrong. Please try again.",createdAt:new Date().toISOString()}])}})},[N,d,z]),q=n.useCallback(t=>{t.preventDefault(),p.trim()&&C(p)},[p,C]),H=n.useCallback(t=>{C(t)},[C]),R=n.useCallback(t=>{if(!["image/jpeg","image/png","image/webp"].includes(t.type)||t.size>10*1024*1024)return;const l=URL.createObjectURL(t);L({file:t,previewUrl:l})},[]),_=n.useCallback(()=>{h&&(URL.revokeObjectURL(h.previewUrl),L(null))}
1,[h]),F=n.useCallback(async()=>{if(!(!h||A)){D(!0);try{const t=await ee(h.file);URL.revokeObjectURL(h.previewUrl),L(null),C(p.trim()||"Here is a photo of the damage.",[t.data.url])}catch{const t=h.previewUrl;L(null),C(p.trim()||"Here is a photo of the damage.",[t])}finally{D(!1)}}},[h,A,p,C]),G=n.useCallback(t=>{var l;const r=(l=t.target.files)==null?void 0:l[0];r&&R(r),t.target.value=""},[R]),K=n.useCallback(t=>{t.preventDefault();const r=t.dataTransfer.files[0];r!=null&&r.type.startsWith("image/")&&R(r)},[R]),Q=n.useMemo(()=>({"--chat-primary":a}),[a]);return e.jsxs("div",{className:"flex flex-col h-full bg-background text-foreground",style:Q,onDragOver:t=>t.preventDefault(),onDrop:K,children:[e.jsxs("div",{className:"flex items-center gap-3 px-4 py-3 shrink-0",style:{backgroundColor:a},children:[o.logoUrl?e.jsx("img",{src:o.logoUrl,alt:i,className:"w-8 h-8 rounded-full object-cover bg-white/20"}):e.jsx("div",{className:"w-8 h-8 rounded-full bg-white/20 flex items-center justify-center",children:e.jsx("svg",{className:"w-4 h-4 text-white",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"})})}),e.jsxs("div",{className:"flex-1 min-w-0",children:[e.jsx("p",{className:"text-sm font-semibold text-white truncate",children:i}),e.jsx("p",{className:"text-xs text-white/70",children:"AI Assistant"})]}),e.jsx("div",{className:"flex items-center gap-1",children:e.jsx("button",{type:"button",className:"w-8 h-8 rounded-full flex items-center justify-center text-white/70 hover:text-white hover:bg-white/10 transition-colors",onClick:()=>$(t=>!t),"aria-label":M?"Mute notifications":"Unmute notifications",children:M?e.jsx("svg",{className:"w-4 h-4",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M15.536 8.464a5 5 0 010 7.072M17.95 6.05a8 8 0 010 11.9M6.5 8H4a1 1 0 00-1 1v6a1 1 0 001 1h2.5l4.5 4V4l-4.5 4z"})}):e.jsxs("svg",{className:"w-4 h-4",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:[e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M5.586 15H4a1 1 0 01-1-1v-4a1 1 0 011-1h1.586l4.707-4.707C10.923 3.663 12 4.109 12 5v14c0 .891-1.077 1.337-1.707.707L5.586 15z"}),e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M17 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2"})]})})})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto px-4 py-3 space-y-3",role:"log","aria-label":"Chat messages","aria-live":"polite",children:[u&&e.jsx("div",{className:"flex items-center justify-center py-8",children:e.jsx("div",{className:"text-sm text-muted",children:"Connecting..."})}),x.map(t=>{var r;return e.jsx("div",{className:`flex ${t.role==="customer"?"justify-end":"justify-start"}`,children:e.jsxs("div",{className:`max-w-[85%] ${t.role==="customer","order-1"}`,children:[e.jsxs("div",{className:`rounded-2xl px-3.5 py-2.5 text-sm leading-relaxed ${t.role==="customer"?"text-white rounded-br-md":"bg-elevated text-foreground rounded-bl-md"}`,style:t.role==="customer"?{backgroundColor:a}:void 0,children:[(r=t.imageUrls)==null?void 0:r.map(l=>e.jsx(ce,{url:l,onExpand:T},l)),t.content&&e.jsxs("p",{className:"whitespace-pre-wrap break-words",children:[t.content,t.isStreaming&&e.jsx("span",{className:"inline-block w-1.5 h-4 ml-0.5 bg-current animate-pulse rounded-sm"})]}),t.estimateCard&&e.jsx(ie,{card:t.estimateCard,primaryColor:a}),t.appointmentCard&&e.jsx(le,{card:t.appointmentCard,primaryColor:a})]}),t.quickReplies&&t.quickReplies.length>0&&e.jsx(ue,{replies:t.quickReplies,onSelect:H,primaryColor:a}),e.jsx("p",{className:`text-[10px] text-subtle mt-1 ${t.role==="customer"?"text-right":"text-left"}`,children:ne(t.createdAt)})]})},t.id)}),d&&!x.some(t=>t.isStreaming)&&e.jsx("div",{className:"flex justify-start",children:e.jsx("div",{className:"bg-elevated rounded-2xl rounded-bl-md",children:e.jsx(oe,{})})}),e.jsx("div",{ref:P})]}),h&&e.jsx(me,{file:h.file,previewUrl:h.previewUrl,onRemove:_,onSend:F,primaryColor:a}),e.jsxs("form",{onSubmit:q,className:"px-3 py-2.5 border-t border-border shrink-0 flex items-end gap-2",children:[e.jsx("button",{type:"button",className:"w-9 h-9 rounded-full flex items-center justify-center text-muted hover:text-foreground hover:bg-elevated transition-colors shrink-0",onClick:()=>{var t;return(t=U.current)==null?void 0:t.click()},"aria-label":"Upload photo",disabled:A||d,children:e.jsxs("svg",{className:"w-5 h-5",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:1.5,children:[e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6.827 6.175A2.31 2.31 0 015.186 7.23c-.38.054-.757.112-1.134.175C2.999 7.58 2.25 8.507 2.25 9.574V18a2.25 2.25 0 002.25 2.25h15A2.25 2.25 0 0021.75 18V9.574c0-1.067-.75-1.994-1.802-2.169a47.865 47.865 0 00-1.134-.175 2.31 2.31 0 01-1.64-1.055l-.822-1.316a2.192 2.192 0 00-1.736-1.039 48.774 48.774 0 00-5.232 0 2.192 2.192 0 00-1.736 1.039l-.821 1.316z"}),e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M16.5 12.75a4.5 4.5 0 11-9 0 4.5 4.5 0 019 0z"})]})}),e.jsx("input",{ref:U,type:"file",accept:"image/jpeg,image/png,image/webp",capture:"environment",onChange:G,className:"hidden"}),e.jsx("input",{ref:B,type:"text",value:p,onChange:t=>y(t.target.value),placeholder:d?"Agent is responding...":"Type a message...",disabled:u||d,className:"flex-1 h-9 rounded-full bg-elevated border border-border px-4 text-sm text-foreground placeholder:text-muted focus-visible:outline-none focus-visible:ring-1 disabled:opacity-50",style:{"--tw-ring-color":a},"aria-label":"Message input"}),e.jsx("button",{type:"submit",disabled:!p.trim()||u||d,className:"w-9 h-9 rounded-full flex items-center justify-center text-white transition-opacity hover:opacity-90 disabled:opacity-40 shrink-0",style:{backgroundColor:a},"aria-label":"Send message",children:e.jsx("svg",{className:"w-4 h-4",fill:"none",stroke:"currentColor",viewBox:"0 0 24 24",strokeWidth:2,children:e.jsx("path",{strokeLinecap:"round",strokeLinejoin:"round",d:"M6 12L3.269 3.126A59.768 59.768 0 0121.485 12 59.77 59.77 0 013.27 20.876L5.999 12zm0 0h7.5"})})})]}),e.jsx("div",{className:"px-4 py-1.5 text-center border-t border-border shrink-0",children:e.jsx("a",{href:"https://repairsnap.ai/for-shops",target:"_blank",rel:"noopener noreferrer",className:"text-[10px] text-subtle hover:text-muted transition-colors",children:"Powered by RepairSnap"})}),E&&e.jsx(de,{url:E,onClose:()=>T(null)})]})}function fe(){const[s]=Y(),o=s.get("shop")??"",a=s.get("color"),[i,x]=n.useState({status:"loading"}),[c,p]=n.useState(null),y=(c==null?void 0:c.shopId)||o,N=(c==null?void 0:c.color)||a;n.useEffect(()=>{function u(m){var d;((d=m.data)==null?void 0:d.type)==="repairsnap-chat-config"&&p({shopId:m.data.shopId,color:m.data.color,position:m.data.position})}return window.addEventListener("message",u),window.parent.postMessage({type:"repairsnap-chat-ready"},"*"),()=>window.removeEventListener("message",u)},[]),n.useEffect(()=>{if(!y){x({status:"error",message:"No shop specified."});return}let u=!1;async function m(){x({status:"loading"});try{const d=await J(y);if(!u){const w={...d.data};
1N&&(w.primaryColor=N),x({status:"ready",shopConfig:w,shopId:w.id||y,shopSlug:w.slug||y})}}catch{u||x({status:"error",message:"Could not load chat configuration."})}}return m(),()=>{u=!0}},[y,N]),n.useEffect(()=>{function u(){const d=document.documentElement.scrollHeight;window.parent.postMessage({type:"repairsnap-chat-resize",height:d},"*")}const m=new ResizeObserver(u);return m.observe(document.body),u(),()=>m.disconnect()},[i]);const S=n.useMemo(()=>({height:"100vh",width:"100%",overflow:"hidden"}),[]);return i.status==="loading"?e.jsx("div",{className:"min-h-screen bg-background flex items-center justify-center",style:S,children:e.jsx("div",{className:"text-sm text-muted",children:"Loading chat..."})}):i.status==="error"?e.jsx("div",{className:"min-h-screen bg-background flex items-center justify-center p-4",style:S,children:e.jsx("div",{className:"text-center space-y-2",children:e.jsx("p",{className:"text-sm text-muted",children:i.message})})}):e.jsx("div",{style:S,children:e.jsx(he,{shopId:i.shopId,shopConfig:i.shopConfig})})}export{fe 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.