import{s as t}from"./p-COqFEHJ7.js";import{u as n,h as o,y as a,c as e,a as i}from"./p-CrfXJRIo.js";async function s(i){var s,u;const{data:d,currentDomId:v,setMessages:f,setSurveyDomId:m,config:w,setButtons:y,inputType:p}=i;try{let i=(new t.Converter).makeHtml(d.answer);i=n(i,(null===(s=w.controlSettings)||void 0===s?void 0:s.linkTarget)||"_blank","bot"===p?"ai12z-link":"ai12z-cta-link",w.utmConfig||(null===(u=w.controlSettings)||void 0===u?void 0:u.utm),"answer");const $=o(i);i=$.body,y($.buttons);const b=i.match(/\[control[^\]]*\]/g)||[];i.indexOf("[control")>-1?await async function(t,n,o,i,s,u,d,v){0===s.length?await r(t,o,u,n):s.length>0&&await async function(t,n,o,i,s,r,u){var d,v;const f=a(t);if(f&&0!==f.parts.length)for(let t=0;t<f.parts.length;t++){const a=f.parts[t];if("text"===a.type&&a.content.trim().length>0)if(0===t)null===(d=s.updateConversations)||void 0===d||d.call(s,a.content,n),null===(v=s.updateChatMessage)||void 0===v||v.call(s,a.content,n);else if("bot"===u){const t=e(s.conversationId);t.text=a.content,t.isLoading=!1,i(t)}else"cta"===u&&i(s.getUpdatedChat(a.content,n));if("control"===a.type&&a.controlType)switch(a.controlType){case"form":await l(n,s,r,u,i,a,o,t);break;case"carousel":case"list":case"template":await c(n,s,a,i,u);break;default:console.warn(`Unknown control type: ${a.controlType}`);continue}t<f.parts.length-1&&await new Promise((t=>setTimeout(t,200)))}else console.warn("No parts found in splitContentMultiple")}(t,n,o,i,u,d,v)}(i,d,v,f,b,w,m,p):await r(i,v,w,d)}catch(t){throw console.error("Error in handleEndResponse:",t),t}}async function l(t,n,o,a,s,l,c,r){var d,v,f,m;const w=`surveyElement-${i()}`;let y;o&&o(w),0!==r&&("bot"===a?(y=e(n.conversationId),y.surveyDomId=w,s(y)):"cta"===a&&(y=n.getUpdatedChat("",t),y.surveyDomId=w,s(y)),y.controlType="form",y.itemComponent=t,await u(`#${y.id}`,n.element)),t.controlType="form",t.surveyDomId=w;const p=`<div id=${w} style="width:${"v1"===(null===(d=n.controlSettings)||void 0===d?void 0:d.botVersion)?"330px":"100%"}" class="survey-container"></div>`;null===(v=n.updateConversations)||void 0===v||v.call(n,p,t),null===(f=n.updateChatMessage)||void 0===f||f.call(n,p,t);const $=n.element.shadowRoot.querySelector(`#${0===r?c:y.id}`);if($&&($.style.width="100%",$.parentElement.style.width="100%",$.innerHTML=p,l.agentDataId&&(null===(m=t.controls)||void 0===m?void 0:m.length)>0)){const n=t.controls.filter((t=>t.agent_data_id===l.agentDataId));t.controlData=n.length>0?n[0]:null}setTimeout((()=>{n.renderSurvey(t)}),100)}async function c(t,n,o,a,i){var s,l,c;let r;if("bot"===i?(r=e(n.conversationId),r.itemComponent=t,r.controlType=o.controlType,a(r)):"cta"===i&&(r=n.getUpdatedChat("",t),r.itemComponent=t,r.controlType=o.controlType,a(r)),await u(`#${r.id}`,n.element),o.agentDataId&&(null===(s=t.controls)||void 0===s?void 0:s.length)>0){const n=t.controls.filter((t=>t.agent_data_id===o.agentDataId));t.controlData=n.length>0?n[0].custom_data:null,t.messageId=r.id,t.controlType=o.controlType}const d=t.controlData?Object.assign(Object.assign({},t),{messageId:r.id}):t;(null===(c=null===(l=d.controlData)||void 0===l?void 0:l.data)||void 0===c?void 0:c.source)&&(d.controlData.data.source.customCss=d.controlData.styles||"",d.controlData.data.source.customJs=d.controlData.scripts||""),requestAnimationFrame((async()=>{setTimeout((async()=>{"template"===o.controlType?await n.processTemplateData(r,d):await n.processChatData(r,d),await n.hideSpinner(r)}),100)}))}async function r(t,n,o,a){var e,i;const s=o.element.shadowRoot.querySelector(`#${n}`);s&&(s.innerHTML=t,o.checkHighlightcode(n),null===(e=o.updateConversations)||void 0===e||e.call(o,t,null!=a?a:{}),null===(i=o.updateChatMessage)||void 0===i||i.call(o,t,{}),o.addEventListeners(`#${n}`))}function u(t,n,o=1e4){return new Promise(((a,e)=>{const i=Date.now(),s=()=>{var l;const c=null===(l=n.shadowRoot)||void 0===l?void 0:l.querySelector(t);c?a(c):Date.now()-i>o?e(new Error(`AI12Z: waitForElementRender timed out after ${o}ms waiting for "${t}"`)):requestAnimationFrame(s)};s()}))}export{s as h,u as w}