import{j as e}from"./vendor-ui-Di8ht53r.js";import{r as a}from"./vendor-react-CQmvUQ48.js";import{c as A,u as D,B as m,X as F}from"./index-dhtZamXm.js";import{I as E}from"./input-DHTP4p-F.js";import{T as O}from"./textarea-Cjkw6JzO.js";import{s as h}from"./client-Ck-HpqZ3.js";import{i as P,R as _}from"./rateLimitError-ChGrtg6L.js";import{M}from"./LazyChatbot-DSa3HMVr.js";import{M as $}from"./mail-Bvm72uxM.js";import{o as q,s as S,Z as B}from"./vendor-forms-DYr4pOjH.js";import"./vendor-charts-DHIBAAe4.js";import"./vendor-query-X4y0eUJS.js";/**
 * @license lucide-react v0.462.0 - ISC
 *
 * This source code is licensed under the ISC license.
 * See the LICENSE file in the root directory of this source tree.
 */const K=A("Send",[["path",{d:"M14.536 21.686a.5.5 0 0 0 .937-.024l6.5-19a.496.496 0 0 0-.635-.635l-19 6.5a.5.5 0 0 0-.024.937l7.93 3.18a2 2 0 0 1 1.112 1.11z",key:"1ffxy3"}],["path",{d:"m21.854 2.147-10.94 10.939",key:"12cjpa"}]]),W=q({email:S().trim().email("Invalid email address").max(255,"Email must be less than 255 characters"),message:S().trim().min(1,"Message is required").max(2e3,"Message must be less than 2000 characters")}),ae=({defaultOpen:I=!1})=>{const[x,p]=a.useState(I),[f,r]=a.useState([{role:"assistant",content:"Hi! I'm here to help answer questions about automation, AI, and how Pinnacle Consulting Group can help your business. What would you like to know?"}]),[d,g]=a.useState(""),[n,i]=a.useState(!1),[b,l]=a.useState(!1),[y,j]=a.useState(""),[v,w]=a.useState(""),N=a.useRef(null),{toast:u}=D(),T=()=>{N.current?.scrollIntoView({behavior:"smooth"})};a.useEffect(()=>{T()},[f]);const C=async()=>{if(!d.trim()||n)return;const s=d.trim();g(""),r(t=>[...t,{role:"user",content:s}]),i(!0);try{const{data:t,error:o}=await h.functions.invoke("chatbot",{body:{messages:[...f,{role:"user",content:s}]}});if(o)throw o;t.error?(r(c=>[...c,{role:"assistant",content:t.fallback||"I apologize, but I'm having trouble right now. Would you like to submit a contact form instead?"}]),l(!0)):r(c=>[...c,{role:"assistant",content:t.response}])}catch(t){console.error("Chat error:",t),r(o=>[...o,{role:"assistant",content:"I apologize, but I'm experiencing technical difficulties. Would you like to submit a contact form so our team can help you directly?"}]),l(!0)}finally{i(!1)}},z=async()=>{i(!0);try{const s=W.parse({email:y,message:v}),{error:t}=await h.from("contact_submissions").insert({name:"Chatbot User",email:s.email.toLowerCase(),company:null,message:s.message,source:"chatbot"});if(t&&(console.error("Error saving chatbot contact:",t),P(t))){u({title:"Too Many Requests",description:_,variant:"destructive"}),i(!1);return}await h.from("email_subscribers").insert({email:s.email.toLowerCase(),source:"chatbot",tags:["chatbot-contact"]});const o=`Email: ${s.email}
Message: ${s.message}`,{data:c,error:k}=await h.functions.invoke("chatbot",{body:{messages:[{role:"user",content:o}],isContactForm:!0}});if(k)throw k;r(R=>[...R,{role:"user",content:"📧 Contact form submitted"},{role:"assistant",content:c.response}]),l(!1),j(""),w(""),u({title:"Message Sent",description:"Our team will contact you shortly!"})}catch(s){s instanceof B?u({title:"Validation Error",description:s.errors[0].message,variant:"destructive"}):(console.error("Contact form error:",s),u({title:"Error",description:"Failed to send message. Please try again.",variant:"destructive"}))}finally{i(!1)}},L=s=>{s.key==="Enter"&&!s.shiftKey&&(s.preventDefault(),C())};return e.jsxs(e.Fragment,{children:[!x&&e.jsx(m,{onClick:()=>p(!0),className:"fixed bottom-6 right-6 h-14 w-14 rounded-full shadow-lg bg-primary hover:bg-primary/90 z-50",size:"icon","aria-label":"Open chat assistant",children:e.jsx(M,{className:"h-6 w-6"})}),x&&e.jsxs("div",{className:"fixed bottom-6 right-6 w-[380px] h-[600px] bg-background border border-border rounded-lg shadow-2xl flex flex-col z-50 animate-in slide-in-from-bottom-4",children:[e.jsxs("div",{className:"bg-gradient-to-r from-primary to-[#2c9e8a] text-white p-4 rounded-t-lg flex items-center justify-between",children:[e.jsxs("div",{className:"flex items-center gap-2",children:[e.jsx(M,{className:"h-5 w-5"}),e.jsxs("div",{children:[e.jsx("h3",{className:"font-semibold text-sm",children:"Pinnacle Assistant"}),e.jsx("p",{className:"text-xs text-white/80",children:"Ask me anything!"})]})]}),e.jsx(m,{onClick:()=>p(!1),variant:"ghost",size:"icon",className:"h-8 w-8 text-white hover:bg-white/20",children:e.jsx(F,{className:"h-4 w-4"})})]}),e.jsxs("div",{className:"flex-1 overflow-y-auto p-4 space-y-4",children:[f.map((s,t)=>e.jsx("div",{className:`flex ${s.role==="user"?"justify-end":"justify-start"}`,children:e.jsx("div",{className:`max-w-[80%] rounded-lg px-4 py-2 ${s.role==="user"?"bg-primary text-primary-foreground":"bg-muted text-foreground"}`,children:e.jsx("p",{className:"text-sm whitespace-pre-wrap",children:s.content})})},t)),n&&e.jsx("div",{className:"flex justify-start",children:e.jsx("div",{className:"bg-muted rounded-lg px-4 py-2",children:e.jsxs("div",{className:"flex gap-1",children:[e.jsx("div",{className:"w-2 h-2 bg-foreground/60 rounded-full animate-bounce",style:{animationDelay:"0ms"}}),e.jsx("div",{className:"w-2 h-2 bg-foreground/60 rounded-full animate-bounce",style:{animationDelay:"150ms"}}),e.jsx("div",{className:"w-2 h-2 bg-foreground/60 rounded-full animate-bounce",style:{animationDelay:"300ms"}})]})})}),e.jsx("div",{ref:N})]}),b&&e.jsxs("div",{className:"border-t border-border p-4 bg-muted/30",children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx($,{className:"h-4 w-4 text-primary"}),e.jsx("p",{className:"text-sm font-medium",children:"Contact Our Team"})]}),e.jsx(E,{type:"email",placeholder:"Your email",value:y,onChange:s=>j(s.target.value),className:"mb-2"}),e.jsx(O,{placeholder:"Your message",value:v,onChange:s=>w(s.target.value),className:"mb-2 resize-none",rows:3}),e.jsx(m,{onClick:z,disabled:n,className:"w-full",size:"sm",children:"Send Message"}),e.jsx(m,{onClick:()=>l(!1),variant:"ghost",className:"w-full mt-1",size:"sm",children:"Cancel"})]}),!b&&e.jsxs("div",{className:"border-t border-border p-4",children:[e.jsxs("div",{className:"flex gap-2",children:[e.jsx(E,{value:d,onChange:s=>g(s.target.value),onKeyPress:L,placeholder:"Type your message...",disabled:n,className:"flex-1"}),e.jsx(m,{onClick:C,disabled:n||!d.trim(),size:"icon",children:e.jsx(K,{className:"h-4 w-4"})})]}),e.jsx("button",{onClick:()=>l(!0),className:"text-xs text-muted-foreground hover:text-foreground mt-2 transition-colors",children:"Need to speak to someone? Contact us"})]})]})]})};export{ae as default};