import{da as l,_ as m}from"./main-tangy-colts-beg.js";globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(e,a){return this.cache.has(e)?this.cache.get(e):(this.cache.set(e,a),a)}};const g=(e,a,s,t)=>l({bodyParams:{SmsMethod:"POST",SmsStatusCallback:t,SmsStatusCallbackMethod:"POST",SmsUrl:t},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),S=(e,a,s,t)=>l({bodyParams:{VoiceMethod:"POST",VoiceUrl:t},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),C=(e,a,s,t)=>l({bodyParams:{RecordingStatusCallback:t,RecordingStatusCallbackMethod:"POST"},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),T=(e,a,s,t)=>l({bodyParams:{SmsStatusCallback:t,SmsStatusCallbackMethod:"POST"},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),P=(e,a,s,t)=>l({bodyParams:{RecordingStatusCallback:t,RecordingStatusCallbackMethod:"POST",SmsStatusCallback:t,SmsStatusCallbackMethod:"POST",StatusCallback:t,StatusCallbackMethod:"POST",VoiceMethod:"POST",VoiceUrl:t},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),f=(e,a,s,t)=>l({bodyParams:{TranscriptionCallback:t,TranscriptionCallbackMethod:"POST",VoiceMethod:"POST",VoiceUrl:t},headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`https://api.twilio.com/2010-04-01/Accounts/${e}/IncomingPhoneNumbers/${s}.json`}),$=(e,a)=>l({headers:{Authorization:`Basic ${btoa(`${e}:${a}`)}`,"Content-Type":"application/x-www-form-urlencoded"},method:"GET",url:"https://messaging.twilio.com/v1/Services"});globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(e,a){return this.cache.has(e)?this.cache.get(e):(this.cache.set(e,a),a)}};const y=async(e,a)=>{var u,d,p,w,b;const s=await e.getConnection();if(!s||!((u=s.auth_details)!=null&&u.password)||!((d=s.auth_details)!=null&&d.username))return;const t=(p=s.auth_details)==null?void 0:p.username,n=(w=s.auth_details)==null?void 0:w.password,i=(b=e.db)==null?void 0:b.phoneNumberId[0].value,h=e.thisNode.machineSlug;let o,c=m("We have attached the webhook to your Twilio.");switch(h){case"watchAllEvents":{o=(await P(t,n,i,a)).data;break}case"watchCalls":{o=(await S(t,n,i,a)).data;break}case"watchMessages":{o=(await g(t,n,i,a)).data;break}case"watchRecordings":{o=(await C(t,n,i,a)).data;break}case"watchSmsStatus":{o=(await T(t,n,i,a)).data;break}case"watchTranscriptions":{o=(await f(t,n,i,a)).data;break}default:{console.warn(`Unhandled machine slug: ${h}`);return}}(!o.account_sid||!o.sid)&&(c=m("Failed to attach webhook to Twilio. Please recheck your Phone Number ID")),e.setComponent("note-id",r=>{e.util.isNoteComponent(r)&&(r.note=[c],r.render=!0)})},A=async e=>{var a,s;e.setComponent("message-service-id",t=>{e.util.isSelectComponent(t)&&(t.loading=!0)});try{const t=await e.getConnection();if(!t)return;const n=(a=t.auth_details)==null?void 0:a.username,i=(s=t.auth_details)==null?void 0:s.password;if(!n||!i)return;const{data:h,status:o}=await $(n,i);if(o!=="success"){e.setComponent("message-service-id",c=>{e.util.isSelectComponent(c)&&(c.status=o,c.invalidMessage=m("Failed to fetch message services"))}),console.error("Failed to fetch message services");return}e.setComponent("message-service-id",c=>{var u;e.util.isSelectComponent(c)&&(c.options=((u=h==null?void 0:h.services)==null?void 0:u.map(d=>({label:d.friendly_name||d.sid,value:d.sid})))||[])})}finally{e.setComponent("message-service-id",t=>{t.loading=!1})}};export{A as f,y as h};
