import{da as p}from"./main-tangy-colts-beg.js";globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,o){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,o),o)}};const m=t=>p({headers:{Authorization:`Klaviyo-API-Key ${t}`,revision:"2023-09-15"},method:"GET",url:"https://a.klaviyo.com/api/lists"}),b=(t,o)=>{const{description:a,endpointUrl:s,name:e,topicIds:i}=o;return p({bodyParams:{data:{attributes:{...e?{name:e}:{},endpoint_url:s,secret_key:t,...a?{description:a}:{}},relationships:{"webhook-topics":{data:i.map(n=>({id:n,type:"webhook-topic"}))}},type:"webhook"}},headers:{Authorization:`Klaviyo-API-Key ${t}`,"Content-Type":"application/json",revision:"2025-07-15"},method:"POST",url:"https://a.klaviyo.com/api/webhooks"})};globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,o){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,o),o)}};const g=async t=>{t.setComponent("list-id",e=>{e.loading=!0});const o=await t.getConnection();if(!o)return;const{data:a,status:s}=await m(o.auth_details.value);if(s!=="success"){t.setComponent("list-id",e=>{t.util.isSelectComponent(e)&&(e.loading=!1,e.status=s,e.invalidMessage=a)});return}t.setComponent("list-id",e=>{t.util.isSelectComponent(e)&&(e.options=a.data.map(i=>({label:`${i.attributes.name}`,value:i.id})),e.loading=!1)})},f=async(t,o)=>{var r,l,h,d,u;const a=await t.getConnection();if(!a||!((r=a.auth_details)!=null&&r.value))return;const s=(l=t.db)==null?void 0:l.selectedTopics,e=(h=t.db)==null?void 0:h.description,{data:i}=await b(a.auth_details.value,{description:e,endpointUrl:o,name:(d=t.db)==null?void 0:d.webhookName,topicIds:s});let n="We have attached the webhook to your Klaviyo.";(!i||i!=null&&i.errors)&&(n=((u=i.errors[0])==null?void 0:u.detail)||"Unknown error."),t.setComponent("note-id",c=>{t.util.isNoteComponent(c)&&(c.note=[n],c.render=!0)})};export{g as f,f as h};
