@debales/ai 1.1.45 → 1.1.46

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2865,7 +2865,7 @@ In order to be iterable, non-array objects must have a [Symbol.iterator]() metho
2865
2865
 
2866
2866
  `+te+`
2867
2867
 
2868
- => User Action: Klaviyo form closed`}return""},ue=Me=>{Me instanceof CustomEvent&&Oe(Me)};return window.addEventListener("klaviyoForms",ue),()=>{window.removeEventListener("klaviyoForms",ue)}},[ae,t,r]);const[Ro,po]=Z.useState(!1),qn=ee==="Highest"?"120px":ee==="Higher"?"60px":"15px",ua=async()=>{if(Ee){console.warn("Support request called while already in support mode");return}try{const Oe=await fetch(`${fo}/api/wordpress/support`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({chatid:nr()+e,nameSpace:e,userName:dt.name,userEmail:dt.email,userMessage:dt.message,tag:"support"})});if(Oe.ok){const at=await Oe.json(),{contactId:ue,ticketId:Me}=at;if(ue&&Me){const te=Up.connect(`${Yz}/api/v1/live-agent`);if(!te){console.error("Failed to create support socket connection"),m(un=>[...un,{text:"Sorry, we couldn't establish a connection to support. Please try again.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}te.emit("join_ticket_conversation",{ticketID:Me,role:"contact",contactID:ue,nameSpace:e});const de=un=>{console.log("Joined support conversation:",un),m(Un=>[...Un,{text:"You are now connected to live support. A support agent will join shortly.",isBot:!0,conversationId:`support-live-${Date.now()}`}])},Ae=un=>{console.log("Received conversation history:",un.length,"messages")},we=un=>{console.log("Support agent joined:",un),m(Un=>[...Un,{text:"A support agent has joined the conversation. You can now chat directly with them.",isBot:!0,conversationId:`agent-joined-${Date.now()}`}])},Je=un=>{console.log("New support message:",un),m(Un=>[...Un,{text:un.text||un.content,isBot:!0,conversationId:`support-msg-${Date.now()}`}])},$t=()=>{console.log("Support connection disconnected"),m(un=>[...un,{text:"Support connection has ended. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-end-${Date.now()}`}]),$e(!1)};te.on("joined_ticket_conversation",de),te.on("conversation_history",Ae),te.on("participant_joined",we),te.on("message",Je),te.on("disconnect",$t),ve(te),$e(!0);const an=()=>{te.off("joined_ticket_conversation",de),te.off("conversation_history",Ae),te.off("participant_joined",we),te.off("new_message",Je),te.off("disconnect",$t)};te.cleanupHandlers=an}const j=`support-confirm-${Date.now()}`;m(te=>[...te,{text:"Thanks for reaching out! Our support team has received your request and will get back to you within 24 hours.<br>In the meantime, feel free to continue chatting with me - I'll do my best to help!",isBot:!0,leading_queries:Et,conversationId:j}]),d(j),mt(te=>({...te,leadingQueries:!0})),Nt([]),console.log("Connected to support with existing user info:",jo())}else{const at=await Oe.json();console.error("Failed to connect to support:",at),m(ue=>[...ue,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}}catch{m(at=>[...at,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}},Ti=()=>{if(Ie(e)){const Me=Fp("https://test.thecollective.in/content/customersupport");Me.isValid&&(t!=null&&t.shopifyShopDomain&&(Me.url.hostname=t==null?void 0:t.shopifyShopDomain),window.open(Os(Me.url.toString()),"_blank"));return}if(Ee){console.warn("Already in support mode");return}const Oe=jo();let at=[];const ue=h[h.length-1];ue!=null&&ue.leading_queries&&ue.leading_queries.length>0?at=ue.leading_queries:h.length<=1&&(at=Dt.flatMap(Me=>Array.isArray(Me)?Me.map(j=>j.question):[Me.question]).slice(0,4)),Nt(at),Oe&&Oe.name&&Oe.email&&_t({name:Oe==null?void 0:Oe.name,email:Oe==null?void 0:Oe.email,message:""}),m(Me=>[...Me,{text:"Please provide your name, email and message to connect with support.",isBot:!0}]),nt(!0)},pr=async Oe=>{Oe.preventDefault();const at={name:"",email:"",message:""};let ue=!0;const Me=dt.name.trim(),j=dt.message.trim();Me||(at.name="Name cannot be empty or just whitespace.",ue=!1),(!dt.email||!zm.isEmail(dt.email))&&(at.email="Please enter a valid email address.",ue=!1),j||(at.message="Message cannot be empty or just whitespace.",ue=!1),ne(at),ue&&(lf({name:Me,email:dt.email}),nt(!1),m(te=>te.slice(0,-1)),await ua())};Z.useEffect(()=>{if(h.length===1&&h[0].isBot){Re(at=>({...at,leadingQueries:!0}));const Oe=setTimeout(()=>{Re(at=>({...at,leadingQueries:!1}))},2e3);return()=>clearTimeout(Oe)}},[h]),Z.useEffect(()=>()=>{se&&(se.cleanupHandlers&&se.cleanupHandlers(),Up.disconnect(),ve(null),$e(!1))},[se]);const Br=()=>{Up.emit("send_message",{content:"The Contact has left the conversation.",type:"text",timestamp:new Date().toISOString()}),Up.emit("leave_conversation",{role:"contact"}),se&&(se.cleanupHandlers&&se.cleanupHandlers(),Up.disconnect(),ve(null),$e(!1),m(Oe=>[...Oe,{text:"You have exited live support mode. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-exit-${Date.now()}`}]))},ca=Oe=>{if(!Ee){console.warn("Attempted to send support message when not in support mode");return}if(!Up.getIsConnected()){console.error("Support socket is not connected"),m(at=>[...at,{text:"Connection lost. Please wait while we reconnect you to support.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}Up.emit("send_message",{content:Oe,type:"text",timestamp:new Date().toISOString()}),m(at=>[...at,{text:Oe,isBot:!1,conversationId:`support-user-${Date.now()}`}])};return Z.useEffect(()=>{console.log(I,l,h[h.length-1].conversationId,st.cartSummary,bt)},[I,l,st.cartSummary,bt,h]),!t||t.show_bot===!1||a?null:M.jsxs(eC,{theme:gE(t,e),children:[M.jsx("link",{rel:"preconnect",href:"https://fonts.googleapis.com"}),M.jsx("link",{rel:"preconnect",href:"https://fonts.gstatic.com",crossOrigin:"anonymous"}),M.jsx("link",{href:"https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap",rel:"stylesheet"}),M.jsx($pe,{}),!ie&&(rr==null?void 0:rr.length)!==0&&M.jsx(Ipe,{className:rt?"visible":"",$botId:e,$iconSize:V,$removeLogoBackground:t.removeLogoBackground,topOffset:qn,style:{right:z?"auto":"24px",left:z?"24px":"auto",display:"flex",flexDirection:"column",alignItems:z?"flex-start":"flex-end"},children:_e.suggestions?M.jsx(Fi,{isBot:!0,children:M.jsx(Ds,{loading:_e.suggestions})}):rr.map((Oe,at)=>{if(Array.isArray(Oe)){const ue=Oe.map(Me=>({question:Me.question,isHighlighted:Me.isHighlighted??!1})).filter(Me=>!fe.has(Me.question));return M.jsx(fH,{$index:at,$totalCount:rr.length,children:M.jsx(rhe,{messages:ue,botId:e,uiData:t,onClick:Me=>{const j=Oe.find(te=>te.question===Me);Te(!0,"suggested_question_clicked"),ke(te=>{const de=new Set(te);return de.add(Me),de}),window.DebalesChat.track(tn.SUGGESTED_QUESTION_CLICKED,{suggested_question:j}),!R&&j?(Qt(!1),pt(!0),r({message:j.Follow_up_Question??j.question,answer:j==null?void 0:j.answer,products:j==null?void 0:j.products,fromKlaviyo:!1,leading_queries:j==null?void 0:j.leading_queries,followUpQuestion:j==null?void 0:j.Follow_up_Question,messType:"suggested_question"})):(Qt(!1),pt(!0),E(!0),O(j))}})},at)}else return M.jsx(fH,{$index:at,$totalCount:rr.length,children:M.jsx(the,{value:`${Oe.question}`,index:at,botId:e,uiData:t,onClose:()=>{ke(ue=>{const Me=new Set(ue);return Me.add(Oe.question),Me})},onClick:()=>{Te(!0,"suggested_question_clicked"),ke(ue=>{const Me=new Set(ue);return Me.add(Oe.question),Me}),window.DebalesChat.track(tn.SUGGESTED_QUESTION_CLICKED,{suggested_question:Oe}),R?(Qt(!1),pt(!0),E(!0),O(Oe)):(Qt(!1),pt(!0),r({message:Oe.question,answer:Oe.answer,products:Oe.products,leading_queries:Oe.leading_queries,followUpQuestion:Oe.Follow_up_Question,messType:"suggested_question"}))},isHighlighted:Oe.isHighlighted??!1})},at)})}),M.jsxs(ype,{$topOffset:qn,$removeLogoBackground:t.removeLogoBackground,style:{display:ie?"none":"inline-flex",right:z?"auto":"24px",left:z?"24px":"auto"},children:[M.jsx(bpe,{$showAnimation:t.enableChatButtonBackgroundAnimation??!1,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",$backgroundColor:t.backgroundColor||"#fff",$removeLogoBackground:t.removeLogoBackground,$topOffset:qn,$iconSize:V}),M.jsx(xpe,{$backgroundColor:t.removeLogoBackground?"transparent":t.backgroundColor||"#fff",$botId:e,$iconSize:V,$removeLogoBackground:t.removeLogoBackground,topOffset:qn,$showAnimation:t.enableChatButtonBackgroundAnimation,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",id:"debales-chat-button","data-testid":"debales-chat-button",onClick:()=>{if(window.DebalesChat.track(tn.CHAT_BUTTON_CLICKED),Te(!ie,"chat_button_clicked"),!ie){const Oe=_();Oe.length>0?(m(Oe),Qt(!1),pt(!0)):(Qt(!0),pt(!1),Re(at=>({...at,initialMessage:!0,leadingQueries:!0})),mt({text:!0,leadingQueries:!1,collections:!1,products:!1,variants:!1,cartSummary:!1,bulkItems:!1}),setTimeout(()=>{Qt(!1),pt(!0),Re(ue=>({...ue,initialMessage:!1}));const at={text:(t==null?void 0:t.initialMessage)||"Hi, I am an AI chatbot, how can I help you today?",isBot:!0};m([at]),Re(ue=>({...ue,leadingQueries:!0})),setTimeout(()=>{mt(ue=>({...ue,leadingQueries:!0}))},it)},it))}},"aria-label":"Chat",style:{display:ie?"none":"inline-flex",right:z?"auto":"24px",left:z?"24px":"auto"},children:M.jsx(_pe,{$botId:e,$removeLogoBackground:t.removeLogoBackground,"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"})})]}),ie&&yw.createPortal(M.jsx(M.Fragment,{children:M.jsxs(Cpe,{$backgroundColor:t.backgroundColor||"#fff",$fullScreenOnMobile:t.fullScreenOnMobile,$headerFontColor:t.headerFontColor||"#1C1C1C",$isLeft:z,$topOffset:qn,$mobileDisplayMode:t.mobileDisplayMode,$isVisible:pe,"data-testid":"debales-chat-container",children:[M.jsxs(kpe,{$backgroundColor:Ie(e)?t.backgroundColor:t.botBackgroundColor||"#fff",style:{color:t.headerFontColor},children:[M.jsx("div",{style:{flexGrow:.1,display:"flex",justifyContent:"center",alignItems:"center",overflow:"hidden",marginRight:"8px",gap:"10px"},children:Ie(e)?M.jsxs(M.Fragment,{children:[M.jsx(aH,{"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"}),Ie(e)?M.jsx("div",{children:M.jsx("img",{src:bk,alt:"Chat Bot Icon",style:{height:"36px"}})}):M.jsxs(Spe,{$botNameColor:t.headerFontColor,children:[M.jsxs("div",{style:{fontWeight:"700",wordBreak:"break-word"},children:[t.chatbotName,e==="catch-org-ai"&&M.jsx(Rk,{children:"Beta"})]}),M.jsx(wpe,{})]})]}):M.jsx(Epe,{children:M.jsxs(Ape,{children:[M.jsx(aH,{src:t.bot_logo,alt:"Chat Bot Icon"}),M.jsx("span",{style:{whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",display:"inline-block",maxWidth:"22ch"},children:t.chatbotName})]})})}),M.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",marginLeft:"auto",flexShrink:0},children:[Ee&&M.jsxs("div",{style:{background:"#4CAF50",color:"white",padding:"4px 8px",borderRadius:"12px",fontSize:"12px",fontWeight:"500",display:"flex",alignItems:"center",gap:"4px"},children:[M.jsx("span",{style:{fontSize:"8px"},children:"●"}),"Live Support",M.jsx("button",{onClick:Br,style:{background:"none",border:"none",color:"white",cursor:"pointer",fontSize:"12px",marginLeft:"4px",padding:"2px 4px",borderRadius:"4px"},title:"Exit Support",children:"✕"})]}),M.jsx(Ok,{handleSupportClick:Ti,isSupportMode:Ee}),M.jsx(jb,{onClick:()=>{Ve?yn(!0):Te(!1,"close_button_clicked")},id:"debales-minimize-button","data-testid":"debales-minimize-button","aria-label":"Minimize",children:M.jsx(Zm,{botNameColor:t.headerFontColor})})]}),M.jsx(jpe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Kt?1:0}})]}),M.jsxs(Ope,{$backgroundColor:t.botBackgroundColor||"#fff",$mobileDisplayMode:t.mobileDisplayMode,children:[Ro&&((ot=(Kr=t.pluginConfig)==null?void 0:Kr.collectedUserInfo)==null?void 0:ot.discount)&&M.jsx(Uk,{discount:t.pluginConfig.collectedUserInfo.discount,uiData:t,onClose:()=>{po(!1),window.DebalesChat.track(tn.DISCOUNT_VIEWED,{discount:t.pluginConfig.collectedUserInfo.discount})}}),M.jsx(Tk,{}),M.jsx(Gk,{uidata:t,product:fn,showModel:br,setShowModel:Ur,onVariantSelect:Oe=>{console.debug("VariantSelectionModel : Adding item to cart:",Oe),L(Oe.id,1,{productId:(fn==null?void 0:fn.productId)||""}),Pt(!0),window.DebalesChat.track(tn.ADD_TO_CART_CLICKED,{product:Oe.title,price:Number(Oe.price),link:`https://${t.shopifyShopDomain}/variants/${Oe.id}`,message:l,chatId:nr()+e})},botId:e}),nn&&M.jsx(jk,{setShowExitModal:yn,handleCloseChatbot:xr,chatId:nr()+e}),M.jsxs(Tpe,{id:"debales-conversation-group",$botBackgroundColor:t.botBackgroundColor||"#fff",ref:re,$mobileDisplayMode:t.mobileDisplayMode,"data-testid":"debales-conversation-group",children:[M.jsx(Fpe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Kt?1:0}}),M.jsxs(Rpe,{children:[gn&&h.map((Oe,at)=>{const ue=Oe.isBot,Me=Ip(Oe.text);return Oe.hideConfirmation?null:M.jsxs(Z.Fragment,{children:[ue&&M.jsxs(sH,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff","data-testid":"debales-ai-message-container",children:[M.jsx("img",{src:ue?t.bot_logo:t.userMessage_logo,alt:"Chat User Icon",style:{marginLeft:"8px"}}),M.jsxs(cH,{children:[Me.map(j=>M.jsx("div",{children:M.jsx(Fi,{isBot:ue,children:M.jsx(_d,{components:{a:({href:te,children:de})=>{const Ae=()=>{te!=null&&te.startsWith("https://shop.catch.org/")&&window.DebalesChat.track(tn.SHOPIFY_LINK_CLICKED)};return M.jsx("a",{href:te,target:"_blank",onClick:Ae,children:de})}},remarkPlugins:[Iw,yE],children:j})})})),Oe.collections&&l===Oe.conversationId&&st.collections&&M.jsx(Nk,{message:Oe,botId:e,uidata:t,onSendMessage:r,loading:p}),Oe.products&&l===Oe.conversationId&&st.products&&M.jsx(Hk,{message:Oe,botId:e,uidata:t,onSendMessage:r,loading:p,storeMetaData:n,platform:W,setShowVariantModel:Ur,setSelectedProduct:ho,setCartVersion:P}),o&&M.jsx(j6,{children:M.jsx(Nb,{onClick:()=>{window._klOnsite&&(window._klOnsite.push(["openForm",Ce]),Te(!1,"klaviyo_grab_deal_button"))},children:"🚀 Grab Deal Now!"})}),I&&I.itemCount>0&&l===Oe.conversationId&&st.cartSummary&&bt&&M.jsx(Ik,{onCheckout:Ho,onKeepShopping:()=>{Pt(!1)}}),We&&We.length>0&&l===Oe.conversationId&&st.bulkItems&&M.jsx(vk,{onClick:()=>us(Oe.conversationId),children:"Add Bulk Items to Cart"}),A&&h.indexOf(Oe)===h.length-1&&M.jsx(M.Fragment,{children:M.jsx(qk,{setShowModel:j=>{var de,Ae;const te=typeof j=="function"?j(A):j;E(te),!te&&((Ae=(de=t.pluginConfig)==null?void 0:de.collectedUserInfo)!=null&&Ae.discount)&&po(!0)},inChat:!0,showFormInstructions:!1})}),It&&h.indexOf(Oe)===h.length-1&&M.jsxs(Bpe,{onSubmit:pr,noValidate:!0,"data-testid":"debales-support-form",children:[Ie(e)?M.jsxs(M.Fragment,{children:[M.jsx(nu,{label:"Name",name:"name",type:"text",value:dt.name,onChange:j=>_t(te=>({...te,name:j.target.value})),required:!0,error:yt.name,isCollectiveBot:!0}),M.jsx(nu,{label:"Email",name:"email",type:"email",value:dt.email,onChange:j=>_t(te=>({...te,email:j.target.value})),required:!0,error:yt.email,isCollectiveBot:!0}),M.jsx(Pk,{label:"Message",name:"message",value:dt.message,onChange:j=>_t(te=>({...te,message:j.target.value})),required:!0,error:yt.message,rows:4,isCollectiveBot:!0})]}):M.jsxs(M.Fragment,{children:[M.jsxs(P6,{children:[M.jsxs(H6,{children:["Name",M.jsx("span",{children:"*"})]}),M.jsx(dH,{type:"text",required:!0,value:dt.name,onChange:j=>_t(te=>({...te,name:j.target.value})),placeholder:"John"}),yt.name&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.name})]}),M.jsxs(P6,{children:[M.jsxs(H6,{children:["Email",M.jsx("span",{children:"*"})]}),M.jsx(dH,{type:"email",required:!0,value:dt.email,onChange:j=>_t(te=>({...te,email:j.target.value})),placeholder:"olivia@untitledui.com"}),yt.email&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.email})]}),M.jsxs(P6,{children:[M.jsxs(H6,{children:["Message",M.jsx("span",{children:"*"})]}),M.jsx(Lpe,{required:!0,value:dt.message,onChange:j=>_t(te=>({...te,message:j.target.value})),placeholder:"I need help with..."}),yt.message&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.message})]})]}),M.jsx(zpe,{children:M.jsx(Npe,{type:"submit",children:"Submit"})})]}),Ct&&!p&&l===Oe.conversationId&&M.jsx(Fi,{isBot:ue,children:M.jsx(Ds,{loading:Ct})})]})]}),!ue&&M.jsx(Dpe,{$fontColor:t.userFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:M.jsx(Fi,{isBot:!!ue,children:M.jsx(_d,{children:Oe.text})})}),h.length==1&&at==0&&!A&&M.jsx(j6,{children:_e.leadingQueries?M.jsx(Fi,{isBot:ue??!0,children:M.jsx(Ds,{loading:_e.leadingQueries})}):Dt.map(j=>Array.isArray(j)?j[0]:j).map((j,te)=>M.jsxs(uH,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{R?(E(!0),O({question:j.question,answer:j.answer,products:j.products,path:"/",nameSpace:window.DebalesChat.projectId})):(Qt(!1),pt(!0),r({message:j.question,answer:j.answer,products:j.products,leading_queries:j.leading_queries,followUpQuestion:j.Follow_up_Question,messType:"leading_query"})),ke(de=>{const Ae=new Set(de);return Ae.add(j.question),Ae}),window.DebalesChat.track(tn.INITIAL_LEADING_QUERY_SUGGESTED_QUESTION_CLICKED,{question:j.question})},disabled:p,$disabled:p,children:[j.question,M.jsx(lH,{color:t.callToActionFontColor})]},te))}),Oe.leading_queries&&Oe.leading_queries.length>0&&l===Oe.conversationId&&st.leadingQueries&&!o&&!A&&M.jsx(j6,{children:Oe.leading_queries.map((j,te)=>M.jsxs(uH,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{R?(E(!0),O({question:j,path:"/",nameSpace:window.DebalesChat.projectId})):r({message:j,messType:"leading_query"}),window.DebalesChat.track(tn.LEADING_QUERY_CLICKED,{query:j})},disabled:p,$disabled:p,children:[j,M.jsx(lH,{color:t.callToActionFontColor})]},te))})]},at)}),(p||en)&&!Ee&&M.jsxs(sH,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:[M.jsx("img",{src:t.bot_logo,alt:"Chat User Icon"}),M.jsx(cH,{children:M.jsx(Fi,{isBot:!0,children:M.jsx("div",{style:{display:"flex",alignItems:"center",gap:"4px",animation:"fadeIn 0.3s ease-in-out"},children:M.jsx(Ds,{loading:p||en})})})})]})]}),M.jsx(Upe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Y?1:0}})]}),M.jsxs("div",{className:"debales-footer-container",style:{width:"calc(100%-24px)",backgroundColor:t.botBackgroundColor||"#fff",color:t.fontColor||"#000"},children:[M.jsx(Fk,{}),M.jsx("div",{children:M.jsx(Vk,{disabled:p,inputState:!1,isInSupportMode:Ee,onSupportMessage:ca})}),M.jsx(zk,{})]})]})]})}),Q.shadowRoot)]})},Ppe=ce.p`
2868
+ => User Action: Klaviyo form closed`}return""},ue=Me=>{Me instanceof CustomEvent&&Oe(Me)};return window.addEventListener("klaviyoForms",ue),()=>{window.removeEventListener("klaviyoForms",ue)}},[ae,t,r]);const[Ro,po]=Z.useState(!1),qn=ee==="Highest"?"120px":ee==="Higher"?"60px":"15px",ua=async()=>{if(Ee){console.warn("Support request called while already in support mode");return}try{const Oe=await fetch(`${fo}/api/wordpress/support`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({chatid:nr()+e,nameSpace:e,userName:dt.name,userEmail:dt.email,userMessage:dt.message,tag:"support"})});if(Oe.ok){const at=await Oe.json(),{contactId:ue,ticketId:Me}=at;if(ue&&Me){const te=Up.connect(`${Yz}/api/v1/live-agent`);if(!te){console.error("Failed to create support socket connection"),m(un=>[...un,{text:"Sorry, we couldn't establish a connection to support. Please try again.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}te.emit("join_ticket_conversation",{ticketID:Me,role:"contact",contactID:ue,nameSpace:e});const de=un=>{console.log("Joined support conversation:",un),m(Un=>[...Un,{text:"You are now connected to live support. A support agent will join shortly.",isBot:!0,conversationId:`support-live-${Date.now()}`}])},Ae=un=>{console.log("Received conversation history:",un.length,"messages")},we=un=>{console.log("Support agent joined:",un),m(Un=>[...Un,{text:"A support agent has joined the conversation. You can now chat directly with them.",isBot:!0,conversationId:`agent-joined-${Date.now()}`}])},Je=un=>{console.log("New support message:",un),m(Un=>[...Un,{text:un.text||un.content,isBot:!0,conversationId:`support-msg-${Date.now()}`}])},$t=()=>{console.log("Support connection disconnected"),m(un=>[...un,{text:"Support connection has ended. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-end-${Date.now()}`}]),$e(!1)};te.on("joined_ticket_conversation",de),te.on("conversation_history",Ae),te.on("participant_joined",we),te.on("message",Je),te.on("disconnect",$t),ve(te),$e(!0);const an=()=>{te.off("joined_ticket_conversation",de),te.off("conversation_history",Ae),te.off("participant_joined",we),te.off("new_message",Je),te.off("disconnect",$t)};te.cleanupHandlers=an}const j=`support-confirm-${Date.now()}`;m(te=>[...te,{text:"Thanks for reaching out! Our support team has received your request and will get back to you within 24 hours.<br>In the meantime, feel free to continue chatting with me - I'll do my best to help!",isBot:!0,leading_queries:Et,conversationId:j}]),d(j),mt(te=>({...te,leadingQueries:!0})),Nt([]),console.log("Connected to support with existing user info:",jo())}else{const at=await Oe.json();console.error("Failed to connect to support:",at),m(ue=>[...ue,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}}catch{m(at=>[...at,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}},Ti=()=>{if(Ie(e)){const Me=Fp("https://test.thecollective.in/content/customersupport");Me.isValid&&(t!=null&&t.shopifyShopDomain&&(Me.url.hostname=t==null?void 0:t.shopifyShopDomain),window.open(Os(Me.url.toString()),"_blank"));return}if(Ee){console.warn("Already in support mode");return}const Oe=jo();let at=[];const ue=h[h.length-1];ue!=null&&ue.leading_queries&&ue.leading_queries.length>0?at=ue.leading_queries:h.length<=1&&(at=Dt.flatMap(Me=>Array.isArray(Me)?Me.map(j=>j.question):[Me.question]).slice(0,4)),Nt(at),Oe&&Oe.name&&Oe.email&&_t({name:Oe==null?void 0:Oe.name,email:Oe==null?void 0:Oe.email,message:""}),m(Me=>[...Me,{text:"Please provide your name, email and message to connect with support.",isBot:!0}]),nt(!0)},pr=async Oe=>{Oe.preventDefault();const at={name:"",email:"",message:""};let ue=!0;const Me=dt.name.trim(),j=dt.message.trim();Me||(at.name="Name cannot be empty or just whitespace.",ue=!1),(!dt.email||!zm.isEmail(dt.email))&&(at.email="Please enter a valid email address.",ue=!1),j||(at.message="Message cannot be empty or just whitespace.",ue=!1),ne(at),ue&&(lf({name:Me,email:dt.email}),nt(!1),m(te=>te.slice(0,-1)),await ua())};Z.useEffect(()=>{if(h.length===1&&h[0].isBot){Re(at=>({...at,leadingQueries:!0}));const Oe=setTimeout(()=>{Re(at=>({...at,leadingQueries:!1}))},2e3);return()=>clearTimeout(Oe)}},[h]),Z.useEffect(()=>()=>{se&&(se.cleanupHandlers&&se.cleanupHandlers(),Up.disconnect(),ve(null),$e(!1))},[se]);const Br=()=>{Up.emit("send_message",{content:"The Contact has left the conversation.",type:"text",timestamp:new Date().toISOString()}),Up.emit("leave_conversation",{role:"contact"}),se&&(se.cleanupHandlers&&se.cleanupHandlers(),Up.disconnect(),ve(null),$e(!1),m(Oe=>[...Oe,{text:"You have exited live support mode. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-exit-${Date.now()}`}]))},ca=Oe=>{if(!Ee){console.warn("Attempted to send support message when not in support mode");return}if(!Up.getIsConnected()){console.error("Support socket is not connected"),m(at=>[...at,{text:"Connection lost. Please wait while we reconnect you to support.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}Up.emit("send_message",{content:Oe,type:"text",timestamp:new Date().toISOString()}),m(at=>[...at,{text:Oe,isBot:!1,conversationId:`support-user-${Date.now()}`}])};return Z.useEffect(()=>{console.log(I,l,h,st.cartSummary,bt)},[I,l,st.cartSummary,bt,h]),!t||t.show_bot===!1||a?null:M.jsxs(eC,{theme:gE(t,e),children:[M.jsx("link",{rel:"preconnect",href:"https://fonts.googleapis.com"}),M.jsx("link",{rel:"preconnect",href:"https://fonts.gstatic.com",crossOrigin:"anonymous"}),M.jsx("link",{href:"https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap",rel:"stylesheet"}),M.jsx($pe,{}),!ie&&(rr==null?void 0:rr.length)!==0&&M.jsx(Ipe,{className:rt?"visible":"",$botId:e,$iconSize:V,$removeLogoBackground:t.removeLogoBackground,topOffset:qn,style:{right:z?"auto":"24px",left:z?"24px":"auto",display:"flex",flexDirection:"column",alignItems:z?"flex-start":"flex-end"},children:_e.suggestions?M.jsx(Fi,{isBot:!0,children:M.jsx(Ds,{loading:_e.suggestions})}):rr.map((Oe,at)=>{if(Array.isArray(Oe)){const ue=Oe.map(Me=>({question:Me.question,isHighlighted:Me.isHighlighted??!1})).filter(Me=>!fe.has(Me.question));return M.jsx(fH,{$index:at,$totalCount:rr.length,children:M.jsx(rhe,{messages:ue,botId:e,uiData:t,onClick:Me=>{const j=Oe.find(te=>te.question===Me);Te(!0,"suggested_question_clicked"),ke(te=>{const de=new Set(te);return de.add(Me),de}),window.DebalesChat.track(tn.SUGGESTED_QUESTION_CLICKED,{suggested_question:j}),!R&&j?(Qt(!1),pt(!0),r({message:j.Follow_up_Question??j.question,answer:j==null?void 0:j.answer,products:j==null?void 0:j.products,fromKlaviyo:!1,leading_queries:j==null?void 0:j.leading_queries,followUpQuestion:j==null?void 0:j.Follow_up_Question,messType:"suggested_question"})):(Qt(!1),pt(!0),E(!0),O(j))}})},at)}else return M.jsx(fH,{$index:at,$totalCount:rr.length,children:M.jsx(the,{value:`${Oe.question}`,index:at,botId:e,uiData:t,onClose:()=>{ke(ue=>{const Me=new Set(ue);return Me.add(Oe.question),Me})},onClick:()=>{Te(!0,"suggested_question_clicked"),ke(ue=>{const Me=new Set(ue);return Me.add(Oe.question),Me}),window.DebalesChat.track(tn.SUGGESTED_QUESTION_CLICKED,{suggested_question:Oe}),R?(Qt(!1),pt(!0),E(!0),O(Oe)):(Qt(!1),pt(!0),r({message:Oe.question,answer:Oe.answer,products:Oe.products,leading_queries:Oe.leading_queries,followUpQuestion:Oe.Follow_up_Question,messType:"suggested_question"}))},isHighlighted:Oe.isHighlighted??!1})},at)})}),M.jsxs(ype,{$topOffset:qn,$removeLogoBackground:t.removeLogoBackground,style:{display:ie?"none":"inline-flex",right:z?"auto":"24px",left:z?"24px":"auto"},children:[M.jsx(bpe,{$showAnimation:t.enableChatButtonBackgroundAnimation??!1,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",$backgroundColor:t.backgroundColor||"#fff",$removeLogoBackground:t.removeLogoBackground,$topOffset:qn,$iconSize:V}),M.jsx(xpe,{$backgroundColor:t.removeLogoBackground?"transparent":t.backgroundColor||"#fff",$botId:e,$iconSize:V,$removeLogoBackground:t.removeLogoBackground,topOffset:qn,$showAnimation:t.enableChatButtonBackgroundAnimation,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",id:"debales-chat-button","data-testid":"debales-chat-button",onClick:()=>{if(window.DebalesChat.track(tn.CHAT_BUTTON_CLICKED),Te(!ie,"chat_button_clicked"),!ie){const Oe=_();Oe.length>0?(m(Oe),Qt(!1),pt(!0)):(Qt(!0),pt(!1),Re(at=>({...at,initialMessage:!0,leadingQueries:!0})),mt({text:!0,leadingQueries:!1,collections:!1,products:!1,variants:!1,cartSummary:!1,bulkItems:!1}),setTimeout(()=>{Qt(!1),pt(!0),Re(ue=>({...ue,initialMessage:!1}));const at={text:(t==null?void 0:t.initialMessage)||"Hi, I am an AI chatbot, how can I help you today?",isBot:!0};m([at]),Re(ue=>({...ue,leadingQueries:!0})),setTimeout(()=>{mt(ue=>({...ue,leadingQueries:!0}))},it)},it))}},"aria-label":"Chat",style:{display:ie?"none":"inline-flex",right:z?"auto":"24px",left:z?"24px":"auto"},children:M.jsx(_pe,{$botId:e,$removeLogoBackground:t.removeLogoBackground,"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"})})]}),ie&&yw.createPortal(M.jsx(M.Fragment,{children:M.jsxs(Cpe,{$backgroundColor:t.backgroundColor||"#fff",$fullScreenOnMobile:t.fullScreenOnMobile,$headerFontColor:t.headerFontColor||"#1C1C1C",$isLeft:z,$topOffset:qn,$mobileDisplayMode:t.mobileDisplayMode,$isVisible:pe,"data-testid":"debales-chat-container",children:[M.jsxs(kpe,{$backgroundColor:Ie(e)?t.backgroundColor:t.botBackgroundColor||"#fff",style:{color:t.headerFontColor},children:[M.jsx("div",{style:{flexGrow:.1,display:"flex",justifyContent:"center",alignItems:"center",overflow:"hidden",marginRight:"8px",gap:"10px"},children:Ie(e)?M.jsxs(M.Fragment,{children:[M.jsx(aH,{"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"}),Ie(e)?M.jsx("div",{children:M.jsx("img",{src:bk,alt:"Chat Bot Icon",style:{height:"36px"}})}):M.jsxs(Spe,{$botNameColor:t.headerFontColor,children:[M.jsxs("div",{style:{fontWeight:"700",wordBreak:"break-word"},children:[t.chatbotName,e==="catch-org-ai"&&M.jsx(Rk,{children:"Beta"})]}),M.jsx(wpe,{})]})]}):M.jsx(Epe,{children:M.jsxs(Ape,{children:[M.jsx(aH,{src:t.bot_logo,alt:"Chat Bot Icon"}),M.jsx("span",{style:{whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",display:"inline-block",maxWidth:"22ch"},children:t.chatbotName})]})})}),M.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",marginLeft:"auto",flexShrink:0},children:[Ee&&M.jsxs("div",{style:{background:"#4CAF50",color:"white",padding:"4px 8px",borderRadius:"12px",fontSize:"12px",fontWeight:"500",display:"flex",alignItems:"center",gap:"4px"},children:[M.jsx("span",{style:{fontSize:"8px"},children:"●"}),"Live Support",M.jsx("button",{onClick:Br,style:{background:"none",border:"none",color:"white",cursor:"pointer",fontSize:"12px",marginLeft:"4px",padding:"2px 4px",borderRadius:"4px"},title:"Exit Support",children:"✕"})]}),M.jsx(Ok,{handleSupportClick:Ti,isSupportMode:Ee}),M.jsx(jb,{onClick:()=>{Ve?yn(!0):Te(!1,"close_button_clicked")},id:"debales-minimize-button","data-testid":"debales-minimize-button","aria-label":"Minimize",children:M.jsx(Zm,{botNameColor:t.headerFontColor})})]}),M.jsx(jpe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Kt?1:0}})]}),M.jsxs(Ope,{$backgroundColor:t.botBackgroundColor||"#fff",$mobileDisplayMode:t.mobileDisplayMode,children:[Ro&&((ot=(Kr=t.pluginConfig)==null?void 0:Kr.collectedUserInfo)==null?void 0:ot.discount)&&M.jsx(Uk,{discount:t.pluginConfig.collectedUserInfo.discount,uiData:t,onClose:()=>{po(!1),window.DebalesChat.track(tn.DISCOUNT_VIEWED,{discount:t.pluginConfig.collectedUserInfo.discount})}}),M.jsx(Tk,{}),M.jsx(Gk,{uidata:t,product:fn,showModel:br,setShowModel:Ur,onVariantSelect:Oe=>{console.debug("VariantSelectionModel : Adding item to cart:",Oe),L(Oe.id,1,{productId:(fn==null?void 0:fn.productId)||""}),Pt(!0),window.DebalesChat.track(tn.ADD_TO_CART_CLICKED,{product:Oe.title,price:Number(Oe.price),link:`https://${t.shopifyShopDomain}/variants/${Oe.id}`,message:l,chatId:nr()+e})},botId:e}),nn&&M.jsx(jk,{setShowExitModal:yn,handleCloseChatbot:xr,chatId:nr()+e}),M.jsxs(Tpe,{id:"debales-conversation-group",$botBackgroundColor:t.botBackgroundColor||"#fff",ref:re,$mobileDisplayMode:t.mobileDisplayMode,"data-testid":"debales-conversation-group",children:[M.jsx(Fpe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Kt?1:0}}),M.jsxs(Rpe,{children:[gn&&h.map((Oe,at)=>{const ue=Oe.isBot,Me=Ip(Oe.text);return Oe.hideConfirmation?null:M.jsxs(Z.Fragment,{children:[ue&&M.jsxs(sH,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff","data-testid":"debales-ai-message-container",children:[M.jsx("img",{src:ue?t.bot_logo:t.userMessage_logo,alt:"Chat User Icon",style:{marginLeft:"8px"}}),M.jsxs(cH,{children:[Me.map(j=>M.jsx("div",{children:M.jsx(Fi,{isBot:ue,children:M.jsx(_d,{components:{a:({href:te,children:de})=>{const Ae=()=>{te!=null&&te.startsWith("https://shop.catch.org/")&&window.DebalesChat.track(tn.SHOPIFY_LINK_CLICKED)};return M.jsx("a",{href:te,target:"_blank",onClick:Ae,children:de})}},remarkPlugins:[Iw,yE],children:j})})})),Oe.collections&&l===Oe.conversationId&&st.collections&&M.jsx(Nk,{message:Oe,botId:e,uidata:t,onSendMessage:r,loading:p}),Oe.products&&l===Oe.conversationId&&st.products&&M.jsx(Hk,{message:Oe,botId:e,uidata:t,onSendMessage:r,loading:p,storeMetaData:n,platform:W,setShowVariantModel:Ur,setSelectedProduct:ho,setCartVersion:P}),o&&M.jsx(j6,{children:M.jsx(Nb,{onClick:()=>{window._klOnsite&&(window._klOnsite.push(["openForm",Ce]),Te(!1,"klaviyo_grab_deal_button"))},children:"🚀 Grab Deal Now!"})}),I&&I.itemCount>0&&l===Oe.conversationId&&st.cartSummary&&bt&&M.jsx(Ik,{onCheckout:Ho,onKeepShopping:()=>{Pt(!1)}}),We&&We.length>0&&l===Oe.conversationId&&st.bulkItems&&M.jsx(vk,{onClick:()=>us(Oe.conversationId),children:"Add Bulk Items to Cart"}),A&&h.indexOf(Oe)===h.length-1&&M.jsx(M.Fragment,{children:M.jsx(qk,{setShowModel:j=>{var de,Ae;const te=typeof j=="function"?j(A):j;E(te),!te&&((Ae=(de=t.pluginConfig)==null?void 0:de.collectedUserInfo)!=null&&Ae.discount)&&po(!0)},inChat:!0,showFormInstructions:!1})}),It&&h.indexOf(Oe)===h.length-1&&M.jsxs(Bpe,{onSubmit:pr,noValidate:!0,"data-testid":"debales-support-form",children:[Ie(e)?M.jsxs(M.Fragment,{children:[M.jsx(nu,{label:"Name",name:"name",type:"text",value:dt.name,onChange:j=>_t(te=>({...te,name:j.target.value})),required:!0,error:yt.name,isCollectiveBot:!0}),M.jsx(nu,{label:"Email",name:"email",type:"email",value:dt.email,onChange:j=>_t(te=>({...te,email:j.target.value})),required:!0,error:yt.email,isCollectiveBot:!0}),M.jsx(Pk,{label:"Message",name:"message",value:dt.message,onChange:j=>_t(te=>({...te,message:j.target.value})),required:!0,error:yt.message,rows:4,isCollectiveBot:!0})]}):M.jsxs(M.Fragment,{children:[M.jsxs(P6,{children:[M.jsxs(H6,{children:["Name",M.jsx("span",{children:"*"})]}),M.jsx(dH,{type:"text",required:!0,value:dt.name,onChange:j=>_t(te=>({...te,name:j.target.value})),placeholder:"John"}),yt.name&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.name})]}),M.jsxs(P6,{children:[M.jsxs(H6,{children:["Email",M.jsx("span",{children:"*"})]}),M.jsx(dH,{type:"email",required:!0,value:dt.email,onChange:j=>_t(te=>({...te,email:j.target.value})),placeholder:"olivia@untitledui.com"}),yt.email&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.email})]}),M.jsxs(P6,{children:[M.jsxs(H6,{children:["Message",M.jsx("span",{children:"*"})]}),M.jsx(Lpe,{required:!0,value:dt.message,onChange:j=>_t(te=>({...te,message:j.target.value})),placeholder:"I need help with..."}),yt.message&&M.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:yt.message})]})]}),M.jsx(zpe,{children:M.jsx(Npe,{type:"submit",children:"Submit"})})]}),Ct&&!p&&l===Oe.conversationId&&M.jsx(Fi,{isBot:ue,children:M.jsx(Ds,{loading:Ct})})]})]}),!ue&&M.jsx(Dpe,{$fontColor:t.userFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:M.jsx(Fi,{isBot:!!ue,children:M.jsx(_d,{children:Oe.text})})}),h.length==1&&at==0&&!A&&M.jsx(j6,{children:_e.leadingQueries?M.jsx(Fi,{isBot:ue??!0,children:M.jsx(Ds,{loading:_e.leadingQueries})}):Dt.map(j=>Array.isArray(j)?j[0]:j).map((j,te)=>M.jsxs(uH,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{R?(E(!0),O({question:j.question,answer:j.answer,products:j.products,path:"/",nameSpace:window.DebalesChat.projectId})):(Qt(!1),pt(!0),r({message:j.question,answer:j.answer,products:j.products,leading_queries:j.leading_queries,followUpQuestion:j.Follow_up_Question,messType:"leading_query"})),ke(de=>{const Ae=new Set(de);return Ae.add(j.question),Ae}),window.DebalesChat.track(tn.INITIAL_LEADING_QUERY_SUGGESTED_QUESTION_CLICKED,{question:j.question})},disabled:p,$disabled:p,children:[j.question,M.jsx(lH,{color:t.callToActionFontColor})]},te))}),Oe.leading_queries&&Oe.leading_queries.length>0&&l===Oe.conversationId&&st.leadingQueries&&!o&&!A&&M.jsx(j6,{children:Oe.leading_queries.map((j,te)=>M.jsxs(uH,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{R?(E(!0),O({question:j,path:"/",nameSpace:window.DebalesChat.projectId})):r({message:j,messType:"leading_query"}),window.DebalesChat.track(tn.LEADING_QUERY_CLICKED,{query:j})},disabled:p,$disabled:p,children:[j,M.jsx(lH,{color:t.callToActionFontColor})]},te))})]},at)}),(p||en)&&!Ee&&M.jsxs(sH,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:[M.jsx("img",{src:t.bot_logo,alt:"Chat User Icon"}),M.jsx(cH,{children:M.jsx(Fi,{isBot:!0,children:M.jsx("div",{style:{display:"flex",alignItems:"center",gap:"4px",animation:"fadeIn 0.3s ease-in-out"},children:M.jsx(Ds,{loading:p||en})})})})]})]}),M.jsx(Upe,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Y?1:0}})]}),M.jsxs("div",{className:"debales-footer-container",style:{width:"calc(100%-24px)",backgroundColor:t.botBackgroundColor||"#fff",color:t.fontColor||"#000"},children:[M.jsx(Fk,{}),M.jsx("div",{children:M.jsx(Vk,{disabled:p,inputState:!1,isInSupportMode:Ee,onSupportMessage:ca})}),M.jsx(zk,{})]})]})]})}),Q.shadowRoot)]})},Ppe=ce.p`
2869
2869
  margin: 0;
2870
2870
  font-size: ${e=>e.theme.typography.fontSizes.lg};
2871
2871
  text-align: left;
package/dist/module/ai.js CHANGED
@@ -41774,7 +41774,7 @@ const nde = oe.button`
41774
41774
  console.log(
41775
41775
  I,
41776
41776
  l,
41777
- h[h.length - 1].conversationId,
41777
+ h,
41778
41778
  it.cartSummary,
41779
41779
  Un
41780
41780
  );
@@ -2509,7 +2509,7 @@ In order to be iterable, non-array objects must have a [Symbol.iterator]() metho
2509
2509
 
2510
2510
  `+X+`
2511
2511
 
2512
- => User Action: Klaviyo form closed`}return""},se=Ae=>{Ae instanceof CustomEvent&&Ce(Ae)};return window.addEventListener("klaviyoForms",se),()=>{window.removeEventListener("klaviyoForms",se)}},[fe,t,r]);const[$o,Ci]=ne.useState(!1),Wn=J==="Highest"?"120px":J==="Higher"?"60px":"15px",_o=async()=>{if(Le){console.warn("Support request called while already in support mode");return}try{const Ce=await fetch(`${io}/api/wordpress/support`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({chatid:tr()+e,nameSpace:e,userName:st.name,userEmail:st.email,userMessage:st.message,tag:"support"})});if(Ce.ok){const Je=await Ce.json(),{contactId:se,ticketId:Ae}=Je;if(se&&Ae){const X=hp.connect(`${p7}/api/v1/live-agent`);if(!X){console.error("Failed to create support socket connection"),m(en=>[...en,{text:"Sorry, we couldn't establish a connection to support. Please try again.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}X.emit("join_ticket_conversation",{ticketID:Ae,role:"contact",contactID:se,nameSpace:e});const ce=en=>{console.log("Joined support conversation:",en),m(Ln=>[...Ln,{text:"You are now connected to live support. A support agent will join shortly.",isBot:!0,conversationId:`support-live-${Date.now()}`}])},be=en=>{console.log("Received conversation history:",en.length,"messages")},Se=en=>{console.log("Support agent joined:",en),m(Ln=>[...Ln,{text:"A support agent has joined the conversation. You can now chat directly with them.",isBot:!0,conversationId:`agent-joined-${Date.now()}`}])},Qe=en=>{console.log("New support message:",en),m(Ln=>[...Ln,{text:en.text||en.content,isBot:!0,conversationId:`support-msg-${Date.now()}`}])},Rt=()=>{console.log("Support connection disconnected"),m(en=>[...en,{text:"Support connection has ended. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-end-${Date.now()}`}]),Te(!1)};X.on("joined_ticket_conversation",ce),X.on("conversation_history",be),X.on("participant_joined",Se),X.on("message",Qe),X.on("disconnect",Rt),ge(X),Te(!0);const Qt=()=>{X.off("joined_ticket_conversation",ce),X.off("conversation_history",be),X.off("participant_joined",Se),X.off("new_message",Qe),X.off("disconnect",Rt)};X.cleanupHandlers=Qt}const z=`support-confirm-${Date.now()}`;m(X=>[...X,{text:"Thanks for reaching out! Our support team has received your request and will get back to you within 24 hours.<br>In the meantime, feel free to continue chatting with me - I'll do my best to help!",isBot:!0,leading_queries:_t,conversationId:z}]),d(z),ht(X=>({...X,leadingQueries:!0})),kt([]),console.log("Connected to support with existing user info:",Io())}else{const Je=await Ce.json();console.error("Failed to connect to support:",Je),m(se=>[...se,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}}catch{m(Je=>[...Je,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}},xr=()=>{if(ke(e)){const Ae=dp("https://test.thecollective.in/content/customersupport");Ae.isValid&&(t!=null&&t.shopifyShopDomain&&(Ae.url.hostname=t==null?void 0:t.shopifyShopDomain),window.open(ms(Ae.url.toString()),"_blank"));return}if(Le){console.warn("Already in support mode");return}const Ce=Io();let Je=[];const se=h[h.length-1];se!=null&&se.leading_queries&&se.leading_queries.length>0?Je=se.leading_queries:h.length<=1&&(Je=si.flatMap(Ae=>Array.isArray(Ae)?Ae.map(z=>z.question):[Ae.question]).slice(0,4)),kt(Je),Ce&&Ce.name&&Ce.email&&yt({name:Ce==null?void 0:Ce.name,email:Ce==null?void 0:Ce.email,message:""}),m(Ae=>[...Ae,{text:"Please provide your name, email and message to connect with support.",isBot:!0}]),rt(!0)},Ei=async Ce=>{Ce.preventDefault();const Je={name:"",email:"",message:""};let se=!0;const Ae=st.name.trim(),z=st.message.trim();Ae||(Je.name="Name cannot be empty or just whitespace.",se=!1),(!st.email||!rm.isEmail(st.email))&&(Je.email="Please enter a valid email address.",se=!1),z||(Je.message="Message cannot be empty or just whitespace.",se=!1),te(Je),se&&(Uc({name:Ae,email:st.email}),rt(!1),m(X=>X.slice(0,-1)),await _o())};tt.useEffect(()=>{if(h.length===1&&h[0].isBot){we(Je=>({...Je,leadingQueries:!0}));const Ce=setTimeout(()=>{we(Je=>({...Je,leadingQueries:!1}))},2e3);return()=>clearTimeout(Ce)}},[h]),tt.useEffect(()=>()=>{ae&&(ae.cleanupHandlers&&ae.cleanupHandlers(),hp.disconnect(),ge(null),Te(!1))},[ae]);const Dr=()=>{hp.emit("send_message",{content:"The Contact has left the conversation.",type:"text",timestamp:new Date().toISOString()}),hp.emit("leave_conversation",{role:"contact"}),ae&&(ae.cleanupHandlers&&ae.cleanupHandlers(),hp.disconnect(),ge(null),Te(!1),m(Ce=>[...Ce,{text:"You have exited live support mode. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-exit-${Date.now()}`}]))},wa=Ce=>{if(!Le){console.warn("Attempted to send support message when not in support mode");return}if(!hp.getIsConnected()){console.error("Support socket is not connected"),m(Je=>[...Je,{text:"Connection lost. Please wait while we reconnect you to support.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}hp.emit("send_message",{content:Ce,type:"text",timestamp:new Date().toISOString()}),m(Je=>[...Je,{text:Ce,isBot:!1,conversationId:`support-user-${Date.now()}`}])};return ne.useEffect(()=>{console.log(I,l,h[h.length-1].conversationId,it.cartSummary,Hn)},[I,l,it.cartSummary,Hn,h]),!t||t.show_bot===!1||a?null:R.jsxs(ee.ThemeProvider,{theme:aC(t,e),children:[R.jsx("link",{rel:"preconnect",href:"https://fonts.googleapis.com"}),R.jsx("link",{rel:"preconnect",href:"https://fonts.gstatic.com",crossOrigin:"anonymous"}),R.jsx("link",{href:"https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap",rel:"stylesheet"}),R.jsx(Pce,{}),!oe&&(li==null?void 0:li.length)!==0&&R.jsx(qce,{className:Mt?"visible":"",$botId:e,$iconSize:G,$removeLogoBackground:t.removeLogoBackground,topOffset:Wn,style:{right:F?"auto":"24px",left:F?"24px":"auto",display:"flex",flexDirection:"column",alignItems:F?"flex-start":"flex-end"},children:_e.suggestions?R.jsx(Bi,{isBot:!0,children:R.jsx(vs,{loading:_e.suggestions})}):li.map((Ce,Je)=>{if(Array.isArray(Ce)){const se=Ce.map(Ae=>({question:Ae.question,isHighlighted:Ae.isHighlighted??!1})).filter(Ae=>!ve.has(Ae.question));return R.jsx(RU,{$index:Je,$totalCount:li.length,children:R.jsx(hue,{messages:se,botId:e,uiData:t,onClick:Ae=>{const z=Ce.find(X=>X.question===Ae);$e(!0,"suggested_question_clicked"),Be(X=>{const ce=new Set(X);return ce.add(Ae),ce}),window.DebalesChat.track(Gt.SUGGESTED_QUESTION_CLICKED,{suggested_question:z}),!D&&z?(Ht(!1),pt(!0),r({message:z.Follow_up_Question??z.question,answer:z==null?void 0:z.answer,products:z==null?void 0:z.products,fromKlaviyo:!1,leading_queries:z==null?void 0:z.leading_queries,followUpQuestion:z==null?void 0:z.Follow_up_Question,messType:"suggested_question"})):(Ht(!1),pt(!0),A(!0),O(z))}})},Je)}else return R.jsx(RU,{$index:Je,$totalCount:li.length,children:R.jsx(fue,{value:`${Ce.question}`,index:Je,botId:e,uiData:t,onClose:()=>{Be(se=>{const Ae=new Set(se);return Ae.add(Ce.question),Ae})},onClick:()=>{$e(!0,"suggested_question_clicked"),Be(se=>{const Ae=new Set(se);return Ae.add(Ce.question),Ae}),window.DebalesChat.track(Gt.SUGGESTED_QUESTION_CLICKED,{suggested_question:Ce}),D?(Ht(!1),pt(!0),A(!0),O(Ce)):(Ht(!1),pt(!0),r({message:Ce.question,answer:Ce.answer,products:Ce.products,leading_queries:Ce.leading_queries,followUpQuestion:Ce.Follow_up_Question,messType:"suggested_question"}))},isHighlighted:Ce.isHighlighted??!1})},Je)})}),R.jsxs(Tce,{$topOffset:Wn,$removeLogoBackground:t.removeLogoBackground,style:{display:oe?"none":"inline-flex",right:F?"auto":"24px",left:F?"24px":"auto"},children:[R.jsx(Oce,{$showAnimation:t.enableChatButtonBackgroundAnimation??!1,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",$backgroundColor:t.backgroundColor||"#fff",$removeLogoBackground:t.removeLogoBackground,$topOffset:Wn,$iconSize:G}),R.jsx(Dce,{$backgroundColor:t.removeLogoBackground?"transparent":t.backgroundColor||"#fff",$botId:e,$iconSize:G,$removeLogoBackground:t.removeLogoBackground,topOffset:Wn,$showAnimation:t.enableChatButtonBackgroundAnimation,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",id:"debales-chat-button","data-testid":"debales-chat-button",onClick:()=>{if(window.DebalesChat.track(Gt.CHAT_BUTTON_CLICKED),$e(!oe,"chat_button_clicked"),!oe){const Ce=S();Ce.length>0?(m(Ce),Ht(!1),pt(!0)):(Ht(!0),pt(!1),we(Je=>({...Je,initialMessage:!0,leadingQueries:!0})),ht({text:!0,leadingQueries:!1,collections:!1,products:!1,variants:!1,cartSummary:!1,bulkItems:!1}),setTimeout(()=>{Ht(!1),pt(!0),we(se=>({...se,initialMessage:!1}));const Je={text:(t==null?void 0:t.initialMessage)||"Hi, I am an AI chatbot, how can I help you today?",isBot:!0};m([Je]),we(se=>({...se,leadingQueries:!0})),setTimeout(()=>{ht(se=>({...se,leadingQueries:!0}))},pr)},pr))}},"aria-label":"Chat",style:{display:oe?"none":"inline-flex",right:F?"auto":"24px",left:F?"24px":"auto"},children:R.jsx(Mce,{$botId:e,$removeLogoBackground:t.removeLogoBackground,"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"})})]}),oe&&vo.createPortal(R.jsx(R.Fragment,{children:R.jsxs(Bce,{$backgroundColor:t.backgroundColor||"#fff",$fullScreenOnMobile:t.fullScreenOnMobile,$headerFontColor:t.headerFontColor||"#1C1C1C",$isLeft:F,$topOffset:Wn,$mobileDisplayMode:t.mobileDisplayMode,$isVisible:me,"data-testid":"debales-chat-container",children:[R.jsxs(Nce,{$backgroundColor:ke(e)?t.backgroundColor:t.botBackgroundColor||"#fff",style:{color:t.headerFontColor},children:[R.jsx("div",{style:{flexGrow:.1,display:"flex",justifyContent:"center",alignItems:"center",overflow:"hidden",marginRight:"8px",gap:"10px"},children:ke(e)?R.jsxs(R.Fragment,{children:[R.jsx(kU,{"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"}),ke(e)?R.jsx("div",{children:R.jsx("img",{src:cA,alt:"Chat Bot Icon",style:{height:"36px"}})}):R.jsxs(Rce,{$botNameColor:t.headerFontColor,children:[R.jsxs("div",{style:{fontWeight:"700",wordBreak:"break-word"},children:[t.chatbotName,e==="catch-org-ai"&&R.jsx(bA,{children:"Beta"})]}),R.jsx(Ice,{})]})]}):R.jsx($ce,{children:R.jsxs(Lce,{children:[R.jsx(kU,{src:t.bot_logo,alt:"Chat Bot Icon"}),R.jsx("span",{style:{whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",display:"inline-block",maxWidth:"22ch"},children:t.chatbotName})]})})}),R.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",marginLeft:"auto",flexShrink:0},children:[Le&&R.jsxs("div",{style:{background:"#4CAF50",color:"white",padding:"4px 8px",borderRadius:"12px",fontSize:"12px",fontWeight:"500",display:"flex",alignItems:"center",gap:"4px"},children:[R.jsx("span",{style:{fontSize:"8px"},children:"●"}),"Live Support",R.jsx("button",{onClick:Dr,style:{background:"none",border:"none",color:"white",cursor:"pointer",fontSize:"12px",marginLeft:"4px",padding:"2px 4px",borderRadius:"4px"},title:"Exit Support",children:"✕"})]}),R.jsx(_A,{handleSupportClick:xr,isSupportMode:Le}),R.jsx(i1,{onClick:()=>{qe?vn(!0):$e(!1,"close_button_clicked")},id:"debales-minimize-button","data-testid":"debales-minimize-button","aria-label":"Minimize",children:R.jsx(hm,{botNameColor:t.headerFontColor})})]}),R.jsx(Wce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Vt?1:0}})]}),R.jsxs(Uce,{$backgroundColor:t.botBackgroundColor||"#fff",$mobileDisplayMode:t.mobileDisplayMode,children:[$o&&((et=($r=t.pluginConfig)==null?void 0:$r.collectedUserInfo)==null?void 0:et.discount)&&R.jsx(MA,{discount:t.pluginConfig.collectedUserInfo.discount,uiData:t,onClose:()=>{Ci(!1),window.DebalesChat.track(Gt.DISCOUNT_VIEWED,{discount:t.pluginConfig.collectedUserInfo.discount})}}),R.jsx(xA,{}),R.jsx(LA,{uidata:t,product:ln,showModel:Tr,setShowModel:qn,onVariantSelect:Ce=>{console.debug("VariantSelectionModel : Adding item to cart:",Ce),N(Ce.id,1,{productId:(ln==null?void 0:ln.productId)||""}),Ar(!0),window.DebalesChat.track(Gt.ADD_TO_CART_CLICKED,{product:Ce.title,price:Number(Ce.price),link:`https://${t.shopifyShopDomain}/variants/${Ce.id}`,message:l,chatId:tr()+e})},botId:e}),on&&R.jsx(RA,{setShowExitModal:vn,handleCloseChatbot:gr,chatId:tr()+e}),R.jsxs(zce,{id:"debales-conversation-group",$botBackgroundColor:t.botBackgroundColor||"#fff",ref:le,$mobileDisplayMode:t.mobileDisplayMode,"data-testid":"debales-conversation-group",children:[R.jsx(Qce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Vt?1:0}}),R.jsxs(Fce,{children:[mn&&h.map((Ce,Je)=>{const se=Ce.isBot,Ae=ap(Ce.text);return Ce.hideConfirmation?null:R.jsxs(tt.Fragment,{children:[se&&R.jsxs(TU,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff","data-testid":"debales-ai-message-container",children:[R.jsx("img",{src:se?t.bot_logo:t.userMessage_logo,alt:"Chat User Icon",style:{marginLeft:"8px"}}),R.jsxs(MU,{children:[Ae.map(z=>R.jsx("div",{children:R.jsx(Bi,{isBot:se,children:R.jsx(ad,{components:{a:({href:X,children:ce})=>{const be=()=>{X!=null&&X.startsWith("https://shop.catch.org/")&&window.DebalesChat.track(Gt.SHOPIFY_LINK_CLICKED)};return R.jsx("a",{href:X,target:"_blank",onClick:be,children:ce})}},remarkPlugins:[YS,uC],children:z})})})),Ce.collections&&l===Ce.conversationId&&it.collections&&R.jsx(TA,{message:Ce,botId:e,uidata:t,onSendMessage:r,loading:p}),Ce.products&&l===Ce.conversationId&&it.products&&R.jsx(BA,{message:Ce,botId:e,uidata:t,onSendMessage:r,loading:p,storeMetaData:n,platform:V,setShowVariantModel:qn,setSelectedProduct:Yi,setCartVersion:re}),o&&R.jsx(c6,{children:R.jsx(e1,{onClick:()=>{window._klOnsite&&(window._klOnsite.push(["openForm",Me]),$e(!1,"klaviyo_grab_deal_button"))},children:"🚀 Grab Deal Now!"})}),I&&I.itemCount>0&&l===Ce.conversationId&&it.cartSummary&&Hn&&R.jsx(CA,{onCheckout:Ot,onKeepShopping:()=>{Ar(!1)}}),nt&&nt.length>0&&l===Ce.conversationId&&it.bulkItems&&R.jsx(lA,{onClick:()=>ea(Ce.conversationId),children:"Add Bulk Items to Cart"}),k&&h.indexOf(Ce)===h.length-1&&R.jsx(R.Fragment,{children:R.jsx($A,{setShowModel:z=>{var ce,be;const X=typeof z=="function"?z(k):z;A(X),!X&&((be=(ce=t.pluginConfig)==null?void 0:ce.collectedUserInfo)!=null&&be.discount)&&Ci(!0)},inChat:!0,showFormInstructions:!1})}),zt&&h.indexOf(Ce)===h.length-1&&R.jsxs(Gce,{onSubmit:Ei,noValidate:!0,"data-testid":"debales-support-form",children:[ke(e)?R.jsxs(R.Fragment,{children:[R.jsx(Gl,{label:"Name",name:"name",type:"text",value:st.name,onChange:z=>yt(X=>({...X,name:z.target.value})),required:!0,error:Et.name,isCollectiveBot:!0}),R.jsx(Gl,{label:"Email",name:"email",type:"email",value:st.email,onChange:z=>yt(X=>({...X,email:z.target.value})),required:!0,error:Et.email,isCollectiveBot:!0}),R.jsx(IA,{label:"Message",name:"message",value:st.message,onChange:z=>yt(X=>({...X,message:z.target.value})),required:!0,error:Et.message,rows:4,isCollectiveBot:!0})]}):R.jsxs(R.Fragment,{children:[R.jsxs(f6,{children:[R.jsxs(d6,{children:["Name",R.jsx("span",{children:"*"})]}),R.jsx(IU,{type:"text",required:!0,value:st.name,onChange:z=>yt(X=>({...X,name:z.target.value})),placeholder:"John"}),Et.name&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.name})]}),R.jsxs(f6,{children:[R.jsxs(d6,{children:["Email",R.jsx("span",{children:"*"})]}),R.jsx(IU,{type:"email",required:!0,value:st.email,onChange:z=>yt(X=>({...X,email:z.target.value})),placeholder:"olivia@untitledui.com"}),Et.email&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.email})]}),R.jsxs(f6,{children:[R.jsxs(d6,{children:["Message",R.jsx("span",{children:"*"})]}),R.jsx(Vce,{required:!0,value:st.message,onChange:z=>yt(X=>({...X,message:z.target.value})),placeholder:"I need help with..."}),Et.message&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.message})]})]}),R.jsx(Zce,{children:R.jsx(Yce,{type:"submit",children:"Submit"})})]}),bt&&!p&&l===Ce.conversationId&&R.jsx(Bi,{isBot:se,children:R.jsx(vs,{loading:bt})})]})]}),!se&&R.jsx(jce,{$fontColor:t.userFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:R.jsx(Bi,{isBot:!!se,children:R.jsx(ad,{children:Ce.text})})}),h.length==1&&Je==0&&!k&&R.jsx(c6,{children:_e.leadingQueries?R.jsx(Bi,{isBot:se??!0,children:R.jsx(vs,{loading:_e.leadingQueries})}):si.map(z=>Array.isArray(z)?z[0]:z).map((z,X)=>R.jsxs(DU,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{D?(A(!0),O({question:z.question,answer:z.answer,products:z.products,path:"/",nameSpace:window.DebalesChat.projectId})):(Ht(!1),pt(!0),r({message:z.question,answer:z.answer,products:z.products,leading_queries:z.leading_queries,followUpQuestion:z.Follow_up_Question,messType:"leading_query"})),Be(ce=>{const be=new Set(ce);return be.add(z.question),be}),window.DebalesChat.track(Gt.INITIAL_LEADING_QUERY_SUGGESTED_QUESTION_CLICKED,{question:z.question})},disabled:p,$disabled:p,children:[z.question,R.jsx(OU,{color:t.callToActionFontColor})]},X))}),Ce.leading_queries&&Ce.leading_queries.length>0&&l===Ce.conversationId&&it.leadingQueries&&!o&&!k&&R.jsx(c6,{children:Ce.leading_queries.map((z,X)=>R.jsxs(DU,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{D?(A(!0),O({question:z,path:"/",nameSpace:window.DebalesChat.projectId})):r({message:z,messType:"leading_query"}),window.DebalesChat.track(Gt.LEADING_QUERY_CLICKED,{query:z})},disabled:p,$disabled:p,children:[z,R.jsx(OU,{color:t.callToActionFontColor})]},X))})]},Je)}),(p||Kt)&&!Le&&R.jsxs(TU,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:[R.jsx("img",{src:t.bot_logo,alt:"Chat User Icon"}),R.jsx(MU,{children:R.jsx(Bi,{isBot:!0,children:R.jsx("div",{style:{display:"flex",alignItems:"center",gap:"4px",animation:"fadeIn 0.3s ease-in-out"},children:R.jsx(vs,{loading:p||Kt})})})})]})]}),R.jsx(Kce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:At?1:0}})]}),R.jsxs("div",{className:"debales-footer-container",style:{width:"calc(100%-24px)",backgroundColor:t.botBackgroundColor||"#fff",color:t.fontColor||"#000"},children:[R.jsx(DA,{}),R.jsx("div",{children:R.jsx(NA,{disabled:p,inputState:!1,isInSupportMode:Le,onSupportMessage:wa})}),R.jsx(OA,{})]})]})]})}),Y.shadowRoot)]})},Xce=ee.p`
2512
+ => User Action: Klaviyo form closed`}return""},se=Ae=>{Ae instanceof CustomEvent&&Ce(Ae)};return window.addEventListener("klaviyoForms",se),()=>{window.removeEventListener("klaviyoForms",se)}},[fe,t,r]);const[$o,Ci]=ne.useState(!1),Wn=J==="Highest"?"120px":J==="Higher"?"60px":"15px",_o=async()=>{if(Le){console.warn("Support request called while already in support mode");return}try{const Ce=await fetch(`${io}/api/wordpress/support`,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({chatid:tr()+e,nameSpace:e,userName:st.name,userEmail:st.email,userMessage:st.message,tag:"support"})});if(Ce.ok){const Je=await Ce.json(),{contactId:se,ticketId:Ae}=Je;if(se&&Ae){const X=hp.connect(`${p7}/api/v1/live-agent`);if(!X){console.error("Failed to create support socket connection"),m(en=>[...en,{text:"Sorry, we couldn't establish a connection to support. Please try again.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}X.emit("join_ticket_conversation",{ticketID:Ae,role:"contact",contactID:se,nameSpace:e});const ce=en=>{console.log("Joined support conversation:",en),m(Ln=>[...Ln,{text:"You are now connected to live support. A support agent will join shortly.",isBot:!0,conversationId:`support-live-${Date.now()}`}])},be=en=>{console.log("Received conversation history:",en.length,"messages")},Se=en=>{console.log("Support agent joined:",en),m(Ln=>[...Ln,{text:"A support agent has joined the conversation. You can now chat directly with them.",isBot:!0,conversationId:`agent-joined-${Date.now()}`}])},Qe=en=>{console.log("New support message:",en),m(Ln=>[...Ln,{text:en.text||en.content,isBot:!0,conversationId:`support-msg-${Date.now()}`}])},Rt=()=>{console.log("Support connection disconnected"),m(en=>[...en,{text:"Support connection has ended. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-end-${Date.now()}`}]),Te(!1)};X.on("joined_ticket_conversation",ce),X.on("conversation_history",be),X.on("participant_joined",Se),X.on("message",Qe),X.on("disconnect",Rt),ge(X),Te(!0);const Qt=()=>{X.off("joined_ticket_conversation",ce),X.off("conversation_history",be),X.off("participant_joined",Se),X.off("new_message",Qe),X.off("disconnect",Rt)};X.cleanupHandlers=Qt}const z=`support-confirm-${Date.now()}`;m(X=>[...X,{text:"Thanks for reaching out! Our support team has received your request and will get back to you within 24 hours.<br>In the meantime, feel free to continue chatting with me - I'll do my best to help!",isBot:!0,leading_queries:_t,conversationId:z}]),d(z),ht(X=>({...X,leadingQueries:!0})),kt([]),console.log("Connected to support with existing user info:",Io())}else{const Je=await Ce.json();console.error("Failed to connect to support:",Je),m(se=>[...se,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}}catch{m(Je=>[...Je,{text:"Sorry, we couldn't connect you to support at the moment.<br>Please try again later.",isBot:!0}])}},xr=()=>{if(ke(e)){const Ae=dp("https://test.thecollective.in/content/customersupport");Ae.isValid&&(t!=null&&t.shopifyShopDomain&&(Ae.url.hostname=t==null?void 0:t.shopifyShopDomain),window.open(ms(Ae.url.toString()),"_blank"));return}if(Le){console.warn("Already in support mode");return}const Ce=Io();let Je=[];const se=h[h.length-1];se!=null&&se.leading_queries&&se.leading_queries.length>0?Je=se.leading_queries:h.length<=1&&(Je=si.flatMap(Ae=>Array.isArray(Ae)?Ae.map(z=>z.question):[Ae.question]).slice(0,4)),kt(Je),Ce&&Ce.name&&Ce.email&&yt({name:Ce==null?void 0:Ce.name,email:Ce==null?void 0:Ce.email,message:""}),m(Ae=>[...Ae,{text:"Please provide your name, email and message to connect with support.",isBot:!0}]),rt(!0)},Ei=async Ce=>{Ce.preventDefault();const Je={name:"",email:"",message:""};let se=!0;const Ae=st.name.trim(),z=st.message.trim();Ae||(Je.name="Name cannot be empty or just whitespace.",se=!1),(!st.email||!rm.isEmail(st.email))&&(Je.email="Please enter a valid email address.",se=!1),z||(Je.message="Message cannot be empty or just whitespace.",se=!1),te(Je),se&&(Uc({name:Ae,email:st.email}),rt(!1),m(X=>X.slice(0,-1)),await _o())};tt.useEffect(()=>{if(h.length===1&&h[0].isBot){we(Je=>({...Je,leadingQueries:!0}));const Ce=setTimeout(()=>{we(Je=>({...Je,leadingQueries:!1}))},2e3);return()=>clearTimeout(Ce)}},[h]),tt.useEffect(()=>()=>{ae&&(ae.cleanupHandlers&&ae.cleanupHandlers(),hp.disconnect(),ge(null),Te(!1))},[ae]);const Dr=()=>{hp.emit("send_message",{content:"The Contact has left the conversation.",type:"text",timestamp:new Date().toISOString()}),hp.emit("leave_conversation",{role:"contact"}),ae&&(ae.cleanupHandlers&&ae.cleanupHandlers(),hp.disconnect(),ge(null),Te(!1),m(Ce=>[...Ce,{text:"You have exited live support mode. You can continue chatting with me or request support again.",isBot:!0,conversationId:`support-exit-${Date.now()}`}]))},wa=Ce=>{if(!Le){console.warn("Attempted to send support message when not in support mode");return}if(!hp.getIsConnected()){console.error("Support socket is not connected"),m(Je=>[...Je,{text:"Connection lost. Please wait while we reconnect you to support.",isBot:!0,conversationId:`support-error-${Date.now()}`}]);return}hp.emit("send_message",{content:Ce,type:"text",timestamp:new Date().toISOString()}),m(Je=>[...Je,{text:Ce,isBot:!1,conversationId:`support-user-${Date.now()}`}])};return ne.useEffect(()=>{console.log(I,l,h,it.cartSummary,Hn)},[I,l,it.cartSummary,Hn,h]),!t||t.show_bot===!1||a?null:R.jsxs(ee.ThemeProvider,{theme:aC(t,e),children:[R.jsx("link",{rel:"preconnect",href:"https://fonts.googleapis.com"}),R.jsx("link",{rel:"preconnect",href:"https://fonts.gstatic.com",crossOrigin:"anonymous"}),R.jsx("link",{href:"https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,100;0,300;0,400;0,700;0,900;1,100;1,300;1,400;1,700;1,900&display=swap",rel:"stylesheet"}),R.jsx(Pce,{}),!oe&&(li==null?void 0:li.length)!==0&&R.jsx(qce,{className:Mt?"visible":"",$botId:e,$iconSize:G,$removeLogoBackground:t.removeLogoBackground,topOffset:Wn,style:{right:F?"auto":"24px",left:F?"24px":"auto",display:"flex",flexDirection:"column",alignItems:F?"flex-start":"flex-end"},children:_e.suggestions?R.jsx(Bi,{isBot:!0,children:R.jsx(vs,{loading:_e.suggestions})}):li.map((Ce,Je)=>{if(Array.isArray(Ce)){const se=Ce.map(Ae=>({question:Ae.question,isHighlighted:Ae.isHighlighted??!1})).filter(Ae=>!ve.has(Ae.question));return R.jsx(RU,{$index:Je,$totalCount:li.length,children:R.jsx(hue,{messages:se,botId:e,uiData:t,onClick:Ae=>{const z=Ce.find(X=>X.question===Ae);$e(!0,"suggested_question_clicked"),Be(X=>{const ce=new Set(X);return ce.add(Ae),ce}),window.DebalesChat.track(Gt.SUGGESTED_QUESTION_CLICKED,{suggested_question:z}),!D&&z?(Ht(!1),pt(!0),r({message:z.Follow_up_Question??z.question,answer:z==null?void 0:z.answer,products:z==null?void 0:z.products,fromKlaviyo:!1,leading_queries:z==null?void 0:z.leading_queries,followUpQuestion:z==null?void 0:z.Follow_up_Question,messType:"suggested_question"})):(Ht(!1),pt(!0),A(!0),O(z))}})},Je)}else return R.jsx(RU,{$index:Je,$totalCount:li.length,children:R.jsx(fue,{value:`${Ce.question}`,index:Je,botId:e,uiData:t,onClose:()=>{Be(se=>{const Ae=new Set(se);return Ae.add(Ce.question),Ae})},onClick:()=>{$e(!0,"suggested_question_clicked"),Be(se=>{const Ae=new Set(se);return Ae.add(Ce.question),Ae}),window.DebalesChat.track(Gt.SUGGESTED_QUESTION_CLICKED,{suggested_question:Ce}),D?(Ht(!1),pt(!0),A(!0),O(Ce)):(Ht(!1),pt(!0),r({message:Ce.question,answer:Ce.answer,products:Ce.products,leading_queries:Ce.leading_queries,followUpQuestion:Ce.Follow_up_Question,messType:"suggested_question"}))},isHighlighted:Ce.isHighlighted??!1})},Je)})}),R.jsxs(Tce,{$topOffset:Wn,$removeLogoBackground:t.removeLogoBackground,style:{display:oe?"none":"inline-flex",right:F?"auto":"24px",left:F?"24px":"auto"},children:[R.jsx(Oce,{$showAnimation:t.enableChatButtonBackgroundAnimation??!1,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",$backgroundColor:t.backgroundColor||"#fff",$removeLogoBackground:t.removeLogoBackground,$topOffset:Wn,$iconSize:G}),R.jsx(Dce,{$backgroundColor:t.removeLogoBackground?"transparent":t.backgroundColor||"#fff",$botId:e,$iconSize:G,$removeLogoBackground:t.removeLogoBackground,topOffset:Wn,$showAnimation:t.enableChatButtonBackgroundAnimation,$animationType:"Ping",$animationColor:t.chatButtonBackgroundAnimationColor||"#f48722",id:"debales-chat-button","data-testid":"debales-chat-button",onClick:()=>{if(window.DebalesChat.track(Gt.CHAT_BUTTON_CLICKED),$e(!oe,"chat_button_clicked"),!oe){const Ce=S();Ce.length>0?(m(Ce),Ht(!1),pt(!0)):(Ht(!0),pt(!1),we(Je=>({...Je,initialMessage:!0,leadingQueries:!0})),ht({text:!0,leadingQueries:!1,collections:!1,products:!1,variants:!1,cartSummary:!1,bulkItems:!1}),setTimeout(()=>{Ht(!1),pt(!0),we(se=>({...se,initialMessage:!1}));const Je={text:(t==null?void 0:t.initialMessage)||"Hi, I am an AI chatbot, how can I help you today?",isBot:!0};m([Je]),we(se=>({...se,leadingQueries:!0})),setTimeout(()=>{ht(se=>({...se,leadingQueries:!0}))},pr)},pr))}},"aria-label":"Chat",style:{display:oe?"none":"inline-flex",right:F?"auto":"24px",left:F?"24px":"auto"},children:R.jsx(Mce,{$botId:e,$removeLogoBackground:t.removeLogoBackground,"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"})})]}),oe&&vo.createPortal(R.jsx(R.Fragment,{children:R.jsxs(Bce,{$backgroundColor:t.backgroundColor||"#fff",$fullScreenOnMobile:t.fullScreenOnMobile,$headerFontColor:t.headerFontColor||"#1C1C1C",$isLeft:F,$topOffset:Wn,$mobileDisplayMode:t.mobileDisplayMode,$isVisible:me,"data-testid":"debales-chat-container",children:[R.jsxs(Nce,{$backgroundColor:ke(e)?t.backgroundColor:t.botBackgroundColor||"#fff",style:{color:t.headerFontColor},children:[R.jsx("div",{style:{flexGrow:.1,display:"flex",justifyContent:"center",alignItems:"center",overflow:"hidden",marginRight:"8px",gap:"10px"},children:ke(e)?R.jsxs(R.Fragment,{children:[R.jsx(kU,{"aria-hidden":"true",src:t.bot_logo,alt:"Chat Bot Icon"}),ke(e)?R.jsx("div",{children:R.jsx("img",{src:cA,alt:"Chat Bot Icon",style:{height:"36px"}})}):R.jsxs(Rce,{$botNameColor:t.headerFontColor,children:[R.jsxs("div",{style:{fontWeight:"700",wordBreak:"break-word"},children:[t.chatbotName,e==="catch-org-ai"&&R.jsx(bA,{children:"Beta"})]}),R.jsx(Ice,{})]})]}):R.jsx($ce,{children:R.jsxs(Lce,{children:[R.jsx(kU,{src:t.bot_logo,alt:"Chat Bot Icon"}),R.jsx("span",{style:{whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis",display:"inline-block",maxWidth:"22ch"},children:t.chatbotName})]})})}),R.jsxs("div",{style:{display:"flex",alignItems:"center",gap:"8px",marginLeft:"auto",flexShrink:0},children:[Le&&R.jsxs("div",{style:{background:"#4CAF50",color:"white",padding:"4px 8px",borderRadius:"12px",fontSize:"12px",fontWeight:"500",display:"flex",alignItems:"center",gap:"4px"},children:[R.jsx("span",{style:{fontSize:"8px"},children:"●"}),"Live Support",R.jsx("button",{onClick:Dr,style:{background:"none",border:"none",color:"white",cursor:"pointer",fontSize:"12px",marginLeft:"4px",padding:"2px 4px",borderRadius:"4px"},title:"Exit Support",children:"✕"})]}),R.jsx(_A,{handleSupportClick:xr,isSupportMode:Le}),R.jsx(i1,{onClick:()=>{qe?vn(!0):$e(!1,"close_button_clicked")},id:"debales-minimize-button","data-testid":"debales-minimize-button","aria-label":"Minimize",children:R.jsx(hm,{botNameColor:t.headerFontColor})})]}),R.jsx(Wce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Vt?1:0}})]}),R.jsxs(Uce,{$backgroundColor:t.botBackgroundColor||"#fff",$mobileDisplayMode:t.mobileDisplayMode,children:[$o&&((et=($r=t.pluginConfig)==null?void 0:$r.collectedUserInfo)==null?void 0:et.discount)&&R.jsx(MA,{discount:t.pluginConfig.collectedUserInfo.discount,uiData:t,onClose:()=>{Ci(!1),window.DebalesChat.track(Gt.DISCOUNT_VIEWED,{discount:t.pluginConfig.collectedUserInfo.discount})}}),R.jsx(xA,{}),R.jsx(LA,{uidata:t,product:ln,showModel:Tr,setShowModel:qn,onVariantSelect:Ce=>{console.debug("VariantSelectionModel : Adding item to cart:",Ce),N(Ce.id,1,{productId:(ln==null?void 0:ln.productId)||""}),Ar(!0),window.DebalesChat.track(Gt.ADD_TO_CART_CLICKED,{product:Ce.title,price:Number(Ce.price),link:`https://${t.shopifyShopDomain}/variants/${Ce.id}`,message:l,chatId:tr()+e})},botId:e}),on&&R.jsx(RA,{setShowExitModal:vn,handleCloseChatbot:gr,chatId:tr()+e}),R.jsxs(zce,{id:"debales-conversation-group",$botBackgroundColor:t.botBackgroundColor||"#fff",ref:le,$mobileDisplayMode:t.mobileDisplayMode,"data-testid":"debales-conversation-group",children:[R.jsx(Qce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:Vt?1:0}}),R.jsxs(Fce,{children:[mn&&h.map((Ce,Je)=>{const se=Ce.isBot,Ae=ap(Ce.text);return Ce.hideConfirmation?null:R.jsxs(tt.Fragment,{children:[se&&R.jsxs(TU,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff","data-testid":"debales-ai-message-container",children:[R.jsx("img",{src:se?t.bot_logo:t.userMessage_logo,alt:"Chat User Icon",style:{marginLeft:"8px"}}),R.jsxs(MU,{children:[Ae.map(z=>R.jsx("div",{children:R.jsx(Bi,{isBot:se,children:R.jsx(ad,{components:{a:({href:X,children:ce})=>{const be=()=>{X!=null&&X.startsWith("https://shop.catch.org/")&&window.DebalesChat.track(Gt.SHOPIFY_LINK_CLICKED)};return R.jsx("a",{href:X,target:"_blank",onClick:be,children:ce})}},remarkPlugins:[YS,uC],children:z})})})),Ce.collections&&l===Ce.conversationId&&it.collections&&R.jsx(TA,{message:Ce,botId:e,uidata:t,onSendMessage:r,loading:p}),Ce.products&&l===Ce.conversationId&&it.products&&R.jsx(BA,{message:Ce,botId:e,uidata:t,onSendMessage:r,loading:p,storeMetaData:n,platform:V,setShowVariantModel:qn,setSelectedProduct:Yi,setCartVersion:re}),o&&R.jsx(c6,{children:R.jsx(e1,{onClick:()=>{window._klOnsite&&(window._klOnsite.push(["openForm",Me]),$e(!1,"klaviyo_grab_deal_button"))},children:"🚀 Grab Deal Now!"})}),I&&I.itemCount>0&&l===Ce.conversationId&&it.cartSummary&&Hn&&R.jsx(CA,{onCheckout:Ot,onKeepShopping:()=>{Ar(!1)}}),nt&&nt.length>0&&l===Ce.conversationId&&it.bulkItems&&R.jsx(lA,{onClick:()=>ea(Ce.conversationId),children:"Add Bulk Items to Cart"}),k&&h.indexOf(Ce)===h.length-1&&R.jsx(R.Fragment,{children:R.jsx($A,{setShowModel:z=>{var ce,be;const X=typeof z=="function"?z(k):z;A(X),!X&&((be=(ce=t.pluginConfig)==null?void 0:ce.collectedUserInfo)!=null&&be.discount)&&Ci(!0)},inChat:!0,showFormInstructions:!1})}),zt&&h.indexOf(Ce)===h.length-1&&R.jsxs(Gce,{onSubmit:Ei,noValidate:!0,"data-testid":"debales-support-form",children:[ke(e)?R.jsxs(R.Fragment,{children:[R.jsx(Gl,{label:"Name",name:"name",type:"text",value:st.name,onChange:z=>yt(X=>({...X,name:z.target.value})),required:!0,error:Et.name,isCollectiveBot:!0}),R.jsx(Gl,{label:"Email",name:"email",type:"email",value:st.email,onChange:z=>yt(X=>({...X,email:z.target.value})),required:!0,error:Et.email,isCollectiveBot:!0}),R.jsx(IA,{label:"Message",name:"message",value:st.message,onChange:z=>yt(X=>({...X,message:z.target.value})),required:!0,error:Et.message,rows:4,isCollectiveBot:!0})]}):R.jsxs(R.Fragment,{children:[R.jsxs(f6,{children:[R.jsxs(d6,{children:["Name",R.jsx("span",{children:"*"})]}),R.jsx(IU,{type:"text",required:!0,value:st.name,onChange:z=>yt(X=>({...X,name:z.target.value})),placeholder:"John"}),Et.name&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.name})]}),R.jsxs(f6,{children:[R.jsxs(d6,{children:["Email",R.jsx("span",{children:"*"})]}),R.jsx(IU,{type:"email",required:!0,value:st.email,onChange:z=>yt(X=>({...X,email:z.target.value})),placeholder:"olivia@untitledui.com"}),Et.email&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.email})]}),R.jsxs(f6,{children:[R.jsxs(d6,{children:["Message",R.jsx("span",{children:"*"})]}),R.jsx(Vce,{required:!0,value:st.message,onChange:z=>yt(X=>({...X,message:z.target.value})),placeholder:"I need help with..."}),Et.message&&R.jsx("p",{style:{color:"red",fontSize:"12px",margin:"4px 0 0"},children:Et.message})]})]}),R.jsx(Zce,{children:R.jsx(Yce,{type:"submit",children:"Submit"})})]}),bt&&!p&&l===Ce.conversationId&&R.jsx(Bi,{isBot:se,children:R.jsx(vs,{loading:bt})})]})]}),!se&&R.jsx(jce,{$fontColor:t.userFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:R.jsx(Bi,{isBot:!!se,children:R.jsx(ad,{children:Ce.text})})}),h.length==1&&Je==0&&!k&&R.jsx(c6,{children:_e.leadingQueries?R.jsx(Bi,{isBot:se??!0,children:R.jsx(vs,{loading:_e.leadingQueries})}):si.map(z=>Array.isArray(z)?z[0]:z).map((z,X)=>R.jsxs(DU,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{D?(A(!0),O({question:z.question,answer:z.answer,products:z.products,path:"/",nameSpace:window.DebalesChat.projectId})):(Ht(!1),pt(!0),r({message:z.question,answer:z.answer,products:z.products,leading_queries:z.leading_queries,followUpQuestion:z.Follow_up_Question,messType:"leading_query"})),Be(ce=>{const be=new Set(ce);return be.add(z.question),be}),window.DebalesChat.track(Gt.INITIAL_LEADING_QUERY_SUGGESTED_QUESTION_CLICKED,{question:z.question})},disabled:p,$disabled:p,children:[z.question,R.jsx(OU,{color:t.callToActionFontColor})]},X))}),Ce.leading_queries&&Ce.leading_queries.length>0&&l===Ce.conversationId&&it.leadingQueries&&!o&&!k&&R.jsx(c6,{children:Ce.leading_queries.map((z,X)=>R.jsxs(DU,{style:{backgroundColor:t.callToActionBackgroundColor,color:t.callToActionFontColor},onClick:()=>{D?(A(!0),O({question:z,path:"/",nameSpace:window.DebalesChat.projectId})):r({message:z,messType:"leading_query"}),window.DebalesChat.track(Gt.LEADING_QUERY_CLICKED,{query:z})},disabled:p,$disabled:p,children:[z,R.jsx(OU,{color:t.callToActionFontColor})]},X))})]},Je)}),(p||Kt)&&!Le&&R.jsxs(TU,{$fontColor:t.botFontColor||"#000",$botMessageColor:t.botBackgroundColor||"#fff",children:[R.jsx("img",{src:t.bot_logo,alt:"Chat User Icon"}),R.jsx(MU,{children:R.jsx(Bi,{isBot:!0,children:R.jsx("div",{style:{display:"flex",alignItems:"center",gap:"4px",animation:"fadeIn 0.3s ease-in-out"},children:R.jsx(vs,{loading:p||Kt})})})})]})]}),R.jsx(Kce,{$backgroundColor:t.botBackgroundColor||"#fff",style:{opacity:At?1:0}})]}),R.jsxs("div",{className:"debales-footer-container",style:{width:"calc(100%-24px)",backgroundColor:t.botBackgroundColor||"#fff",color:t.fontColor||"#000"},children:[R.jsx(DA,{}),R.jsx("div",{children:R.jsx(NA,{disabled:p,inputState:!1,isInSupportMode:Le,onSupportMessage:wa})}),R.jsx(OA,{})]})]})]})}),Y.shadowRoot)]})},Xce=ee.p`
2513
2513
  margin: 0;
2514
2514
  font-size: ${e=>e.theme.typography.fontSizes.lg};
2515
2515
  text-align: left;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@debales/ai",
3
3
  "private": false,
4
4
  "access": "public",
5
- "version": "1.1.45",
5
+ "version": "1.1.46",
6
6
  "type": "module",
7
7
  "main": "./dist/module/ai.js",
8
8
  "types": "./dist/module/index.d.ts",
@@ -54,8 +54,8 @@
54
54
  "vite": "^6.2.0",
55
55
  "vite-plugin-dts": "^4.5.3",
56
56
  "@debales/eslint-config": "0.0.0",
57
- "@debales/typescript-config": "0.0.0",
58
- "@debales/shared": "0.0.1"
57
+ "@debales/shared": "0.0.1",
58
+ "@debales/typescript-config": "0.0.0"
59
59
  },
60
60
  "peerDependencies": {
61
61
  "react": "^18.0.0 || ^19.0.0",