(()=>{(function(){"use strict";let o=document.currentScript;if(!o){console.error("AIPKit Embed: Could not find the bootstrap script tag.");return}let r=o.dataset.botId,s=o.dataset.wpSite;if(!r||!s){console.error("AIPKit Embed: Missing 'data-bot-id' or 'data-wp-site' attribute on the script tag.");return}let f=`${s}/wp-json/aipkit/v1/chatbots/${r}/embed-config`,c=`aipkit-chatbot-container-${r}`;function u(t){return new Promise((e,i)=>{if(!t||document.querySelector(`link[href="${t}"]`))return e();let n=document.createElement("link");n.rel="stylesheet",n.href=t,n.onload=()=>e(),n.onerror=()=>i(new Error(`Failed to load CSS: ${t}`)),document.head.appendChild(n)})}function a(t){return new Promise((e,i)=>{if(!t||document.querySelector(`script[src="${t}"]`))return e();let n=document.createElement("script");n.src=t,n.async=!1,n.onload=()=>e(),n.onerror=()=>i(new Error(`Failed to load JS: ${t}`)),document.body.appendChild(n)})}async function m(){let t=await fetch(f);if(!t.ok){let e=await t.json().catch(()=>({message:"Unknown API error."}));throw new Error(`Failed to fetch config: ${e.message||t.statusText}`)}return t.json()}async function h(){let t=document.getElementById(c);if(!t){console.error(`AIPKit Embed: Target container with ID "${c}" not found.`);return}t.innerHTML='<p style="text-align:center;font-family:sans-serif;color:#888;padding:20px;">Loading Chatbot...</p>';try{let p=function(){if(typeof window.aipkit_initializeChatInstance=="function")window.aipkit_initializeChatInstance(d);else if(l<w)l++,setTimeout(p,g);else throw new Error("Main chat initializer function (aipkit_initializeChatInstance) did not become available.")},e=await m(),i=e.config,n=e.html;await u(i.assetUrls.css),await a(i.assetUrls.markdownit),i.assetUrls.jspdf&&await a(i.assetUrls.jspdf),await a(i.assetUrls.mainJs),t.innerHTML=n;let d=t.querySelector(".aipkit_chat_container, .aipkit_popup_wrapper");if(!d)throw new Error("Chatbot container element not found in the HTML received from the server.");let l=0,w=20,g=100;p()}catch(e){console.error("AIPKit Embed: Initialization failed.",e),t.innerHTML=`<p style="text-align:center;font-family:sans-serif;color:#d9534f;padding:20px;">Error: Could not load chatbot. ${e.message}</p>`}}h()})();})();
