const src=document.currentScript.api,customerName=document.currentScript.customerName,scrollbarWidth=parent.window.innerWidth-parent.document.documentElement.clientWidth,primaryColor=document.currentScript.primaryColor??null,onChatLoad=document.currentScript.onChatLoad??null,toggleHidableChat=document.currentScript.toggleHidableChat??null,hostToken=window.btoa(window.top.location.host).replaceAll("=",""),chatUrl=document.currentScript.host+"/"+hostToken+"/chat",classes=document.currentScript.classes??null;let isLogged="1"===document.currentScript.isLogged,isOpen=!1,iframe=null,wrapper=null;function chatIndexFunction(){console.log("Hello you are using a script chat function")}function init(){[iframe,wrapper]=createChatIframe(),iframe.addEventListener("load",(()=>emitInitData(iframe))),window.visualViewport.onresize=t=>handleVisualViewportResize(t)}function createChatIframe(){const t=document.createElement("iframe");t.id="pro-chat-iframe",t.origin=!0,t.src=chatUrl,t.sandbox="allow-same-origin allow-scripts allow-top-navigation allow-popups",t.scrolling="no";const e=createIframeWrapper();return e.appendChild(t),document.body.appendChild(e),new ResizeObserver((t=>handleWrapperResize(t))).observe(e),applyStyles(e,wrapperFixedStyles()),applyStateStyles(e,isOpen),applyStyles(t,iframeFixedStyles()),[t,e]}function createIframeWrapper(){const t=document.createElement("div");return t.id="pro-chat-wrapper",t.setAttribute("class",classes),t}function emitInitData(t){const e=onChatLoad?onChatLoad():null,n=window.innerWidth<800;emit(t,{action:"init",params:{customerName,isLogged,metadata:e,isMobile:n,primaryColor}})}function emit(t,e){const n=new URL(t.src).origin;t.contentWindow.postMessage(e,n)}parent.window.proassistant={},parent.window.proassistant.chatIndexFunction=()=>chatIndexFunction(),parent.window.proassistant.openChat=()=>openChatFromExternal(),window.addEventListener("message",(t=>{t.data.action&&Object(actions).hasOwnProperty(t.data.action)&&actions[t.data.action](t.data.params)}));const actions={"external:toggle:chat":t=>toggleChat(t.isOpen),"external:navigate:new:tab":t=>navigateNewTab(t.url)};function openChatFromExternal(){emit(document.getElementById("pro-chat-iframe"),{action:"external:open:chat",params:{isOpen:!0}})}function toggleChat(t){isOpen=t,applyStateStyles(wrapper,isOpen),toggleHidableChat(t)}function navigateNewTab(t){window.open(t,"_blank")}function wrapperFixedStyles(){return{transform:"rotate(180deg)",position:"fixed",zIndex:999,overflow:"hidden"}}function iframeFixedStyles(){return{height:"100%",width:"100%",transform:"rotate(180deg)"}}function applyStateStyles(t,e){const n={boxShadow:"none",bottom:"2rem",right:"2rem",resize:"none",borderRadius:0,minWidth:0,maxWidth:"none",maxHeight:"none",minHeight:0},i={height:"85px",width:"85px"},a={...n,...i},o=isApp()?window.visualViewport.height-200:window.visualViewport.height,r={...n,width:`calc(100vw - ${scrollbarWidth}px)`,height:`${o}px`,right:0,bottom:0},c={...n,...i,display:"flex",resize:"none"},s={...n,...dynamicSizeOpenedDesktop(),resize:"both",boxShadow:"2px 2px 20px rgba(0, 0, 0, 0.2)",borderRadius:"4px"};applyStyles(t,match(isMobile(),(()=>match(e,(()=>r),(()=>a))),(()=>match(e,(()=>s),(()=>c)))))}function isApp(){return window.navigator.userAgent.includes("rktwebview=")}function saveLastSize(t,e){window.localStorage.setItem("pro-chat-size",JSON.stringify({width:t,height:e}))}function getLastSize(){return JSON.parse(window.localStorage.getItem("pro-chat-size"))??{width:null,height:null}}let resizeTimeout=null;function handleWrapperResize(t){const e=document.querySelector("#pro-chat-wrapper");e.getBoundingClientRect().width<=400||e.getBoundingClientRect().width>=window.visualViewport.width||(clearTimeout(resizeTimeout),resizeTimeout=setTimeout((()=>{const{width:e,height:n}=t[0].contentRect;saveLastSize(e,n)}),300))}function handleVisualViewportResize(t){applyStateStyles(document.querySelector("#pro-chat-wrapper"),isOpen)}function dynamicSizeOpenedDesktop(){const t=getLastSize(),e=window.innerWidth-.2*window.innerWidth,n=window.innerHeight-window.innerHeight*getHeightReduction(),i=Math.min(e,1e3),a=Math.min(n,1200);return{width:`${Math.min(Math.max(Math.min(e,t.width??500),450),i)}px`,height:`${Math.min(Math.max(Math.min(n,t.height??700),300),a)}px`,maxWidth:`${i}px`,maxHeight:`${a}px`,minWidth:"450px",minHeight:"300px"}}function applyStyles(t,e){for(const n in e)t.style[n]=e[n]}function match(t,e,n){return t?e():n()}function isMobile(){return window.innerWidth<800}function getHeightReduction(){return{"proclinic-es":.1,"dontalia-fr":.3}[parent.window.proshop.store]??.1}init();