import{t,p as i,H as s,d as a,E as o,h as e}from"./p-ZGVz1_9H.js";import{d as r,t as h}from"./p-CrfXJRIo.js";import{r as n}from"./p-g36mIk7k.js";import{c}from"./p-DXBxSLgN.js";const d=i(class extends s{constructor(t){super(),!1!==t&&this.__registerHost(),this.__attachShadow(),this.formSubmitted=a(this,"formSubmitted",7),this.errored=a(this,"errored",7),this.loadTimer=null,this.loadVersion=0,this.surveyCDNPath="https://cdn.ai12z.net/pkg/surveyjs",this.dataKey=o.DATA_KEY||"",this.dataMode=o.DATA_MODE||"prod",this.keyName="",this.isLoading=!1,this.error="",this.isReady=!1,this.honeypotValue="",this.styles=["https://cdn.ai12z.net/assets/bot/v2/tailwind.min.css","https://cdn.ai12z.net/pkg/surveyjs/survey-core@2.3.4/survey-core.min.css"],this.surveyDomId=`ai12z-form-survey-${Math.random().toString(36).slice(2,10)}`}async componentDidLoad(){await this.initializeAssetLoader(),this.isReady=!0,await this.loadDynamicForm()}async initializeAssetLoader(){try{this.assetLoader=c({shadowRoot:this.el.shadowRoot,surveyCDNPath:this.surveyCDNPath,customStyles:this.styles}),await this.assetLoader.loadBotAssets(),await this.assetLoader.loadSurveyDependencies()}catch(t){console.error("❌ Failed to initialize asset loader:",t)}}disconnectedCallback(){this.loadVersion++,this.loadTimer&&clearTimeout(this.loadTimer)}async watchLoadProps(){this.isReady&&(this.loadTimer&&clearTimeout(this.loadTimer),this.loadTimer=setTimeout((()=>{this.loadDynamicForm()}),150))}async loadDynamicForm(t){var i;const s=null===(i=null!=t?t:this.keyName)||void 0===i?void 0:i.trim(),a=++this.loadVersion;if(this.dataKey){this.error="",this.isLoading=!0;try{const t=await this.fetchResponse(s);if(a!==this.loadVersion)return;if(!t.success){const t=s?` for keyName "${s}"`:"";throw new Error(`No form control found${t}.`)}await n({chat:{keyName:t.form.keyName,controlData:{form_model:t.form.model},theme:t.form.theme},surveyDomId:this.surveyDomId,shadowRoot:this.el.shadowRoot,config:{dataKey:this.dataKey,dataMode:this.dataMode,onSubmitError:t=>{this.setError("Form submission blocked by validation"!==t?t||"Failed to submit form":"Submission blocked due to suspicious activity.")},formSubmitRenderCallback:t=>{var i,s,a;this.error="";const o=null===(i=this.el.shadowRoot)||void 0===i?void 0:i.querySelector(`#${(null==t?void 0:t.domId)||this.surveyDomId}`);o&&(o.innerHTML=t.completedHtml),this.formSubmitted.emit(null!==(a=null===(s=t.sender)||void 0===s?void 0:s.data)&&void 0!==a?a:t.results)}}})}catch(t){this.setError((null==t?void 0:t.message)||"Failed to load form")}finally{this.isLoading=!1}}else this.setError('Public Key is required. Please provide using attribute "data-key".')}async fetchResponse(t){return await h(this.dataKey,this.dataMode,null!=t?t:this.keyName)}setError(t){this.error=t,this.errored.emit(t)}render(){return e("div",{key:"b31dc3055b20ff63de53571749e4394acfce4441",class:"ai12z-form-wrapper"},this.error&&!this.isLoading&&e("div",{key:"0a09604f199fa41a6a8a18755782489cc4fa8f6b",class:"ai12z-form-error"},this.error),e("div",{key:"970ee7d88aa2a7971c9841a8d51392a1c703d2ce",id:this.surveyDomId,class:"ai12z-form-container"}))}get el(){return this}static get watchers(){return{keyName:[{watchLoadProps:0}]}}static get style(){return".ai12z-form-wrapper{width:100%}.ai12z-form-status,.ai12z-form-error{font-size:14px;margin-bottom:8px}.ai12z-form-error{color:#b91c1c}.ai12z-form-container{width:100%}.ai12z-form-honeypot{position:absolute;left:-10000px;top:auto;width:1px;height:1px;overflow:hidden}@keyframes ai12z-form-spin{to{transform:rotate(360deg)}}@keyframes ai12z-form-bounce{0%,80%,100%{opacity:0.35;transform:translateY(0)}40%{opacity:1;transform:translateY(-3px)}}"}},[1,"ai12z-form",{dataKey:[513,"data-key"],dataMode:[513,"data-mode"],keyName:[513,"key-name"],isLoading:[32],error:[32],isReady:[32],honeypotValue:[32],loadDynamicForm:[64]},void 0,{keyName:[{watchLoadProps:0}]}]);function m(){"undefined"!=typeof customElements&&["ai12z-form","item-list"].forEach((i=>{switch(i){case"ai12z-form":customElements.get(t(i))||customElements.define(t(i),d);break;case"item-list":customElements.get(t(i))||r()}}))}m();export{d as A,m as d}