class t{constructor(t){this.loadedScripts=new Set,this.loadedStyles=new Set,this.scriptPromises=new Map,this.stylePromises=new Map,this.config=Object.assign({cdnPath:"https://cdn.ai12z.net/assets/bot",surveyCDNPath:"https://cdn.ai12z.net/pkg/surveyjs",customStyles:["https://cdn.jsdelivr.net/npm/@highlightjs/cdn-assets@11.11.1/styles/default.min.css"],customScripts:[]},t)}loadScript(t){const s="string"==typeof t?{src:t}:t,{src:i}=s;if(this.scriptPromises.has(i))return this.scriptPromises.get(i);if(this.loadedScripts.has(i)||this.isScriptLoaded(i))return Promise.resolve();const e=new Promise(((t,e)=>{var r,o,n;const a=document.createElement("script");a.src=i,a.async=null!==(r=s.async)&&void 0!==r&&r,a.defer=null!==(o=s.defer)&&void 0!==o&&o,a.type=null!==(n=s.type)&&void 0!==n?n:"text/javascript",a.onload=()=>{this.loadedScripts.add(i),this.scriptPromises.delete(i),s.onLoad&&s.onLoad(),t()},a.onerror=t=>{this.scriptPromises.delete(i);const r=new Error(`Failed to load script: ${i}`);s.onError&&s.onError(r),console.error(`❌ Script failed to load: ${i}`,t),e(r)},this.config.shadowRoot?this.config.shadowRoot.appendChild(a):document.head.appendChild(a)}));return this.scriptPromises.set(i,e),e}loadStyle(t,s=!1){const i="string"==typeof t?{href:t}:t,{href:e}=i;if(this.stylePromises.has(e))return this.stylePromises.get(e);if(this.loadedStyles.has(e)||this.isStyleLoaded(e))return Promise.resolve();const r=new Promise(((t,r)=>{var o;const n=document.createElement("link");n.rel="stylesheet",n.href=e,n.media=null!==(o=i.media)&&void 0!==o?o:"all",n.onload=()=>{this.loadedStyles.add(e),this.stylePromises.delete(e),i.onLoad&&i.onLoad(),t()},n.onerror=t=>{this.stylePromises.delete(e);const s=new Error(`Failed to load style: ${e}`);i.onError&&i.onError(s),console.error(`❌ Style failed to load: ${e}`,t),r(s)},this.config.shadowRoot&&!s?this.config.shadowRoot.appendChild(n):document.head.appendChild(n)}));return this.stylePromises.set(e,r),r}async loadScriptsSequentially(t){for(const s of t)try{await this.loadScript(s)}catch(t){throw console.error("Failed to load script in sequence:",t),t}}async loadScriptsParallel(t){try{await Promise.all(t.map((t=>this.loadScript(t))))}catch(t){throw console.error("Failed to load scripts in parallel:",t),t}}async loadStyles(t,s=!1){try{await Promise.all(t.map((t=>this.loadStyle(t,s))))}catch(t){throw console.error("Failed to load styles:",t),t}}async loadSurveyDependencies(){const t=[`${this.config.surveyCDNPath}/survey-core@2.3.4/survey.core.min.js`,`${this.config.surveyCDNPath}/survey-core@2.3.4/survey.i18n.min.js`,`${this.config.surveyCDNPath}/survey-core@2.3.4/themes/index.min.js`,`${this.config.surveyCDNPath}/survey-js-ui@2.3.4/survey-js-ui.min.js`],s=[`${this.config.surveyCDNPath}/survey-core@2.3.4/survey-core.min.css`];await this.loadStyles(s),await this.loadScriptsSequentially(t)}async loadBotAssets(){try{await Promise.all([this.loadStyles(this.config.customStyles||[])])}catch(t){throw console.error("❌ Failed to load bot assets:",t),t}}async loadBotTheme(t){var s,i;const e=[],r=[],o=[];t.theme?"custom"!==t.theme?e.push(`${this.config.cdnPath}/v2/themes/theme-${t.theme}.css`):t.customTheme&&e.push(t.customTheme):e.push(`${this.config.cdnPath}/v2/themes/theme-default.css`),(null===(s=t.welcome)||void 0===s?void 0:s.styles)&&r.push(t.welcome.styles),t.customCSS&&r.push(t.customCSS),(null===(i=t.welcome)||void 0===i?void 0:i.scripts)&&o.push({src:t.welcome.scripts,type:"text/javascript"}),t.customJS&&o.push({src:t.customJS,type:"text/javascript"}),await Promise.all([e.length>0?this.loadStyles(e,!0):Promise.resolve(),r.length>0?this.loadStyles(r,!1):Promise.resolve(),o.length>0?this.loadScriptsParallel(o):Promise.resolve()])}isScriptLoaded(t){var s;return!(!document.querySelector(`script[src="${t}"]`)&&!(null===(s=this.config.shadowRoot)||void 0===s?void 0:s.querySelector(`script[src="${t}"]`)))}isStyleLoaded(t){var s;return!(!document.querySelector(`link[href="${t}"]`)&&!(null===(s=this.config.shadowRoot)||void 0===s?void 0:s.querySelector(`link[href="${t}"]`)))}getLoadingStatus(){return{loadedScripts:Array.from(this.loadedScripts),loadedStyles:Array.from(this.loadedStyles),pendingScripts:Array.from(this.scriptPromises.keys()),pendingStyles:Array.from(this.stylePromises.keys())}}reset(){this.loadedScripts.clear(),this.loadedStyles.clear(),this.scriptPromises.clear(),this.stylePromises.clear()}}function s(s){return new t(s)}export{s as c}