!function(){var e={position:"right",themeColor:"#0f6cbd",width:"380px"},t=window.__CS_AGENT_WIDGET_STATE__;function n(t){var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);for(n in t=t||{})Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n]);return r.baseUrl=String(r.baseUrl||window.location.origin).replace(/\/$/,""),r.apiBaseUrl?r.apiBaseUrl=String(r.apiBaseUrl).replace(/\/$/,""):delete r.apiBaseUrl,r.channelId=String(r.channelId||""),r.externalId&&(r.externalId=String(r.externalId)),"function"!=typeof r.getUserToken&&delete r.getUserToken,r}function r(e){var t=document.currentScript;return t&&t.src?t.src.replace(/\/sdk\/cs-ai-agent-sdk\.min\.js(?:\?.*)?$/,""):String(e.widgetBaseUrl||e.baseUrl||window.location.origin).replace(/\/$/,"")}function i(e,t){var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&"getUserToken"!==n&&(r[n]=e[n]);return t&&(r.userToken=t),r}function a(){return function(){var e=t.config||{};if("function"!=typeof e.getUserToken)return Promise.resolve("");try{return Promise.resolve(e.getUserToken()).then(function(e){return String(e||"").trim()})}catch(e){return Promise.reject(e)}}().then(function(e){return t.frameUrl=function(e,t){var n=r(e),i=new URL(n+"/kefu/chat/");return i.searchParams.set("channelId",e.channelId),i.searchParams.set("baseUrl",e.baseUrl),e.apiBaseUrl&&i.searchParams.set("apiBaseUrl",e.apiBaseUrl),e.externalId&&i.searchParams.set("externalId",e.externalId),e.externalName&&i.searchParams.set("externalName",e.externalName),t&&i.searchParams.set("userToken",t),i}(t.config,e),t.frameConfig=i(t.config,e),t.frameUrl})}function o(){t.frameHideTimer&&(window.clearTimeout(t.frameHideTimer),t.frameHideTimer=null),t.frameDestroyTimer&&(window.clearTimeout(t.frameDestroyTimer),t.frameDestroyTimer=null)}function s(){var e=t.frame,n=t.config;if(e&&n){if(e.style.position="fixed",e.style.border="0",e.style.overflow="hidden",e.style.background="#fff",e.style.zIndex="2147483000",e.style.boxShadow="0 28px 80px rgba(15, 35, 65, 0.28)",e.style.willChange="top,right,bottom,left,width,height,opacity,transform,border-radius",e.style.transition="top 260ms cubic-bezier(0.22, 1, 0.36, 1), right 260ms cubic-bezier(0.22, 1, 0.36, 1), bottom 260ms cubic-bezier(0.22, 1, 0.36, 1), left 260ms cubic-bezier(0.22, 1, 0.36, 1), width 260ms cubic-bezier(0.22, 1, 0.36, 1), height 260ms cubic-bezier(0.22, 1, 0.36, 1), opacity 220ms ease, transform 260ms cubic-bezier(0.22, 1, 0.36, 1), border-radius 260ms cubic-bezier(0.22, 1, 0.36, 1), box-shadow 260ms ease",e.style.transformOrigin="left"===n.position?"left bottom":"right bottom",t.isMaximized)return e.style.top="20px",e.style.right="20px",e.style.bottom="20px",e.style.left="20px",e.style.width="calc(100vw - 40px)",e.style.maxWidth="none",e.style.height="calc(100vh - 40px)",void(e.style.borderRadius="24px");e.style.top="",e.style.bottom="112px",e.style.right="left"===n.position?"":"24px",e.style.left="left"===n.position?"24px":"",e.style.width=n.width||"380px",e.style.maxWidth="calc(100vw - 24px)",e.style.height="min(760px, calc(100vh - 136px))",e.style.borderRadius="28px"}}function l(e){if(t.frame&&t.frame.contentWindow&&t.frameUrl)try{t.frame.contentWindow.postMessage(e,t.frameUrl.origin)}catch(e){console.error("[cs-agent-widget] postMessage failed",e)}}function c(){t.frame&&t.frameLoaded&&t.frameReady&&(t.initSent||(t.initSent=!0,l({type:"cs-agent:init",payload:t.frameConfig||i(t.config,"")})),l({type:t.isOpen?"cs-agent:open":"cs-agent:minimize"}),l({type:"cs-agent:maximized",payload:{isMaximized:t.isMaximized}}))}function f(){var e=t.frame;if(e){if(o(),s(),e.style.display="block",t.isOpen)return e.style.visibility="visible",e.style.pointerEvents="auto",t.frameHideTimer=window.setTimeout(function(){t.frame&&(t.frame.style.opacity="1",t.frame.style.transform="translate3d(0, 0, 0) scale(1)")},16),void c();e.style.pointerEvents="none",e.style.opacity="0",e.style.transform=t.isMaximized?"translate3d(0, 10px, 0) scale(0.985)":"translate3d(0, 16px, 0) scale(0.96)",t.frameHideTimer=window.setTimeout(function(){t.frame&&!t.isOpen&&(t.frame.style.visibility="hidden")},t.animationDuration),c()}}function d(e){var i=e||window.CSAgentConfig||{};t.config=n(i);var a=r(t.config);i.baseUrl||(t.config.baseUrl=a),t.config.channelId?(t.configLoading=!0,function(e){var t=String(e.apiBaseUrl||e.baseUrl||"").replace(/\/$/,"");if(!t||!e.channelId||"function"!=typeof fetch)return Promise.resolve(e);var n=t+"/api/channel/config?channelId="+encodeURIComponent(e.channelId);return fetch(n,{method:"GET",cache:"no-store",headers:{"X-Channel-Id":e.channelId}}).then(function(e){return e.json()}).then(function(t){return t&&!1!==t.success?function(e,t){if(!t)return e;var n,r={};for(n in e)Object.prototype.hasOwnProperty.call(e,n)&&(r[n]=e[n]);for(var i=["title","subtitle","themeColor","position","width"],a=0;a