tybotflow-widget 0.2.5 → 0.2.6

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.
package/dist/index.es.js CHANGED
@@ -48015,7 +48015,7 @@ const Y1 = "https://api.tybotflow.com/api/v1", W1 = "https://api.tybotflow.com/a
48015
48015
  const h = u?.mainColor || "#59168b", m = u?.secondaryColor || "#6366f1", y = u?.thirdColor || "#0f172b", E = u?.headerFirstColor || "#6e11b0", v = u?.headerSecondColor || "#372aac", w = u?.inputBackgroundColor || "#1f2937", A = u?.inputTextColor || "#fff", R = u?.roundness || 24, C = u?.chatRoundness || 16, N = (u?.minPanelWidth || 434) > window.innerWidth ? window.innerWidth : u?.minPanelWidth || 434, F = (u?.maxPanelWidth || window.innerWidth) > window.innerWidth ? window.innerWidth : u?.maxPanelWidth || window.innerWidth, O = u?.chatMaxWidth || 88, x = u?.toggleBtnBorderTopLeftRadius || 12, M = u?.toggleBtnBorderTopRightRadius || 0, U = u?.toggleBtnBorderBottomLeftRadius || 12, z = u?.toggleBtnBorderBottomRightRadius || 0, B = u?.menuBackgroundColor || "#1f2937", H = u?.menuTextColor || "#e5e7eb", V = u?.menuHoverBackgroundColor || "#374151", Q = u?.agentChatResponseColor || "#e5e7eb", te = u?.agentChatResponseBgColor || "#1f2937", re = u?.agentIconBgColor || "#59168b", q = u?.agentIconColor || "#fff", K = u?.userChatMessagesColor || "#fff", Y = u?.userChatMessagesBgColor || "#7a1ebe", oe = u?.userIconBgColor || "#7a1ebe", S = u?.userIconColor || "#fff", j = u?.ButtonColor || "#fff", $ = u?.ButtonsBgColor || "#59168b", I = u?.fontSize || 13, se = u?.toggleBtnPosition || "bottom-right", de = (() => {
48016
48016
  const D = u?.firstMessageKeyword;
48017
48017
  if (Array.isArray(D)) {
48018
- const ue = window.location.origin, pe = D.find((Ce) => Ce.url && ue.includes(Ce.url));
48018
+ const ue = window.location.origin, pe = D.find((Ce) => Ce.url && Ce.url.includes(ue));
48019
48019
  if (pe) return pe.message;
48020
48020
  const Ae = D.find((Ce) => !Ce.url || Ce.url === "");
48021
48021
  return Ae ? Ae.message : D[0]?.message || "hi";
package/dist/index.umd.js CHANGED
@@ -337,7 +337,7 @@ In order to be iterable, non-array objects must have a [Symbol.iterator]() metho
337
337
  opacity: 0;
338
338
  }
339
339
  }
340
- `);var Ut=he.memo(he.forwardRef(function(e,t){var n=e.sequence,a=e.repeat,o=e.className,l=e.speed,u=l===void 0?40:l,d=e.deletionSpeed,f=e.omitDeletionAnimation,p=f!==void 0&&f,g=e.preRenderFirstString,m=g!==void 0&&g,y=e.wrapper,E=y===void 0?"span":y,v=e.splitter,w=v===void 0?function(S){return ar([],Gn(S),!1)}:v,A=e.cursor,R=A===void 0||A,N=e.style,C=function(S,j){var $={};for(var I in S)Object.prototype.hasOwnProperty.call(S,I)&&j.indexOf(I)<0&&($[I]=S[I]);if(S!=null&&typeof Object.getOwnPropertySymbols=="function"){var se=0;for(I=Object.getOwnPropertySymbols(S);se<I.length;se++)j.indexOf(I[se])<0&&Object.prototype.propertyIsEnumerable.call(S,I[se])&&($[I[se]]=S[I[se]])}return $}(e,["sequence","repeat","className","speed","deletionSpeed","omitDeletionAnimation","preRenderFirstString","wrapper","splitter","cursor","style"]),F=C["aria-label"],O=C["aria-hidden"],x=C.role;d||(d=u);var M=new Array(2).fill(40);[u,d].forEach(function(S,j){switch(typeof S){case"number":M[j]=Math.abs(S-100);break;case"object":var $=S.type,I=S.value;if(typeof I!="number")break;$==="keyStrokeDelayInMs"&&(M[j]=I)}});var U,z,B,H,V,Q,ne=M[0],re=M[1],q=function(S,j){j===void 0&&(j=null);var $=he.useRef(j);return he.useEffect(function(){S&&(typeof S=="function"?S($.current):S.current=$.current)},[S]),$}(t),K=MY;U=o?"".concat(R?K+" ":"").concat(o):R?K:"",z=he.useRef(function(){var S,j=n;a===1/0?S=y1:typeof a=="number"&&(j=Array(1+a).fill(n).flat());var $=S?ar(ar([],Gn(j),!1),[S],!1):ar([],Gn(j),!1);return y1.apply(void 0,ar([q.current,w,ne,re,p],Gn($),!1)),function(){q.current}}),B=he.useRef(),H=he.useRef(!1),V=he.useRef(!1),Q=Gn(he.useState(0),2)[1],H.current&&(V.current=!0),he.useEffect(function(){return H.current||(B.current=z.current(),H.current=!0),Q(function(S){return S+1}),function(){V.current&&B.current&&B.current()}},[]);var Y=E,oe=m?n.find(function(S){return typeof S=="string"})||"":null;return Pn.createElement(Y,{"aria-hidden":O,"aria-label":F,role:x,style:N,className:U,children:F?Pn.createElement("span",{"aria-hidden":"true",ref:q,children:oe}):oe,ref:F?void 0:q})}),function(e,t){return!0});const v1="https://api.tybotflow.com/api/v1",S1="https://api.tybotflow.com/api/v1/web/send-message",FY="https://api.tybotflow.com/api/v1/chatbots-config",BY=({attachment:e,formatFileSize:t,mainColor:n,secondaryColor:a,controlsColor:o})=>{const{type:l,url:u,name:d,size:f}=e;switch(l){case"image":return L.jsxs("div",{className:"relative group",children:[L.jsx("img",{src:u,alt:d,className:"max-w-xs max-h-48 rounded-lg object-cover cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(u,"_blank")}),L.jsx("div",{className:"absolute bottom-2 left-2 bg-black bg-opacity-60 text-white text-xs px-2 py-1 rounded",children:t(f)})]});case"video":return L.jsxs("div",{className:"max-w-xs",children:[L.jsx("video",{src:u,controls:!0,className:"w-full max-h-48 rounded-lg",preload:"metadata",onError:()=>{console.error("Video failed to load:",u)},children:"Your browser does not support the video tag."}),L.jsxs("div",{className:"flex text-xs text-white mt-1 w-full overflow-hidden whitespace-nowrap text-ellipsis",children:[L.jsx("span",{className:"block w-full overflow-hidden whitespace-nowrap text-ellipsis",title:d,children:d}),L.jsxs("span",{className:"ml-2",children:["• ",t(f)]})]})]});case"audio":return L.jsxs("div",{className:"bg-gray-800 bg-opacity-70 p-3 rounded-lg flex-1 w-full",children:[L.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[L.jsx(nd,{className:"w-4 h-4",style:{color:n}}),L.jsx("span",{className:"text-sm font-medium truncate text-white",title:d,children:d})]}),L.jsx(UY,{src:u,mainColor:n,secondaryColor:a,controlsColor:o}),L.jsx("p",{className:"text-xs text-gray-400 mt-1",children:t(f)})]});case"document":return L.jsx("div",{className:"bg-gray-800 bg-opacity-50 p-3 rounded-lg max-w-xs cursor-pointer hover:bg-opacity-70 transition-colors",onClick:()=>window.open(u,"_blank"),children:L.jsxs("div",{className:"flex items-center gap-3",children:[L.jsx("div",{className:"w-10 h-10 bg-purple-600 rounded-lg flex items-center justify-center",children:L.jsx(Iw,{className:"w-5 h-5 text-white"})}),L.jsxs("div",{className:"flex-1 min-w-0",children:[L.jsx("p",{className:"text-sm font-medium truncate text-white",children:d}),L.jsx("p",{className:"text-xs text-gray-400",children:t(f)})]})]})});default:return null}},UY=({src:e,mainColor:t,secondaryColor:n,controlsColor:a})=>{const o=he.useRef(null),[l,u]=he.useState(1),{wavesurfer:d,isPlaying:f}=kY({container:o,waveColor:t,progressColor:n,barWidth:2,height:50,url:e}),p=()=>{d&&d.playPause()},g=()=>{const E=l===0?1:0;u(E),d?.setVolume(E)},m=()=>{d?.skip(-.096)},y=()=>{d?.skip(.096)};return L.jsxs("div",{className:"flex w-full flex-col items-center rounded-xl pt-1",children:[L.jsx("div",{ref:o,className:"w-full px-2"}),L.jsxs("div",{className:"flex w-full items-center justify-between",children:[L.jsx("div",{className:"relative flex items-center",children:L.jsx("button",{className:"transition-colors",style:{color:a},onClick:g,"aria-label":"Toggle volume",children:l===0?L.jsx(fz,{size:20}):L.jsx(pz,{size:20})})}),L.jsxs("div",{className:"-ml-9 flex flex-1 cursor-auto justify-center gap-2 rounded-full py-2 text-white",children:[L.jsx("button",{onClick:m,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":"Step back",children:L.jsx(sz,{})}),L.jsx("button",{onClick:p,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":f?"Pause":"Play",children:f?L.jsx(nz,{}):L.jsx(rz,{})}),L.jsx("button",{onClick:y,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":"Step forward",children:L.jsx(lz,{})})]})]})]})},PY=Pn.memo(({attachment:e,onRemove:t})=>L.jsxs("div",{className:"relative group w-24 h-24 rounded-xl overflow-hidden bg-gray-900 shadow-md border border-gray-700",children:[e.type==="image"&&e.url?L.jsx("img",{src:e.url,alt:e.name,className:"w-full h-full object-cover"}):L.jsxs("div",{className:"w-full h-full flex flex-col items-center justify-center text-gray-300 px-1 text-center text-xs",children:[e.type==="video"&&L.jsx(Dw,{className:"w-5 h-5 mb-1 text-red-400"}),e.type==="audio"&&L.jsx(nd,{className:"w-5 h-5 mb-1 text-green-400"}),e.type==="document"&&L.jsx(JP,{className:"w-5 h-5 mb-1 text-yellow-400"}),L.jsx("span",{className:"w-full truncate",title:e.name,children:e.name})]}),L.jsx("button",{onClick:t,className:"absolute top-1 right-1 w-5 h-5 bg-black bg-opacity-50 rounded-full text-white flex items-center justify-center hover:bg-opacity-80",title:"Remove",children:L.jsx(vo,{className:"w-3 h-3"})})]})),zY=({config:e})=>{const t=he.useRef(null),n=he.useRef(null),a=he.useRef(null),o=he.useRef(null),l=he.useRef(null),[u,d]=he.useState(null),[f,p]=he.useState(!0);he.useEffect(()=>{(async()=>{p(!0),await St.get(`${FY}/bot/${e.bot?.botId}`,{}).then(ue=>{d(ue.data)}).catch(ue=>{console.error("Error fetching bot config:",ue)}).finally(()=>{p(!1)})})()},[e.bot?.botId,e?.token]);const g=u?.mainColor||"#59168b",m=u?.secondaryColor||"#6366f1",y=u?.thirdColor||"#0f172b",E=u?.headerFirstColor||"#6e11b0",v=u?.headerSecondColor||"#372aac",w=u?.inputBackgroundColor||"#1f2937",A=u?.inputTextColor||"#fff",R=u?.roundness||24,N=u?.chatRoundness||16,C=(u?.minPanelWidth||434)>window.innerWidth?window.innerWidth:u?.minPanelWidth||434,F=(u?.maxPanelWidth||window.innerWidth)>window.innerWidth?window.innerWidth:u?.maxPanelWidth||window.innerWidth,O=u?.chatMaxWidth||88,x=u?.toggleBtnBorderTopLeftRadius||12,M=u?.toggleBtnBorderTopRightRadius||0,U=u?.toggleBtnBorderBottomLeftRadius||12,z=u?.toggleBtnBorderBottomRightRadius||0,B=u?.menuBackgroundColor||"#1f2937",H=u?.menuTextColor||"#e5e7eb",V=u?.menuHoverBackgroundColor||"#374151",Q=u?.agentChatResponseColor||"#e5e7eb",ne=u?.agentChatResponseBgColor||"#1f2937",re=u?.agentIconBgColor||"#59168b",q=u?.agentIconColor||"#fff",K=u?.userChatMessagesColor||"#fff",Y=u?.userChatMessagesBgColor||"#7a1ebe",oe=u?.userIconBgColor||"#7a1ebe",S=u?.userIconColor||"#fff",j=u?.ButtonColor||"#fff",$=u?.ButtonsBgColor||"#59168b",I=u?.fontSize||13,se=u?.toggleBtnPosition||"bottom-right",de=(()=>{const D=u?.firstMessageKeyword;if(Array.isArray(D)){const ue=window.location.origin,pe=D.find(Ne=>Ne.url&&ue.includes(Ne.url));if(pe)return pe.message;const Ae=D.find(Ne=>!Ne.url||Ne.url==="");return Ae?Ae.message:D[0]?.message||"hi"}return D||"hi"})(),[Se,Re]=he.useState(C),Qe=he.useRef(!1),ut=he.useRef(0),gt=he.useRef(C),Zt=D=>{Qe.current=!0,ut.current=D.clientX,gt.current=Se,document.body.style.cursor="ew-resize"};he.useEffect(()=>{const D=pe=>{if(!Qe.current)return;const Ae=ut.current-pe.clientX;let Ne=gt.current+Ae;Ne=Math.max(C,Math.min(F,Ne)),Re(Ne)},ue=()=>{Qe.current&&(Qe.current=!1,document.body.style.cursor="")};return window.addEventListener("mousemove",D),window.addEventListener("mouseup",ue),()=>{window.removeEventListener("mousemove",D),window.removeEventListener("mouseup",ue)}},[Se,C,F]);const[Ye,xt]=he.useState(!1),[me,ye]=he.useState(!1),[Fe,Be]=he.useState(!1),[ke,Ue]=he.useState(""),[et,ct]=he.useState(!1),[Pe,Ie]=he.useState([]),[ze,ee]=he.useState(!1),ae=he.useRef(0),[ge,Ee]=he.useState(!1),[Ge,Nt]=he.useState(!1),Ct=he.useRef(!1),[At,yt]=he.useState("00:00"),Xe=he.useRef(null),tt=he.useRef(0),[Mt,Gt]=he.useState([]),[Pt,nt]=he.useState([]),{startRecording:_n,stopRecording:ir,pauseRecording:gi,resumeRecording:Ia,status:Et,error:Vn}=F4.useReactMediaRecorder({video:!1,onStop(D,ue){if(Ct.current){Ct.current=!1,yt("00:00"),tt.current=0;return}Ie(pe=>[...pe,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),file:ue,type:"audio",url:D,name:`recording-${new Date().toISOString()}.webm`,size:ue.size}]),yt("00:00"),tt.current=0}}),Hr=u?.icon||e?.bot.avatar_url||SY,Gr=u?.title||e?.bot.name||"Tybot webchat",[Da,Yn]=he.useState(()=>{if(!u?.botNeedsAuth){let D=localStorage.getItem("conversationId");if(!D){const ue=new Uint8Array(16);window.crypto.getRandomValues(ue),D=Array.from(ue).map(pe=>pe.toString(16).padStart(2,"0")).join("")}return localStorage.setItem("conversationId",D),D}}),[ht,Jt]=he.useState(()=>u?.botNeedsAuth?!!localStorage.getItem("conversationId"):!0),[vn,Fo]=he.useState(""),[hi,Fb]=he.useState(""),[La,Ma]=he.useState(""),cn=he.useRef(!1),dn=Pt.reduce((D,ue)=>ue.seen?D:D+(ue.sender==="agent"?ue.nMessage||1:0),0),Wn=()=>{nt(D=>D.map(ue=>({...ue,seen:!0})))};he.useEffect(()=>{let D;return localStorage.getItem("webChatOpen")||(D=setTimeout(()=>{xt(!0),cn.current=!0,Wn(),localStorage.setItem("webChatOpen","true")},3200)),()=>clearTimeout(D)},[]),he.useEffect(()=>{const D=u?.widgetLoadBehavior;if(!D||!(()=>{if(!D.urls||D.urls.length===0)return!0;const Ae=window.location.origin,Ne=window.location.href;return D.urls.some(We=>{if(!We||We.trim()==="")return!0;try{return Ae.includes(We)||Ne.includes(We)||We.includes(Ae)}catch{return console.warn("Invalid URL in widgetLoadBehavior:",We),!1}})})())return;let pe;if(D.behavior==="after seconds")pe=setTimeout(()=>{Be(!0)},D.value*1e3);else if(D.behavior==="after scroll"){const Ae=()=>{const Ne=window.pageYOffset||document.documentElement.scrollTop,We=document.documentElement.scrollHeight-document.documentElement.clientHeight;Ne/We*100>=D.value&&(Be(!0),window.removeEventListener("scroll",Ae))};return window.addEventListener("scroll",Ae),()=>{window.removeEventListener("scroll",Ae)}}return()=>{pe&&clearTimeout(pe)}},[u?.widgetLoadBehavior]),he.useEffect(()=>{Ye&&Be(!1)},[Ye]),he.useEffect(()=>{const D=async()=>{if(e?.bot){ct(!0);try{const ue=await Fa(de,e?.bot.botId);nt(pe=>[...pe,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:ue,timestamp:new Date,seen:cn.current,nMessage:typeof ue=="string"?1:ue.length}])}catch(ue){console.error("Error sending message:",ue)}finally{ct(!1)}}};ht&&e?.bot&&Pt.length===0&&u&&(D(),localStorage.getItem("webChatAuth")&&JSON.parse(localStorage.getItem("webChatAuth"))&&u?.botNeedsAuth&&Da&&(vn||La||hi)&&(setTimeout(()=>{Ub(Da)},2e3),localStorage.setItem("webChatAuth","true")))},[ht,e?.bot,Da,u]),he.useEffect(()=>(Et==="recording"?Xe.current||(Xe.current=setInterval(()=>{tt.current+=1;const D=Math.floor(tt.current/60).toString().padStart(2,"0"),ue=(tt.current%60).toString().padStart(2,"0");yt(`${D}:${ue}`)},1e3)):Xe.current&&(clearInterval(Xe.current),Xe.current=null),()=>{Xe.current&&(clearInterval(Xe.current),Xe.current=null)}),[Et]),he.useEffect(()=>{Vn&&Vn.toLowerCase().includes("permission_denied")&&(Nt(!1),yt("00:00"),tt.current=0,Xe.current&&(clearInterval(Xe.current),Xe.current=null),alert("Microphone permission denied. Please allow access to record audio."))},[Vn]),he.useEffect(()=>{Et!=="recording"&&Et!=="paused"&&(yt("00:00"),tt.current=0)},[Et]);const or=he.useRef(null);he.useEffect(()=>{if(or&&or.current){const D=or.current;D.style.height="auto";const ue=120;D.scrollHeight>ue?(D.style.height=ue+"px",D.style.overflowY="auto"):(D.style.height=D.scrollHeight+"px",D.style.overflowY="hidden")}},[ke]),he.useEffect(()=>{(Ye||et)&&t.current&&(t.current.scrollTop=t.current.scrollHeight)},[Pt,et,Ye]);const _r=D=>{const ue=D.type.toLowerCase();return ue.startsWith("image/")?"image":ue.startsWith("video/")?"video":ue.startsWith("audio/")?"audio":"document"},mt=he.useCallback((D,ue)=>{const pe=[];Array.from(D).forEach(Ae=>{if(Ae.size>10*1024*1024){alert(`File ${Ae.name} is too large. Maximum size is 10MB.`);return}const Ne={id:Date.now().toString()+Math.random().toString(36).substring(2,9),file:Ae,type:_r(Ae),url:URL.createObjectURL(Ae),name:Ae.name,size:Ae.size};pe.push(Ne)}),Ie(Ae=>[...Ae,...pe]),ue&&ue.current&&(ue.current.value="")},[]),Fl=he.useCallback(D=>{D.preventDefault(),ae.current=0,ee(!1);const ue=D.dataTransfer.files;ue.length>0&&mt(ue)},[mt]),Bl=he.useCallback(D=>{D.preventDefault(),ae.current+=1,ee(!0)},[]),br=he.useCallback(D=>{D.preventDefault()},[]),Vr=he.useCallback(D=>{D.preventDefault(),ae.current-=1,ae.current<=0&&ee(!1)},[]),Bb=he.useCallback(D=>{Ie(ue=>{const pe=ue.find(Ae=>Ae.id===D);return pe&&URL.revokeObjectURL(pe.url),ue.filter(Ae=>Ae.id!==D)})},[]),Ul=D=>{if(D===0)return"0 Bytes";const ue=1024,pe=["Bytes","KB","MB","GB"],Ae=Math.floor(Math.log(D)/Math.log(ue));return Number.parseFloat((D/Math.pow(ue,Ae)).toFixed(2))+" "+pe[Ae]},sr=async(D=ke,ue)=>{if(D.trim()||Pe.length!==0){const Ae={id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"user",content:D,attachments:[...Pe],timestamp:new Date};if(nt(Ne=>[...Ne,Ae]),Ue(""),Ie([]),ct(!0),e?.bot)try{try{const Ne=await Fa(D,e?.bot.botId,Pe,ue||void 0);nt(We=>[...We,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:Ne,timestamp:new Date,seen:cn.current,nMessage:typeof Ne=="string"?1:Ne.length}])}catch{nt(Ne=>[...Ne,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:"Erreur du serveur",timestamp:new Date,seen:cn.current,nMessage:1}])}}catch(Ne){console.error("Error parsing selectedBotInfo:",Ne)}else nt(Ne=>[...Ne,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:"Aucun bot sélectionné",timestamp:new Date,seen:cn.current,nMessage:1}]);ct(!1)}else return},mi=D=>{switch(Ee(!1),D){case"image":n.current?.click();break;case"video":a.current?.click();break;case"audio":o.current?.click();break;case"document":l.current?.click();break}},Fa=async(D,ue,pe=[],Ae)=>{if(pe.length>0){const We=new FormData;We.append("type","image"),We.append("file",pe[0].file),We.append("botId",ue),We.append("conversation_id",Da??""),We.append("host",window.location.origin);const{data:fn}=await St.post(e?.API_URL||S1,We,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"multipart/form-data"}});return fn}else{let We;We={type:"text",messageText:D,botId:ue,conversation_id:Da,isintern:!!e.token,host:window.location.origin},Ae&&(We={...We,payload:Ae,messageText:Ae});const{data:fn}=await St.post(e?.API_URL||S1,We,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"application/json"}});return fn}},bi=/^[\u0600-\u06FF\u0750-\u077F\s\d\p{P}]+$/u,Xn=/[\u0600-\u06FF\u0750-\u077F]/,$t=D=>bi.test(D),Yr=D=>Xn.test(D),On=he.useMemo(()=>({h1:({children:D})=>L.jsx("h1",{className:"sm:text-2xl text-xl font-bold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h2:({children:D})=>L.jsx("h2",{className:"sm:text-xl text-lg font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h3:({children:D})=>L.jsx("h3",{className:"sm:text-lg text-base font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h4:({children:D})=>L.jsx("h4",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h5:({children:D})=>L.jsx("h5",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h6:({children:D})=>L.jsx("h6",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),p:({children:D})=>{let ue;return typeof D=="string"&&Yr(D)||typeof D=="object"&&!Array.isArray(D)&&D?.props?.children&&$t(D?.props?.children)||typeof D=="object"&&Array.isArray(D)&&D.some(pe=>Yr(pe))?ue="rtl":ue="ltr",L.jsx("div",{className:"leading-relaxed",dir:typeof D=="string"||!Array.isArray(D)?ue:"",children:Array.isArray(D)?D.map((pe,Ae)=>typeof pe=="string"&&!new RegExp("^(?:\\p{Emoji_Presentation}|\\p{Emoji}\\uFE0F|\\s)+$","u").test(pe)?L.jsx("span",{dir:Yr(pe)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[pe,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})},Ae):pe.type!=="br"?pe:null):L.jsx(L.Fragment,{children:D})})},ul:({children:D})=>L.jsx("ul",{className:"list-disc list-inside ml-4 my-2",style:{color:Q},dir:Yr(D)?"rtl":"ltr",children:D}),ol:({children:D})=>{let ue=!1;return D.filter(pe=>typeof pe!="string").every(pe=>Yr(pe?.props?.children))?ue=!0:ue=!1,L.jsx("ol",{className:"list-decimal list-inside ml-4 my-2",style:{color:Q},dir:ue?"rtl":"ltr",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D})},li:({children:D})=>L.jsx("li",{className:"my-1",dir:$t(D)?"rtl":"ltr",style:{color:Q},children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),a:({href:D,children:ue})=>L.jsx("a",{href:D,className:"text-blue-400 underline hover:text-blue-300",target:"_blank",rel:"noopener noreferrer",children:typeof ue=="string"?L.jsx(Ut,{sequence:[ue,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):ue}),blockquote:({children:D})=>L.jsx("blockquote",{className:"border-l-4 pl-4 italic my-2",style:{borderColor:$,color:Q},children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),code({node:D,className:ue,children:pe,...Ae}){const Ne=/language-(\w+)/.exec(ue||"");return Ne?L.jsx(f1,{...Ae,PreTag:"div",children:String(pe).replace(/\n$/,""),language:Ne[1],style:vY,customStyle:{scrollbarWidth:"thin"}}):L.jsx("code",{className:"px-1 rounded font-mono inline",style:{backgroundColor:w,color:Q},...Ae,children:typeof pe=="string"?L.jsx(Ut,{sequence:[pe,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):pe})},table:({children:D})=>L.jsx("table",{className:"min-w-full my-2",style:{border:`1px solid ${Q}`,color:ne,backgroundColor:Q},children:D}),thead:({children:D})=>L.jsx("thead",{children:D}),tbody:({children:D})=>L.jsx("tbody",{children:D}),tr:({children:D})=>L.jsx("tr",{style:{border:`1px solid ${ne}`},children:D}),th:({children:D})=>L.jsx("th",{className:"px-3 py-2 font-bold text-left",children:D}),td:({children:D})=>L.jsx("td",{className:"px-3 py-2",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),strong:({children:D})=>L.jsx("strong",{className:"font-bold",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),em:({children:D})=>L.jsx("em",{className:"italic ",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),hr:()=>L.jsx("hr",{className:"my-4",style:{borderColor:`1px solid ${Q}`}})}),[A,Q,$,w,N]);he.useEffect(()=>{const D=ue=>{ge&&ue.target instanceof Element&&!ue.composedPath().some(pe=>pe?.id==="upload-button-toggle"||pe?.id==="document-button"||pe?.id==="image-button"||pe?.id==="video-button"||pe?.id==="audio-button")&&Ee(!1)};return document.addEventListener("mousedown",D),()=>{document.removeEventListener("mousedown",D)}},[ge]);const Bo=D=>{switch(D.type){case"text":return L.jsx(bo,{remarkPlugins:[Eo],components:On,children:D.text?.replace(/\n/g,`
340
+ `);var Ut=he.memo(he.forwardRef(function(e,t){var n=e.sequence,a=e.repeat,o=e.className,l=e.speed,u=l===void 0?40:l,d=e.deletionSpeed,f=e.omitDeletionAnimation,p=f!==void 0&&f,g=e.preRenderFirstString,m=g!==void 0&&g,y=e.wrapper,E=y===void 0?"span":y,v=e.splitter,w=v===void 0?function(S){return ar([],Gn(S),!1)}:v,A=e.cursor,R=A===void 0||A,N=e.style,C=function(S,j){var $={};for(var I in S)Object.prototype.hasOwnProperty.call(S,I)&&j.indexOf(I)<0&&($[I]=S[I]);if(S!=null&&typeof Object.getOwnPropertySymbols=="function"){var se=0;for(I=Object.getOwnPropertySymbols(S);se<I.length;se++)j.indexOf(I[se])<0&&Object.prototype.propertyIsEnumerable.call(S,I[se])&&($[I[se]]=S[I[se]])}return $}(e,["sequence","repeat","className","speed","deletionSpeed","omitDeletionAnimation","preRenderFirstString","wrapper","splitter","cursor","style"]),F=C["aria-label"],O=C["aria-hidden"],x=C.role;d||(d=u);var M=new Array(2).fill(40);[u,d].forEach(function(S,j){switch(typeof S){case"number":M[j]=Math.abs(S-100);break;case"object":var $=S.type,I=S.value;if(typeof I!="number")break;$==="keyStrokeDelayInMs"&&(M[j]=I)}});var U,z,B,H,V,Q,ne=M[0],re=M[1],q=function(S,j){j===void 0&&(j=null);var $=he.useRef(j);return he.useEffect(function(){S&&(typeof S=="function"?S($.current):S.current=$.current)},[S]),$}(t),K=MY;U=o?"".concat(R?K+" ":"").concat(o):R?K:"",z=he.useRef(function(){var S,j=n;a===1/0?S=y1:typeof a=="number"&&(j=Array(1+a).fill(n).flat());var $=S?ar(ar([],Gn(j),!1),[S],!1):ar([],Gn(j),!1);return y1.apply(void 0,ar([q.current,w,ne,re,p],Gn($),!1)),function(){q.current}}),B=he.useRef(),H=he.useRef(!1),V=he.useRef(!1),Q=Gn(he.useState(0),2)[1],H.current&&(V.current=!0),he.useEffect(function(){return H.current||(B.current=z.current(),H.current=!0),Q(function(S){return S+1}),function(){V.current&&B.current&&B.current()}},[]);var Y=E,oe=m?n.find(function(S){return typeof S=="string"})||"":null;return Pn.createElement(Y,{"aria-hidden":O,"aria-label":F,role:x,style:N,className:U,children:F?Pn.createElement("span",{"aria-hidden":"true",ref:q,children:oe}):oe,ref:F?void 0:q})}),function(e,t){return!0});const v1="https://api.tybotflow.com/api/v1",S1="https://api.tybotflow.com/api/v1/web/send-message",FY="https://api.tybotflow.com/api/v1/chatbots-config",BY=({attachment:e,formatFileSize:t,mainColor:n,secondaryColor:a,controlsColor:o})=>{const{type:l,url:u,name:d,size:f}=e;switch(l){case"image":return L.jsxs("div",{className:"relative group",children:[L.jsx("img",{src:u,alt:d,className:"max-w-xs max-h-48 rounded-lg object-cover cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(u,"_blank")}),L.jsx("div",{className:"absolute bottom-2 left-2 bg-black bg-opacity-60 text-white text-xs px-2 py-1 rounded",children:t(f)})]});case"video":return L.jsxs("div",{className:"max-w-xs",children:[L.jsx("video",{src:u,controls:!0,className:"w-full max-h-48 rounded-lg",preload:"metadata",onError:()=>{console.error("Video failed to load:",u)},children:"Your browser does not support the video tag."}),L.jsxs("div",{className:"flex text-xs text-white mt-1 w-full overflow-hidden whitespace-nowrap text-ellipsis",children:[L.jsx("span",{className:"block w-full overflow-hidden whitespace-nowrap text-ellipsis",title:d,children:d}),L.jsxs("span",{className:"ml-2",children:["• ",t(f)]})]})]});case"audio":return L.jsxs("div",{className:"bg-gray-800 bg-opacity-70 p-3 rounded-lg flex-1 w-full",children:[L.jsxs("div",{className:"flex items-center gap-2 mb-2",children:[L.jsx(nd,{className:"w-4 h-4",style:{color:n}}),L.jsx("span",{className:"text-sm font-medium truncate text-white",title:d,children:d})]}),L.jsx(UY,{src:u,mainColor:n,secondaryColor:a,controlsColor:o}),L.jsx("p",{className:"text-xs text-gray-400 mt-1",children:t(f)})]});case"document":return L.jsx("div",{className:"bg-gray-800 bg-opacity-50 p-3 rounded-lg max-w-xs cursor-pointer hover:bg-opacity-70 transition-colors",onClick:()=>window.open(u,"_blank"),children:L.jsxs("div",{className:"flex items-center gap-3",children:[L.jsx("div",{className:"w-10 h-10 bg-purple-600 rounded-lg flex items-center justify-center",children:L.jsx(Iw,{className:"w-5 h-5 text-white"})}),L.jsxs("div",{className:"flex-1 min-w-0",children:[L.jsx("p",{className:"text-sm font-medium truncate text-white",children:d}),L.jsx("p",{className:"text-xs text-gray-400",children:t(f)})]})]})});default:return null}},UY=({src:e,mainColor:t,secondaryColor:n,controlsColor:a})=>{const o=he.useRef(null),[l,u]=he.useState(1),{wavesurfer:d,isPlaying:f}=kY({container:o,waveColor:t,progressColor:n,barWidth:2,height:50,url:e}),p=()=>{d&&d.playPause()},g=()=>{const E=l===0?1:0;u(E),d?.setVolume(E)},m=()=>{d?.skip(-.096)},y=()=>{d?.skip(.096)};return L.jsxs("div",{className:"flex w-full flex-col items-center rounded-xl pt-1",children:[L.jsx("div",{ref:o,className:"w-full px-2"}),L.jsxs("div",{className:"flex w-full items-center justify-between",children:[L.jsx("div",{className:"relative flex items-center",children:L.jsx("button",{className:"transition-colors",style:{color:a},onClick:g,"aria-label":"Toggle volume",children:l===0?L.jsx(fz,{size:20}):L.jsx(pz,{size:20})})}),L.jsxs("div",{className:"-ml-9 flex flex-1 cursor-auto justify-center gap-2 rounded-full py-2 text-white",children:[L.jsx("button",{onClick:m,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":"Step back",children:L.jsx(sz,{})}),L.jsx("button",{onClick:p,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":f?"Pause":"Play",children:f?L.jsx(nz,{}):L.jsx(rz,{})}),L.jsx("button",{onClick:y,className:"cursor-pointer hover:scale-110 transition-colors",style:{color:a},"aria-label":"Step forward",children:L.jsx(lz,{})})]})]})]})},PY=Pn.memo(({attachment:e,onRemove:t})=>L.jsxs("div",{className:"relative group w-24 h-24 rounded-xl overflow-hidden bg-gray-900 shadow-md border border-gray-700",children:[e.type==="image"&&e.url?L.jsx("img",{src:e.url,alt:e.name,className:"w-full h-full object-cover"}):L.jsxs("div",{className:"w-full h-full flex flex-col items-center justify-center text-gray-300 px-1 text-center text-xs",children:[e.type==="video"&&L.jsx(Dw,{className:"w-5 h-5 mb-1 text-red-400"}),e.type==="audio"&&L.jsx(nd,{className:"w-5 h-5 mb-1 text-green-400"}),e.type==="document"&&L.jsx(JP,{className:"w-5 h-5 mb-1 text-yellow-400"}),L.jsx("span",{className:"w-full truncate",title:e.name,children:e.name})]}),L.jsx("button",{onClick:t,className:"absolute top-1 right-1 w-5 h-5 bg-black bg-opacity-50 rounded-full text-white flex items-center justify-center hover:bg-opacity-80",title:"Remove",children:L.jsx(vo,{className:"w-3 h-3"})})]})),zY=({config:e})=>{const t=he.useRef(null),n=he.useRef(null),a=he.useRef(null),o=he.useRef(null),l=he.useRef(null),[u,d]=he.useState(null),[f,p]=he.useState(!0);he.useEffect(()=>{(async()=>{p(!0),await St.get(`${FY}/bot/${e.bot?.botId}`,{}).then(ue=>{d(ue.data)}).catch(ue=>{console.error("Error fetching bot config:",ue)}).finally(()=>{p(!1)})})()},[e.bot?.botId,e?.token]);const g=u?.mainColor||"#59168b",m=u?.secondaryColor||"#6366f1",y=u?.thirdColor||"#0f172b",E=u?.headerFirstColor||"#6e11b0",v=u?.headerSecondColor||"#372aac",w=u?.inputBackgroundColor||"#1f2937",A=u?.inputTextColor||"#fff",R=u?.roundness||24,N=u?.chatRoundness||16,C=(u?.minPanelWidth||434)>window.innerWidth?window.innerWidth:u?.minPanelWidth||434,F=(u?.maxPanelWidth||window.innerWidth)>window.innerWidth?window.innerWidth:u?.maxPanelWidth||window.innerWidth,O=u?.chatMaxWidth||88,x=u?.toggleBtnBorderTopLeftRadius||12,M=u?.toggleBtnBorderTopRightRadius||0,U=u?.toggleBtnBorderBottomLeftRadius||12,z=u?.toggleBtnBorderBottomRightRadius||0,B=u?.menuBackgroundColor||"#1f2937",H=u?.menuTextColor||"#e5e7eb",V=u?.menuHoverBackgroundColor||"#374151",Q=u?.agentChatResponseColor||"#e5e7eb",ne=u?.agentChatResponseBgColor||"#1f2937",re=u?.agentIconBgColor||"#59168b",q=u?.agentIconColor||"#fff",K=u?.userChatMessagesColor||"#fff",Y=u?.userChatMessagesBgColor||"#7a1ebe",oe=u?.userIconBgColor||"#7a1ebe",S=u?.userIconColor||"#fff",j=u?.ButtonColor||"#fff",$=u?.ButtonsBgColor||"#59168b",I=u?.fontSize||13,se=u?.toggleBtnPosition||"bottom-right",de=(()=>{const D=u?.firstMessageKeyword;if(Array.isArray(D)){const ue=window.location.origin,pe=D.find(Ne=>Ne.url&&Ne.url.includes(ue));if(pe)return pe.message;const Ae=D.find(Ne=>!Ne.url||Ne.url==="");return Ae?Ae.message:D[0]?.message||"hi"}return D||"hi"})(),[Se,Re]=he.useState(C),Qe=he.useRef(!1),ut=he.useRef(0),gt=he.useRef(C),Zt=D=>{Qe.current=!0,ut.current=D.clientX,gt.current=Se,document.body.style.cursor="ew-resize"};he.useEffect(()=>{const D=pe=>{if(!Qe.current)return;const Ae=ut.current-pe.clientX;let Ne=gt.current+Ae;Ne=Math.max(C,Math.min(F,Ne)),Re(Ne)},ue=()=>{Qe.current&&(Qe.current=!1,document.body.style.cursor="")};return window.addEventListener("mousemove",D),window.addEventListener("mouseup",ue),()=>{window.removeEventListener("mousemove",D),window.removeEventListener("mouseup",ue)}},[Se,C,F]);const[Ye,xt]=he.useState(!1),[me,ye]=he.useState(!1),[Fe,Be]=he.useState(!1),[ke,Ue]=he.useState(""),[et,ct]=he.useState(!1),[Pe,Ie]=he.useState([]),[ze,ee]=he.useState(!1),ae=he.useRef(0),[ge,Ee]=he.useState(!1),[Ge,Nt]=he.useState(!1),Ct=he.useRef(!1),[At,yt]=he.useState("00:00"),Xe=he.useRef(null),tt=he.useRef(0),[Mt,Gt]=he.useState([]),[Pt,nt]=he.useState([]),{startRecording:_n,stopRecording:ir,pauseRecording:gi,resumeRecording:Ia,status:Et,error:Vn}=F4.useReactMediaRecorder({video:!1,onStop(D,ue){if(Ct.current){Ct.current=!1,yt("00:00"),tt.current=0;return}Ie(pe=>[...pe,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),file:ue,type:"audio",url:D,name:`recording-${new Date().toISOString()}.webm`,size:ue.size}]),yt("00:00"),tt.current=0}}),Hr=u?.icon||e?.bot.avatar_url||SY,Gr=u?.title||e?.bot.name||"Tybot webchat",[Da,Yn]=he.useState(()=>{if(!u?.botNeedsAuth){let D=localStorage.getItem("conversationId");if(!D){const ue=new Uint8Array(16);window.crypto.getRandomValues(ue),D=Array.from(ue).map(pe=>pe.toString(16).padStart(2,"0")).join("")}return localStorage.setItem("conversationId",D),D}}),[ht,Jt]=he.useState(()=>u?.botNeedsAuth?!!localStorage.getItem("conversationId"):!0),[vn,Fo]=he.useState(""),[hi,Fb]=he.useState(""),[La,Ma]=he.useState(""),cn=he.useRef(!1),dn=Pt.reduce((D,ue)=>ue.seen?D:D+(ue.sender==="agent"?ue.nMessage||1:0),0),Wn=()=>{nt(D=>D.map(ue=>({...ue,seen:!0})))};he.useEffect(()=>{let D;return localStorage.getItem("webChatOpen")||(D=setTimeout(()=>{xt(!0),cn.current=!0,Wn(),localStorage.setItem("webChatOpen","true")},3200)),()=>clearTimeout(D)},[]),he.useEffect(()=>{const D=u?.widgetLoadBehavior;if(!D||!(()=>{if(!D.urls||D.urls.length===0)return!0;const Ae=window.location.origin,Ne=window.location.href;return D.urls.some(We=>{if(!We||We.trim()==="")return!0;try{return Ae.includes(We)||Ne.includes(We)||We.includes(Ae)}catch{return console.warn("Invalid URL in widgetLoadBehavior:",We),!1}})})())return;let pe;if(D.behavior==="after seconds")pe=setTimeout(()=>{Be(!0)},D.value*1e3);else if(D.behavior==="after scroll"){const Ae=()=>{const Ne=window.pageYOffset||document.documentElement.scrollTop,We=document.documentElement.scrollHeight-document.documentElement.clientHeight;Ne/We*100>=D.value&&(Be(!0),window.removeEventListener("scroll",Ae))};return window.addEventListener("scroll",Ae),()=>{window.removeEventListener("scroll",Ae)}}return()=>{pe&&clearTimeout(pe)}},[u?.widgetLoadBehavior]),he.useEffect(()=>{Ye&&Be(!1)},[Ye]),he.useEffect(()=>{const D=async()=>{if(e?.bot){ct(!0);try{const ue=await Fa(de,e?.bot.botId);nt(pe=>[...pe,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:ue,timestamp:new Date,seen:cn.current,nMessage:typeof ue=="string"?1:ue.length}])}catch(ue){console.error("Error sending message:",ue)}finally{ct(!1)}}};ht&&e?.bot&&Pt.length===0&&u&&(D(),localStorage.getItem("webChatAuth")&&JSON.parse(localStorage.getItem("webChatAuth"))&&u?.botNeedsAuth&&Da&&(vn||La||hi)&&(setTimeout(()=>{Ub(Da)},2e3),localStorage.setItem("webChatAuth","true")))},[ht,e?.bot,Da,u]),he.useEffect(()=>(Et==="recording"?Xe.current||(Xe.current=setInterval(()=>{tt.current+=1;const D=Math.floor(tt.current/60).toString().padStart(2,"0"),ue=(tt.current%60).toString().padStart(2,"0");yt(`${D}:${ue}`)},1e3)):Xe.current&&(clearInterval(Xe.current),Xe.current=null),()=>{Xe.current&&(clearInterval(Xe.current),Xe.current=null)}),[Et]),he.useEffect(()=>{Vn&&Vn.toLowerCase().includes("permission_denied")&&(Nt(!1),yt("00:00"),tt.current=0,Xe.current&&(clearInterval(Xe.current),Xe.current=null),alert("Microphone permission denied. Please allow access to record audio."))},[Vn]),he.useEffect(()=>{Et!=="recording"&&Et!=="paused"&&(yt("00:00"),tt.current=0)},[Et]);const or=he.useRef(null);he.useEffect(()=>{if(or&&or.current){const D=or.current;D.style.height="auto";const ue=120;D.scrollHeight>ue?(D.style.height=ue+"px",D.style.overflowY="auto"):(D.style.height=D.scrollHeight+"px",D.style.overflowY="hidden")}},[ke]),he.useEffect(()=>{(Ye||et)&&t.current&&(t.current.scrollTop=t.current.scrollHeight)},[Pt,et,Ye]);const _r=D=>{const ue=D.type.toLowerCase();return ue.startsWith("image/")?"image":ue.startsWith("video/")?"video":ue.startsWith("audio/")?"audio":"document"},mt=he.useCallback((D,ue)=>{const pe=[];Array.from(D).forEach(Ae=>{if(Ae.size>10*1024*1024){alert(`File ${Ae.name} is too large. Maximum size is 10MB.`);return}const Ne={id:Date.now().toString()+Math.random().toString(36).substring(2,9),file:Ae,type:_r(Ae),url:URL.createObjectURL(Ae),name:Ae.name,size:Ae.size};pe.push(Ne)}),Ie(Ae=>[...Ae,...pe]),ue&&ue.current&&(ue.current.value="")},[]),Fl=he.useCallback(D=>{D.preventDefault(),ae.current=0,ee(!1);const ue=D.dataTransfer.files;ue.length>0&&mt(ue)},[mt]),Bl=he.useCallback(D=>{D.preventDefault(),ae.current+=1,ee(!0)},[]),br=he.useCallback(D=>{D.preventDefault()},[]),Vr=he.useCallback(D=>{D.preventDefault(),ae.current-=1,ae.current<=0&&ee(!1)},[]),Bb=he.useCallback(D=>{Ie(ue=>{const pe=ue.find(Ae=>Ae.id===D);return pe&&URL.revokeObjectURL(pe.url),ue.filter(Ae=>Ae.id!==D)})},[]),Ul=D=>{if(D===0)return"0 Bytes";const ue=1024,pe=["Bytes","KB","MB","GB"],Ae=Math.floor(Math.log(D)/Math.log(ue));return Number.parseFloat((D/Math.pow(ue,Ae)).toFixed(2))+" "+pe[Ae]},sr=async(D=ke,ue)=>{if(D.trim()||Pe.length!==0){const Ae={id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"user",content:D,attachments:[...Pe],timestamp:new Date};if(nt(Ne=>[...Ne,Ae]),Ue(""),Ie([]),ct(!0),e?.bot)try{try{const Ne=await Fa(D,e?.bot.botId,Pe,ue||void 0);nt(We=>[...We,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:Ne,timestamp:new Date,seen:cn.current,nMessage:typeof Ne=="string"?1:Ne.length}])}catch{nt(Ne=>[...Ne,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:"Erreur du serveur",timestamp:new Date,seen:cn.current,nMessage:1}])}}catch(Ne){console.error("Error parsing selectedBotInfo:",Ne)}else nt(Ne=>[...Ne,{id:Date.now().toString()+Math.random().toString(36).substring(2,9),sender:"agent",content:"Aucun bot sélectionné",timestamp:new Date,seen:cn.current,nMessage:1}]);ct(!1)}else return},mi=D=>{switch(Ee(!1),D){case"image":n.current?.click();break;case"video":a.current?.click();break;case"audio":o.current?.click();break;case"document":l.current?.click();break}},Fa=async(D,ue,pe=[],Ae)=>{if(pe.length>0){const We=new FormData;We.append("type","image"),We.append("file",pe[0].file),We.append("botId",ue),We.append("conversation_id",Da??""),We.append("host",window.location.origin);const{data:fn}=await St.post(e?.API_URL||S1,We,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"multipart/form-data"}});return fn}else{let We;We={type:"text",messageText:D,botId:ue,conversation_id:Da,isintern:!!e.token,host:window.location.origin},Ae&&(We={...We,payload:Ae,messageText:Ae});const{data:fn}=await St.post(e?.API_URL||S1,We,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"application/json"}});return fn}},bi=/^[\u0600-\u06FF\u0750-\u077F\s\d\p{P}]+$/u,Xn=/[\u0600-\u06FF\u0750-\u077F]/,$t=D=>bi.test(D),Yr=D=>Xn.test(D),On=he.useMemo(()=>({h1:({children:D})=>L.jsx("h1",{className:"sm:text-2xl text-xl font-bold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h2:({children:D})=>L.jsx("h2",{className:"sm:text-xl text-lg font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h3:({children:D})=>L.jsx("h3",{className:"sm:text-lg text-base font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h4:({children:D})=>L.jsx("h4",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h5:({children:D})=>L.jsx("h5",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),h6:({children:D})=>L.jsx("h6",{className:"sm:text-base text-sm font-semibold my-2",dir:$t(D)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})}),p:({children:D})=>{let ue;return typeof D=="string"&&Yr(D)||typeof D=="object"&&!Array.isArray(D)&&D?.props?.children&&$t(D?.props?.children)||typeof D=="object"&&Array.isArray(D)&&D.some(pe=>Yr(pe))?ue="rtl":ue="ltr",L.jsx("div",{className:"leading-relaxed",dir:typeof D=="string"||!Array.isArray(D)?ue:"",children:Array.isArray(D)?D.map((pe,Ae)=>typeof pe=="string"&&!new RegExp("^(?:\\p{Emoji_Presentation}|\\p{Emoji}\\uFE0F|\\s)+$","u").test(pe)?L.jsx("span",{dir:Yr(pe)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[pe,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:90})},Ae):pe.type!=="br"?pe:null):L.jsx(L.Fragment,{children:D})})},ul:({children:D})=>L.jsx("ul",{className:"list-disc list-inside ml-4 my-2",style:{color:Q},dir:Yr(D)?"rtl":"ltr",children:D}),ol:({children:D})=>{let ue=!1;return D.filter(pe=>typeof pe!="string").every(pe=>Yr(pe?.props?.children))?ue=!0:ue=!1,L.jsx("ol",{className:"list-decimal list-inside ml-4 my-2",style:{color:Q},dir:ue?"rtl":"ltr",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D})},li:({children:D})=>L.jsx("li",{className:"my-1",dir:$t(D)?"rtl":"ltr",style:{color:Q},children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),a:({href:D,children:ue})=>L.jsx("a",{href:D,className:"text-blue-400 underline hover:text-blue-300",target:"_blank",rel:"noopener noreferrer",children:typeof ue=="string"?L.jsx(Ut,{sequence:[ue,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):ue}),blockquote:({children:D})=>L.jsx("blockquote",{className:"border-l-4 pl-4 italic my-2",style:{borderColor:$,color:Q},children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),code({node:D,className:ue,children:pe,...Ae}){const Ne=/language-(\w+)/.exec(ue||"");return Ne?L.jsx(f1,{...Ae,PreTag:"div",children:String(pe).replace(/\n$/,""),language:Ne[1],style:vY,customStyle:{scrollbarWidth:"thin"}}):L.jsx("code",{className:"px-1 rounded font-mono inline",style:{backgroundColor:w,color:Q},...Ae,children:typeof pe=="string"?L.jsx(Ut,{sequence:[pe,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):pe})},table:({children:D})=>L.jsx("table",{className:"min-w-full my-2",style:{border:`1px solid ${Q}`,color:ne,backgroundColor:Q},children:D}),thead:({children:D})=>L.jsx("thead",{children:D}),tbody:({children:D})=>L.jsx("tbody",{children:D}),tr:({children:D})=>L.jsx("tr",{style:{border:`1px solid ${ne}`},children:D}),th:({children:D})=>L.jsx("th",{className:"px-3 py-2 font-bold text-left",children:D}),td:({children:D})=>L.jsx("td",{className:"px-3 py-2",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),strong:({children:D})=>L.jsx("strong",{className:"font-bold",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),em:({children:D})=>L.jsx("em",{className:"italic ",children:typeof D=="string"?L.jsx(Ut,{sequence:[D,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],cursor:!1,speed:92}):D}),hr:()=>L.jsx("hr",{className:"my-4",style:{borderColor:`1px solid ${Q}`}})}),[A,Q,$,w,N]);he.useEffect(()=>{const D=ue=>{ge&&ue.target instanceof Element&&!ue.composedPath().some(pe=>pe?.id==="upload-button-toggle"||pe?.id==="document-button"||pe?.id==="image-button"||pe?.id==="video-button"||pe?.id==="audio-button")&&Ee(!1)};return document.addEventListener("mousedown",D),()=>{document.removeEventListener("mousedown",D)}},[ge]);const Bo=D=>{switch(D.type){case"text":return L.jsx(bo,{remarkPlugins:[Eo],components:On,children:D.text?.replace(/\n/g,`
341
341
  `)});case"single-choice":return L.jsxs(L.Fragment,{children:[L.jsx(bo,{remarkPlugins:[Eo],components:On,children:D.text?.replace(/\n/g,`
342
342
  `)||""}),L.jsx("div",{className:"flex flex-wrap",dir:$t(D.text||"")?"rtl":"ltr",children:D.choices?.map((pe,Ae)=>L.jsx("button",{className:"mb-1 mr-1 px-4 py-1 hover:scale-105 transition-all duration-100",onClick:()=>{sr(pe.title)},style:{background:$,borderRadius:`${N}px`,color:j},dir:$t(pe.title)?"rtl":"ltr",children:L.jsx(Ut,{sequence:[pe.title,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],wrapper:"span",cursor:!1,speed:92})},Ae))})]});case"image":return L.jsx(L.Fragment,{children:L.jsx("div",{className:"relative group",children:L.jsx("img",{src:D.image,className:"max-w-xs max-h-48 rounded-lg object-cover cursor-pointer hover:opacity-90 transition-opacity",onClick:()=>window.open(D.image,"_blank")})})});case"dropdown":return L.jsxs(L.Fragment,{children:[L.jsx(bo,{remarkPlugins:[Eo],components:On,children:D.message?.replace(/\n/g,`
343
343
  `)||""}),L.jsx("div",{className:"flex flex-wrap",dir:$t(D.message||"")?"rtl":"ltr",children:D.options?.map((pe,Ae)=>L.jsx("button",{className:"mb-1 mr-1 px-4 py-1 hover:scale-105 transition-all duration-100",onClick:()=>{sr(pe.value)},style:{background:$,borderRadius:`${N}px`,color:j},dir:$t(D.message||"")?"rtl":"ltr",children:L.jsx(Ut,{sequence:[pe.label,0,()=>{t.current&&(t.current.scrollTop=t.current.scrollHeight)}],wrapper:"span",cursor:!1,speed:92})},Ae))})]});case"carousel":const ue=(D.items?.length||0)<2?1:2;return L.jsx("div",{className:"mt-2 w-full",children:L.jsx("div",{className:`grid grid-cols-1 ${ue<2?"sm:grid-cols-1":"sm:grid-cols-2"} gap-3`,children:D.items?.map((pe,Ae)=>L.jsxs("div",{className:"flex-shrink-0 border p-3 shadow-md",style:{background:ne,borderRadius:`${N}px`},children:[pe.image&&L.jsx("img",{src:pe.image,alt:pe.title,className:"h-32 w-full object-cover rounded-md mb-2"}),L.jsx("h3",{className:"text-sm font-bold",title:pe.title,children:pe.title}),L.jsx("p",{className:"text-xs text-gray-300 mb-2 line-clamp-2",title:pe.subtitle,children:pe.subtitle}),pe.actions?.map((Ne,We)=>{const fn=typeof Ne.payload=="string"?Ne.payload:JSON.stringify(Ne.payload),Kn=typeof Ne.payload=="string"?Ne.payload:D.title;return L.jsx("button",{className:"mb-1 mr-1 px-4 py-1 hover:scale-105 transition-all duration-100",onClick:()=>{Ne.action==="Open URL"?window.open(Ne.url,"_blank"):Ne.title==="scan"?fetch(pe.image).then(lr=>lr.blob()).then(lr=>{const zl=URL.createObjectURL(lr),Xr=document.createElement("a");Xr.href=zl,Xr.download="qrcode.png",document.body.appendChild(Xr),Xr.click(),document.body.removeChild(Xr),URL.revokeObjectURL(zl)}).catch(()=>{alert("Failed to download image.")}):(Ne.action,sr(Kn,fn))},style:{background:$,borderRadius:`${N}px`,color:j},children:Ne.title},We)})]},Ae))})});case"card":return L.jsx("div",{className:"mt-2 w-full",children:L.jsx("div",{className:"grid grid-cols-1 gap-3",children:L.jsxs("div",{className:"flex-shrink-0 border rounded-lg p-3 shadow-md",style:{background:ne},children:[D.image&&L.jsx("img",{src:D.image,alt:D.title,className:"h-32 w-full object-cover rounded-md mb-2"}),L.jsx("h3",{className:"font-bold",children:D.title}),D.actions?.map((pe,Ae)=>{let Ne;try{Ne=typeof pe.payload=="string"?JSON.parse(pe.payload):pe.payload}catch{Ne={productName:D.title}}return L.jsx("button",{className:"mb-1 mr-1 px-4 py-1 hover:scale-105 transition-all duration-100",onClick:()=>{pe.action==="Open URL"?window.open(pe.url,"_blank"):pe.title==="scan"?D.image&&fetch(D.image).then(We=>We.blob()).then(We=>{const fn=URL.createObjectURL(We),Kn=document.createElement("a");Kn.href=fn,Kn.download="qrcode.png",document.body.appendChild(Kn),Kn.click(),document.body.removeChild(Kn),URL.revokeObjectURL(fn)}).catch(()=>{alert("Failed to download image.")}):sr(Ne.productName||D.title)},style:{background:$,borderRadius:`${N}px`,color:j},children:pe.title},Ae)})]})})})}},Uo=async D=>{if(!ht)return;const ue=await fetch(`${v1}/endusers/get-by-username`,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"application/json"},method:"POST",body:JSON.stringify({username:`web_user_${D}`})});if(!ue.ok)throw new Error(`HTTP error! status: ${ue.status}`);return(await ue.json()).id},Ub=async D=>{if(!ht)return;const ue=await Uo(D);let pe;vn&&(pe={last_name:vn.split(" ")[1]||vn.split(" ")[0]||`web_user_${D}`,first_name:vn.split(" ")[0]||`web_user_${D}`}),hi&&(pe={...pe,phone:hi}),La&&(pe={...pe,email:La});const Ae=await fetch(`${v1}/endusers/${ue}`,{headers:{Authorization:`Bearer ${e?.token}`,"Content-Type":"application/json"},method:"PUT",body:JSON.stringify(pe)});if(!Ae.ok)throw new Error(`HTTP error! status: ${Ae.status}`);return await Ae.json()},Pl=async D=>{D.preventDefault();const ue=new Uint8Array(16);window.crypto.getRandomValues(ue);const pe=Array.from(ue).map(Ae=>Ae.toString(16).padStart(2,"0")).join("");Yn(pe),localStorage.setItem("conversationId",pe),localStorage.setItem("webChatAuth","true"),Jt(!0)},[pn,Wr]=he.useState(!1),Pb=async()=>{nt([]);const D=new Uint8Array(16);window.crypto.getRandomValues(D);const ue=Array.from(D).map(pe=>pe.toString(16).padStart(2,"0")).join("");Yn(ue),localStorage.setItem("conversationId",ue)},yi=`linear-gradient(to bottom right, ${y}, ${g}, ${y})`,Po=`linear-gradient(to right, ${g}, ${m})`,Ei=`linear-gradient(to right, ${E}, ${v})`;return f||!u?null:L.jsxs(L.Fragment,{children:[L.jsx("style",{children:`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tybotflow-widget",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "A modern, customizable React widget for Tybot chat integration.",
5
5
  "license": "ISC",
6
6
  "main": "dist/index.umd.js",