import{ef as e,u as t,bD as n,h7 as r,X as o,h8 as s,_ as a,h9 as i,r as c,ha as u,hb as d,U as l,l as p,gk as h,L as m,gB as g,cA as I,gi as f,g_ as v,b3 as S}from"./0.CA-0OlvF.chunk.js";import{c as C,n as k}from"./3.CJvEkDRx.chunk.js";import{f as y,M as _,i as w}from"./7.DaJJFJ3Z.chunk.js";import{b as T}from"./9.DNzTqRPk.chunk.js";import"./2.pu7iWzia.chunk.js";import{r as F}from"./1.CYmTgLyG.chunk.js";function M(e,t){let n,r=Date.now()-2*e;const o=function(){return r=Date.now(),t(...arguments)},s=()=>clearTimeout(n),a=function(){const t=Date.now();t-r>=e&&(r=Date.now()),s();for(var a=arguments.length,i=new Array(a),c=0;cP.connectionStateFetcherContext,setConnectionStateFetcherContext:e=>{P.connectionStateFetcherContext=e},updateConnectionStateFetcherContext:e=>{P.connectionStateFetcherContext={...P.connectionStateFetcherContext,...e}},clearConnectionStateFetcherContext:()=>{P.connectionStateFetcherContext={}},setPendingAsyncFlow:(e,t)=>{P.pendingAsyncFlows[e]=t},getIsPendingAsyncFlow:e=>P.pendingAsyncFlows[e],getHistoryIterator:()=>P.historyIterator,setHistoryIterator:e=>{P.historyIterator=e},getLatestSeen:()=>P.latestSeen,updateLatestSeen:e=>{P.latestSeen=Math.max(P.latestSeen,e)},getEventQueue:()=>P.eventQueue,addToEventQueue:e=>{P.eventQueue.push(e)},clearEventQueue:()=>{const e=P.eventQueue;return P.eventQueue=[],e},getUploadInProgress:e=>P.uploadsInProgress[e],setUploadInProgress:(e,t)=>{P.uploadsInProgress[e]=t},removeUploadInProgress:e=>{delete P.uploadsInProgress[e]},getReportConnectionUnsuccessfulTimerId:()=>P.reportConnectionUnsuccessfulTimerId,setReportConnectionUnsuccessfulTimerId:e=>{P.reportConnectionUnsuccessfulTimerId=e},getMinimizedTimeouts:()=>P.minimizedTimeouts,setMinimizedTimeout:(e,t)=>{P.minimizedTimeouts[e]=t},setLocalizationPromise:e=>{P.localizationPromise=e},getLocalizationPromise:()=>P.localizationPromise},x=(e,t)=>{const n=f(e,m);return n?{...t,events:t.events.map(e=>e.properties.welcomeMessage||e.properties.invitation?{...e,id:n.id}:e)}:t},z=e=>({...e,properties:{...e.properties,accepted:!0}}),D=(e,t)=>{let{sdk:n}=e;const{uniqueId:r,id:o}=t.event.properties;return n.acceptGreeting({greetingId:o,uniqueId:r}).then(()=>z(t),e=>{if("INTERNAL"===e.code)return z(t);throw e})},b=e=>"VALIDATION"===(null==e?void 0:e.code)&&/phone_number/.test(e.message),q=function(t,n){var r,o,s;let{sdk:a,store:i}=t,{group:c,customerStartingEvent:u,startingEvents:d,agentFakeEvent:I}=void 0===n?{}:n;const f=d??(u?[u]:[]),v=f.find(e=>{var t,n;return"ticket"===(null==(n=null==(t=null==e?void 0:e.properties)?void 0:t.lc2)?void 0:n.form_type)}),S=f.some(e=>"file"===(null==e?void 0:e.type)),C=f.find(e=>"filled_form"===(null==e?void 0:e.type)),k={chat:{thread:{properties:{}},properties:{}}};l(i)&&(k.continuous=!0,k.chat.properties.routing={email_follow_up:!0}),"number"==typeof c&&(k.chat.access={groupIds:[c]});let F={};if(C){const e=null==(r=y(e=>"phone"===e.type,C.fields))?void 0:r.answer;F={..._(Boolean,{name:null==(o=y(e=>"name"===e.type,C.fields))?void 0:o.answer,email:null==(s=y(e=>"email"===e.type,C.fields))?void 0:s.answer,phoneNumber:T(e)?e:void 0})}}const M=i.getSessionUser();M.name&&"Customer"!==M.name||(F.name=i.localize("client"),F.nameIsDefault=!0);const P=w(F)?Promise.resolve():a.updateCustomer(F).catch(e=>{if(b(e))throw e;p("update_customer_request_failed",e)});if(v)k.active=!1,k.chat.thread.properties.routing={offline_message:!0},k.chat.thread.events=[h(i,v)];else{const t=S?[]:i.getEvents(m).filter(t=>t.type===e&&null===t.serverId),n=[].concat(t,f).map(e=>h(i,e)).filter(Boolean);if(w(n)||(k.chat.thread.events=n),I){const{properties:e}=I;e.invitation?k.chat.thread.properties=(e=>({lc2:{welcome_author_id:e.author,greeting_id:e.properties.id,greeting_unique_id:e.properties.uniqueId}}))(I):e.id&&(k.welcomeMessageId=e.id)}const r=(e=>{var t;const n=e.find(e=>U.includes(null==e?void 0:e.type));return n?(null==(t=n.properties)?void 0:t.source)??"other":null})([].concat(t,f));r&&(k.chat.thread.properties.chats={first_customer_message_source:r})}const A=i.getChat(m).serverId;return A?P.then(()=>a.resumeChat({...k,chat:{...k.chat,id:A}})).then(e=>x(i,g(i,e))):P.then(()=>a.startChat(k)).then(e=>x(i,g(i,e)))},E=e=>{let{sdk:t}=e;return t.listChats().then(e=>{let{chatsSummary:t}=e;const n=a(t);return n?i(n):null})},N=(e,t)=>{let{sdk:n}=e,{groupIds:r}=t;return n.listGroupStatuses({groupIds:r}).then(e=>r.reduce((t,n)=>{const r=e[n];return t[n]=r?d(r):"not_found",t},{}))},j=(e,t)=>{let{groupId:n}=t;return N(e,{groupIds:[n]}).then(e=>{let{[n]:t}=e;return t})},Q=function(e,t,n){let{sdk:r}=e;return void 0===n&&(n=v),r.getForm(t).then(e=>e.enabled?{enabled:!0,form:n(t.type,e.form)}:e).catch(e=>(p("get_form_request_failed",e),{enabled:!1}))},H=(e,t)=>{let{sdk:n,store:r}=e,o=r.getApplicationState("welcomeMessageId");return"message_and_questions"===t&&"questions_only"===r.getApplicationState("welcomeMessageScope")&&(o=null),n.requestWelcomeMessage({...o?{id:o}:{},aiAgentResponseScope:t}).then(e=>{let{id:t,predictedAgent:n,queue:r}=e;return{welcomeMessageId:t,agent:n,groupHasQueue:r}})},L=(e,t)=>Q(e,{groupId:t,type:"postchat"}),O=(e,t)=>Q(e,{groupId:t,type:"ticket"},(e,t)=>S(t)),R=(e,t)=>{let{store:n}=e,{groupId:r}=t;const{region:o,organizationId:s}=n.getApplicationState();return c({organizationId:s,groupId:r,region:o,version:"tfm"}).then(e=>e.__unsafeProperties.ticketFormMode||u(e))},G=(e,t)=>{let{sdk:n}=e,{chatId:r}=t;return n.getChat({chatId:r}).then(e=>{let{users:t,eventsSeenUpToMap:n}=e;return{users:t,eventsSeenUpToMap:n}})},B=(e,t)=>{let{sdk:n,store:o}=e,{chatId:s,threadId:a}=t;return n.getChat({chatId:s,threadId:a}).then(e=>r(o,{thread:e.thread,users:e.users,eventsSeenUpToMap:e.eventsSeenUpToMap}))},$=(e,t)=>t.next().then(t=>{let{value:n,done:r}=t;return n?{value:n.threads.map(t=>s(e,{thread:t})),done:r}:{value:n,done:r}}).catch(e=>{if("NOT_FOUND"===e.code)return{value:[],done:!0};throw e}),W=(e,t,n)=>{const{store:r}=e;o(r)&&!A.getHistoryIterator()&&A.setHistoryIterator(n||((e,t)=>{let{sdk:n,store:r}=e;const o=n.getChatHistory({chatId:t});return{next:()=>$(r,o)}})(e,t))},Z=(e,t)=>{let{sdk:o,store:s}=e;const a=o.getChatHistory({chatId:t});return Promise.all([a.next(),G({sdk:o},{chatId:t})]).then(e=>{let[{value:t,done:o},{users:i,eventsSeenUpToMap:c}]=e;const u=n("id",i);if(t&&t.threads.some(e=>e.events.some(e=>"system"!==e.authorId&&!(e.authorId in u)))){const e=new Error;throw e.code="MISSING_USER",e}return{threads:t&&t.threads.map(e=>r(s,{thread:e,users:i,eventsSeenUpToMap:c}).thread).filter(Boolean),eventsSeenUpToMap:c,hasMore:!o,iterator:{next:()=>$(s,a)}}}).catch(e=>{if("NOT_FOUND"===e.code)return{threads:[],hasMore:!1};throw e})},V=async(e,n)=>{const r=await t(`https://api.helpdesk.com/v1/contactForms/formSettings?licenseID=${e}&lcGroupID=${n}`);if(!r.ok)throw new Error("Failed to fetch HelpDesk ticket form configuration");return r.json()},X=(e,t)=>{let{sdk:n,store:r}=e;return n.rateChat({chatId:I(r),rating:C({comment:t.rateComment,score:F(t.rating)})}).then(()=>{r.updateChat(m,{properties:C({rate:t.rating,rateComment:t.rateComment})})}).catch(k)},J=(e,t)=>{let{sdk:n,store:r}=e;return n.cancelRate({chatId:I(r),properties:t}).then(()=>{r.updateChat(m,{properties:t.reduce((e,t)=>("comment"===t?e.rateComment=null:"score"===t&&(e.rate=null),e),{})})}).catch(k)},K=M(300,(e,t)=>{const{store:n,sdk:r}=e,o=I(n),s=new Date(t).toISOString().replace(/Z$/,"999Z");o&&r.markEventsAsSeen({chatId:o,seenUpTo:s}).catch(k)});export{B as a,L as b,A as c,q as d,J as e,V as f,Z as g,K as h,O as i,D as j,H as k,N as l,W as m,E as n,j as o,R as p,b as q,M as t,X as u};