!async function(){const e="xgijvqHEffw69bl23XvXG4U5N6EIVc86";let t="";const i=["www.facebook.com","www.youtube.com","www.instagram.com","www.x.com","www.tiktok.com","line.me"];let n,a="",c=[],d=[],o="",s=0,l=[],r=!1,g="",u=0,h="",p="",m="",w=!1,f=0,v=!1,y=!1,b=!1;await async function(){const e=document.querySelectorAll("script");for(const i of e)if(p=i.getAttribute("widget"),m=i.getAttribute("widget-type"),w=i.getAttribute("widget-preview"),p&&m){let e=i.getAttribute("src");e&&(t=e.split("/script/embed.min.js")[0]);break}if(!p||!m||!t)return;const i=`${t}/api/get-config`,c=await fetch(i,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:JSON.stringify({widget:p,widget_type:m,preview_mode:w||null})}),r=await c.json();d=r?.greeting_text||[],a=r?.color||"#FB8500",document.documentElement.style.setProperty("--main-theme-chat-widget-color",a),document.documentElement.style.setProperty("--main-theme-chat-widget-color-bg",a+"20"),s=r?.text_input_quota||250,l=r?.suggest_question,u=r?.pdf_limit_size||50,g=r?.tenant,y=r?.widget_name,r?.out_quota_text&&(h=r?.out_quota_text);w&&(f=r?.quota||0);if(r?.published){if(await async function(e){return new Promise(((t,i)=>{const n=document.createElement("link");n.rel="stylesheet",n.href=e,n.onload=()=>t(),n.onerror=()=>i(new Error(`Failed to load CSS file: ${e}`)),document.head.appendChild(n)}))}(`${t}/script/intellidoc-widget-style.css`),r?.svg_content){const e=document.createElement("script");document.head.appendChild(e),e.textContent=r?.svg_content,n=new WidgetMaterialSVG}await(x="https://cdn.jsdelivr.net/npm/crypto-js@4.1.1/crypto-js.js",new Promise(((e,t)=>{const i=document.createElement("script");i.src=x,i.onload=()=>e(),i.onerror=()=>t(new Error(`Failed to load script ${x}`)),document.head.appendChild(i)})));const e=document.createElement("div");e.id="chat-widget-container",document.body.appendChild(e),o=`\n \n \n \n `,e.innerHTML=`\n
\n \n
\n
\n
\n ${n.expandedIcon}\n \n
\n
\n \n
\n
\n
\n
\n
\n \n
\n `,r?.logo?(v=r?.logo,b=!0,document.getElementById("chat-widget-header-logo").innerHTML=``):(v=n.roverBtnIcon,document.getElementById("chat-widget-header-logo").innerHTML=n.roverBtnIcon),"q"==m&&(document.getElementById("widget-title-chat").textContent="Website AI Chatbot")}var x}();const x=document.getElementById("chat-input"),k=document.getElementById("chat-submit"),L=document.getElementById("chat-messages"),_=document.getElementById("chat-messages-content"),I=document.getElementById("chat-bubble"),E=document.getElementById("chat-popup"),$=document.getElementById("expand-popup"),T=document.getElementById("btn-icon"),A=document.getElementById("widget-list-bar-slide-up-title"),S=document.getElementById("widget-list-bar-slide-up"),B=document.getElementById("widget-list-bar-slide-up-content"),M=document.getElementById("chat-widget-select-link-slidebar");let q=!1,j=!0,C=!1,H=!1,N=[],O=!1,z=!1,P=[],F="",D=0,J=[],W=null,G=!1;function U(){Z()?($.innerHTML=n.closeBtnIcon,document.body.classList.add("widget-no-scrolling"),document.getElementById("chat-widget-container").style.bottom="0px",document.getElementById("chat-widget-container").style.right="0px"):(document.body.classList.remove("widget-no-scrolling"),$.innerHTML=H?n.collapedIcon:n.expandedIcon,K())}function K(){if(document.body.classList.remove("widget-no-scrolling"),Z())return document.getElementById("chat-widget-container").style.bottom="30px",void(document.getElementById("chat-widget-container").style.right="30px");document.getElementById("chat-widget-container").style.bottom="30px",document.getElementById("chat-widget-container").style.right="30px"}async function R(i){const a=document.createElement("div");if(O){let e=P.find((e=>e.url==O));if(e&&O!=z){const t=document.createElement("div");t.className="chat-widget chat-widget-flex justify-end chat-mb-3",t.innerHTML=`\n \n `,_.appendChild(t)}}a.className="chat-widget chat-widget-flex justify-end chat-mb-3",a.innerHTML=`\n \n ${i}\n
\n `,_.appendChild(a),L.scrollTop=L.scrollHeight,x.value="",X(),te(),Y(),await async function(i){C=!0,we();let n="";try{G=!0,de(!0);let a=await async function(){const e=await fetch(`${t}/api/check-package`,{headers:{Accept:"application/json","Content-Type":"application/json"},method:"POST",body:JSON.stringify({tenant:g,widget:p,preview_mode:w||null})});return await e.json()}();if(!a.within_limit)return G=!1,ne("เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง","Get package",a),!1;if(O){n="start check file";let a=P.findIndex((e=>e.url==O));if(!P[a].md5){let e=await async function(){const e=await fetch(`${t}/api/check-storage-file`,{headers:{Accept:"application/json","Content-Type":"application/json"},method:"POST",body:JSON.stringify({link:O,tenant:g,widget:p,preview_mode:w||null})});return await e.json()}();e.data&&(P[a].md5=e.data)}await fe(i,P[a].size,P[a].session);let c="",d="",o="default.pdf";const s=new URL(O).pathname,l=s.substring(s.lastIndexOf("/")+1);if(l&&(o=decodeURIComponent(l),o.includes(".pdf")||(o+=".pdf")),a>-1&&!P[a]?.md5){n="downloading file";const e=await fetch(O,{method:"GET"});d=await e.arrayBuffer(),n="hash file";const t=CryptoJS.lib.WordArray.create(d);c=CryptoJS.MD5(t).toString(),P[a].md5=c}else a>-1&&(c=P[a]?.md5);let r=`${t}/api/file-exist`;n=`check file with api : ${r}`;const u=await fetch(r,{headers:{Accept:"application/json","Content-Type":"application/json"},method:"POST",body:JSON.stringify({md5:c,tenant:g,widget:p,preview_mode:w||null})}),h=await u.json();if(h?.error)return void ne("เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง","",h);if(h.exists)await ce(i,c,P[a]);else{if(!d){const e=await fetch(O,{method:"GET"});d=await e.arrayBuffer()}const s=new Blob([d],{type:"application/pdf"}),l=new File([s],o,{type:"application/pdf"}),r=new FormData;if(r.append("file",l,o),r.append("upload_key",h.upload_key),new Date(h.expired) \n ${y}
\n `:``,t.innerHTML=`\n \n `):t.innerHTML=`\n \n
\n ${i}\n
\n
\n You\n
\n
\n `,_.appendChild(t)}function V(e,t=!1,i=!1){ie();const n=document.createElement("div");if(n.className="chat-widget chat-widget-flex chat-mb-3",t){const t=document.createElement("div");t.className="widget-chat-bg-grey chat-widget chat-widget-text-black border-radius-12 chat-py-3 chat-px-4 chat-widget-reply-slide-up",t.appendChild(e),n.appendChild(t)}else n.innerHTML=`\n \n ${e}\n
\n `;_.appendChild(n),setTimeout((()=>{document.querySelectorAll(".chat-widget-reply-slide-up").forEach(((e,t)=>{e.classList.remove("chat-widget-reply-slide-up")}))}),200),L.scrollTop=L.scrollHeight}function Z(){return!(document.documentElement.clientWidth>500)}function Y(e=!1){const t=document.querySelector("#bg-chat-submit circle");if(t){if(!e||C||""==x.value.trim())return t.setAttribute("fill",n.disabledBtnColor),k.classList.remove("cursor-pointer"),void k.classList.add("no-pointer");t.setAttribute("fill",a),k.classList.add("cursor-pointer"),k.classList.remove("no-pointer")}}function Q(e){if(!e)return"";let t=e.split("/");return t[t.length-1]}async function ee(e){try{const t=1e4,i=new AbortController,n=i.signal,a=setTimeout((()=>i.abort()),t),d=await fetch(e,{method:"HEAD",signal:n});if(clearTimeout(a),d.ok){const t=d.headers.get("content-type");if(t&&t.includes("pdf")){const t=d.headers.get("content-length");let i={url:e,size:!1,md5:""};if(t){const e=parseInt(t,10)/1048576;i.size=e.toFixed(2)}c.push(i)}}}catch(e){e.name}}function te(e="กำลังประมวลผล..."){C=!0;const t=document.createElement("div");t.id="display-typing-widget",t.className="chat-widget-flex chat-mb-3",t.innerHTML=`\n \n `,_.appendChild(t),L.scrollTop=L.scrollHeight}function ie(){C=!1;const e=document.getElementById("display-typing-widget");e&&e.remove(),q||(T.innerHTML=n.roverBtnIcon)}async function ne(e,i="",n=""){if("FILE_SIZE_AT_LIMIT"!=n?.error)if("MESSAGE_AT_LIMIT"!=n?.error)if("TENANT_MISSING"!=n?.error)if("PACKAGE_EXPIRED"!=n?.error)if("DOMAIN_NOT_ALLOW"!=n?.error&&"IP_NOT_ALLOW"!=n?.error){(O||"q"==m)&&me(l);try{let e={status:"error",detail:i,errorMessage:n,tenant:g};await fetch(`${t}/api/create-log`,{headers:{Accept:"application/json","Content-Type":"application/json"},method:"POST",body:JSON.stringify(e)})}catch(e){}ae(e)}else ae("ขออภัย ไม่สามารถใช้งานแชทได้");else ae("ขออภัย ไม่สามารถใช้งานแชทได้");else ae("ขออภัย ไม่สามารถใช้งานแชทได้");else ae("ข้อความของคุณเกินขีดจำกัดที่กำหนด");else ae("ไฟล์ของคุณเกินขีดจำกัดที่กำหนด")}function ae(e){C=!1,ie(),V(e),de(!1)}async function ce(e,i,n=!1){let a="";try{let c=function(){if(O){let e=P.find((e=>e.url==O));return e&&e?.session||""}return F||""}();const d=`${t}/api/chat-with-file`;a=`chat with AI : ${d}`;const o=new FormData;o.append("message",e),o.append("tenant",g),o.append("session_id",c),o.append("widget",p),o.append("widget_type",m),o.append("preview_mode",w||null),i&&(o.append("file_key",i),o.append("file_url",n.url),o.append("file_size",n.size));const s=await fetch(d,{headers:{Accept:"application/json"},method:"POST",body:o}),u=await s.json();if(C=!1,"OK"!=u.message)return ne("เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง",a,u),!1;!function(){if(w)return f-=1,void(document.getElementById("count-free-message").textContent=f);let e=localStorage.getItem("free_rover_message_quota")-1;localStorage.setItem("free_rover_message_quota",e),document.getElementById("count-free-message").textContent=e,re()}();let h=u.response,v=u.quick_reply?.map((e=>({text:e,from_setting:!1})));return G=!0,ge(u.session_id,O),V(h,!1,!0),O?(M.classList.contains("widget-chat-header-open-content")||M.classList.add("widget-chat-header-open-content"),function(){const e=document.querySelectorAll("input.widget-select-link-radio-slide");if(e.forEach((e=>{e.getAttribute("link")!=O?e.checked=!1:e.getAttribute("link")==O&&(e.checked=!0)})),r)return;function t(t){C||(e.forEach((e=>{e.getAttribute("link")!=t.target.getAttribute("link")?e.checked=!1:e.getAttribute("link")==t.target.getAttribute("link")&&t.target.getAttribute("link")==O?(O="",we(),e.checked=!1):t.target.getAttribute("link")!=O&&(O=t.target.getAttribute("link"),me(l),e.checked=!0)})),ue(O))}r=!0,e.forEach((e=>{e.removeEventListener("click",t),e.addEventListener("click",t)}))}(),z=O):z=null,W=null,l=l.filter((e=>e.from_setting)),(O||"q"==m)&&(v.length>0?me(v):me(l)),!0}catch(e){return ne("เกิดข้อผิดพลาด กรุณาลองใหม่อีกครั้ง",a,e.message),!1}}function de(e=!0,t=!1){if(null==W&&!t)return;let i=document.querySelectorAll(`input.widget-select-link-radio[index="${W}"]:not(.disabled)`);t&&(i=document.querySelectorAll("input.widget-select-link-radio:not(.disabled)")),i?.length>0&&i.forEach((t=>{t.disabled=e}))}function oe(e,t="error"){if(!e)return;const i=document.getElementById("chat-widget-message-bar");i.innerHTML=`\n `,"out-of-quota"!=t&&setTimeout((()=>{i.innerHTML=""}),3e3);document.querySelector(".chat-widget-close-message-bar").addEventListener("click",(()=>{i.innerHTML=""}),{once:!0})}function se(e,t=!1){if(w)return f;const i=new Date,n=`${i.getDate()}-${i.getMonth()+1}-${i.getFullYear()}`;return localStorage.getItem("free_rover_message_date")&&n==localStorage.getItem("free_rover_message_date")||(le(e),localStorage.setItem("free_rover_message_date",n)),localStorage.getItem("free_rover_message_quota")||0==localStorage.getItem("free_rover_message_quota")||(le(e),localStorage.setItem("free_rover_message_date",n)),localStorage.getItem("free_rover_message_quota")>e&&t&&(le(e),localStorage.setItem("free_rover_message_date",n)),localStorage.getItem("free_rover_message_quota")||0}function le(e){localStorage.setItem("free_rover_message_quota",e||0)}function re(){return se(localStorage.getItem("free_rover_message_quota"))<=0&&!w?(oe(h,"out-of-quota"),!1):!(w&&f<=0)||(oe(h,"out-of-quota"),!1)}function ge(e,t=""){if(t){let i=P.findIndex((e=>e.url==t));i>=0&&(P[i].session=e)}else F=e}function ue(e){document.querySelectorAll("a").forEach((e=>{e?.classList?.remove("widget-chat-highlight-element")}));const t=document.querySelector(`[href="${e}"]:not(.chat-widget)`);t&&""!=e&&(t.scrollIntoView({behavior:"smooth",block:"center"}),t.classList.add("widget-chat-highlight-element"))}function he(e,t,i){const n=document.querySelector(`.widget-bottom-select-file-text-wrapper[pagination-index="${e}"] [action="chat-prev-pagination"]`),a=document.querySelector(`.widget-bottom-select-file-text-wrapper[pagination-index="${e}"] [action="chat-next-pagination"]`);t==i?a?.classList?.remove("chat-widget-pagination-active"):a?.classList?.add("chat-widget-pagination-active"),1==t?n?.classList?.remove("chat-widget-pagination-active"):n?.classList?.add("chat-widget-pagination-active")}function pe(){const e=document.querySelectorAll("input.widget-select-link-radio");e.forEach((t=>{let i=!1;t.addEventListener("click",(function(t){if(e.forEach((e=>{e.getAttribute("link")!=t.target.getAttribute("link")?e.checked=!1:e.getAttribute("link")==t.target.getAttribute("link")&&t.target.getAttribute("link")==O?(O="",e.checked=!1,we()):t.target.getAttribute("link")&&t.target.getAttribute("link")!=O?(O=t.target.getAttribute("link"),me(l),e.checked=!0,i=!0):t.target.getAttribute("link")||(we(),e.checked=!1,O="")})),i)J.includes(t.target.getAttribute("index"))||J.push(t.target.getAttribute("index")),W=t.target.getAttribute("index");else{if(J.includes(t.target.getAttribute("index"))){let e=J.findIndex((e=>e==t.target.getAttribute("index")));e>-1&&J.splice(e,1)}W=null}ue(O)}))}))}function me(e){if(re()<=0)return;const t=document.getElementById("chat-widget-prompter");if(!t)return;t.classList.add("open"),t.innerHTML="";for(const i of e)t.innerHTML+=`${i.text}
`;document.querySelectorAll(".chat-widget-slide-up-prompt").forEach((e=>{e.addEventListener("click",(function(e){X("User"),R(e.target.textContent),B.classList.contains("widget-chat-slide-up-content")&&(B.classList.toggle("widget-chat-slide-up-content"),S.innerHTML=n.chevronUp)}))}));const i=document.getElementById("chat-messages");i.scrollTop=i.scrollHeight}function we(){const e=document.getElementById("chat-widget-prompter");if(!e)return;e.classList.remove("open");document.querySelectorAll(".chat-widget-slide-up-prompt").forEach(((e,t)=>{e.classList.remove("chat-widget-slide-up-prompt"),e.classList.add("chat-widget-slide-out-prompt")})),setTimeout((()=>{e.innerHTML=""}),200)}async function fe(e,i=null,n=null){const{latitude:a,longitude:c}=await new Promise(((e,t)=>{"geolocation"in navigator?navigator.geolocation.getCurrentPosition((t=>{const{latitude:i,longitude:n}=t.coords;e({latitude:i,longitude:n})}),(t=>{e({latitude:null,longitude:null})})):e({latitude:null,longitude:null})})),d=await fetch(`${t}/api/save-history`,{headers:{Accept:"application/json","Content-Type":"application/json"},method:"POST",body:JSON.stringify({tenant:g,file_url:O||null,file_size:i||null,session_id:O?n:F,file_name:e,widget_type:m,latitude:a,longitude:c,widget:p,preview_mode:w||null})});let o=await d.json();o.session_id&&ge(o.session_id,O)}$?.addEventListener("click",(function(){return Z()?($.innerHTML=n.closeBtnIcon,I.click(),void K()):H?(H=!1,$.innerHTML=n.expandedIcon,E.classList.add("float"),void E.classList.remove("sidebar")):(H=!0,$.innerHTML=n.collapedIcon,E.classList.add("sidebar"),void E.classList.remove("float"))})),k?.addEventListener("click",(function(){const e=x.value.trim();e&&!C&&re()&&(L.scrollTop=L.scrollHeight,x.value="",k.classList.add("chat-input-animation"),setTimeout((()=>{k.classList.remove("chat-input-animation")}),550),X("User"),R(e))})),x?.addEventListener("keyup",(function(e){""!=x.value&&re()?Y(!0):Y(),"Enter"===e.key&&k.click()})),window.addEventListener("resize",(function(e){E.classList.contains("hidden")?K():U()}),!0),I?.addEventListener("click",(async function(){!function(){re();const e=document.getElementById("chat-popup");e.classList.remove("hidden"),q=!q,q?(e.classList.remove("hide-widget-animated"),"q"==m&&me(l),T.classList.add("widget-icon-slide-up-from-origin"),setTimeout((()=>{T.innerHTML=n.closeBtnIcon,T.classList.add("widget-icon-slide-up-from-outer"),setTimeout((()=>{T.classList.remove("widget-icon-slide-up-from-origin"),T.classList.remove("widget-icon-slide-up-from-outer")}),400)}),200)):(e.classList.add("hide-widget-animated"),C?T.innerHTML=`\n `:(T.classList.add("widget-icon-slide-down-from-origin"),setTimeout((()=>{T.innerHTML=n.roverBtnIcon,T.classList.add("widget-icon-slide-down-from-outer"),setTimeout((()=>{T.classList.remove("widget-icon-slide-down-from-origin"),T.classList.remove("widget-icon-slide-down-from-outer")}),400)}),200)));if(Y(),!e.classList.contains("hidden")&&j&&(document.getElementById("chat-input").focus(),document.getElementById("chat-input").setAttribute("maxlength",s),"d"==m&&X("AI"),d.length>0)){"q"==m&&X("AI"),te("กำลังพิมพ์...");for(const e of d)V(e)}}(),U(),j&&(C=!0,j=!1,await async function(){if("q"==m)return;te("กำลังสแกนไฟล์...");const e=document.getElementsByTagName("a");c=[];for(const t of Array.from(e)){const e=i.some((e=>t.href.toLowerCase().includes(e.toLowerCase())));t.className?.includes("widget-no-scan")||N.includes(t.href)||e||(N.push(t.href),await ee(t.href))}if(c?.length>0){D++,P=[...P,...c],function(e){let t="",i=[],a=4,c=Math.ceil(e.length/a);for(let t=1;t<=c;t++){let c="";const d=t*a;for(let i=(t-1)*a;i\n \n \n ${parseFloat(t?.size)<=u?` `:' '}\n
\n `)}i.push(c)}t+=`\n
\n \n
เลือกไฟล์ที่คุณต้องการให้ AI วิเคราะห์หรือดำเนินการ
\n `,e?.length>a&&(t+=``);t+="
",textStack=`ไฟล์เอกสารที่พบในหน้านี้ (${e.length}) ${t}
`;const d=document.createElement("div");d.className="chat-widget chat-doc-list-wrapper",d.innerHTML=textStack,V(d,!0);const o=document.getElementById(`chat-widget-link-card-${D}`),s=document.querySelector(`.widget-bottom-select-file-text-wrapper[pagination-index="${D}"] span[action="chat-next-pagination"]>svg `),l=document.querySelector(`.widget-bottom-select-file-text-wrapper[pagination-index="${D}"] span[action="chat-prev-pagination"]>svg `);let r=1;o.innerHTML=i[r-1],pe(),s?.addEventListener("click",(function(e){if(r++,!i[r-1])return void r--;if(o.innerHTML=i[r-1],O){let e=document.querySelector(`.widget-select-link-radio[link="${O}"]`);e&&(e.checked=!0)}const t=e.target.closest("[pagination-index]");W=t.getAttribute("pagination-index"),de(G),pe(),he(D,r,i?.length)})),l?.addEventListener("click",(function(e){if(r--,!i[r-1])return void r++;if(o.innerHTML=i[r-1],O){let e=document.querySelector(`.widget-select-link-radio[link="${O}"]`);e&&(e.checked=!0)}const t=e.target.closest("[pagination-index]");W=t.getAttribute("pagination-index"),de(G),pe(),he(D,r,i?.length)}))}(c);let e='เลือกไฟล์ที่คุณต้องการให้ AI วิเคราะห์หรือดำเนินการ
';for(const t of c)e+=``;B.innerHTML=e,A.textContent=`ไฟล์เอกสารที่พบในหน้านี้ (${P.length})`,S.addEventListener("click",(function(){C||(B.classList.toggle("widget-chat-slide-up-content"),B.classList.contains("widget-chat-slide-up-content")?S.innerHTML=n.chevronDown:S.innerHTML=n.chevronUp)})),te("กำลังพิมพ์..."),setTimeout((()=>{V("ป้อนคำถามที่ต้องการได้เลยนะครับ")}),400)}else M.classList.remove("widget-chat-header-open-content"),O="",V("ไม่พบไฟล์เอกสาร โปรดไปยังหน้าเว็บไซต์อื่นเพื่อใช้งานบริการวิเคราะห์ไฟล์
")}(),C=!1)})),setTimeout((()=>{!Z()&&I&&I.click()}),300)}();