import{da as u,_ as g}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 h="https://api.apify.com/v2",A=t=>u({headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:t}]},method:"GET",url:`${h}/acts`}),T=t=>u({headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:t}]},method:"GET",url:`${h}/actor-tasks`}),b=(t,o)=>u({bodyParams:o,headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:t}],"Content-Type":"application/json"},method:"POST",url:`${h}/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 w=async t=>{var o;t.setComponent("actor-id",e=>{t.util.isSelectComponent(e)&&(e.loading=!0)});try{const e=await t.getConnection();if(!e)return;const{data:n,status:s}=await A(e.auth_details.token);if(s!=="success"){t.setComponent("actor-id",a=>{t.util.isSelectComponent(a)&&(a.status=s,a.invalidMessage=n)}),console.error("Failed to fetch actor list");return}if(!((o=n==null?void 0:n.data)!=null&&o.items))return;t.setComponent("actor-id",a=>{t.util.isSelectComponent(a)&&(a.options=n.data.items.map(r=>({label:r.name,value:r.id})))})}finally{t.setComponent("actor-id",e=>{t.util.isSelectComponent(e)&&(e.loading=!1)})}},S=async t=>{var o;t.setComponent("task-id",e=>{t.util.isSelectComponent(e)&&(e.loading=!0)});try{const e=await t.getConnection();if(!e)return;const{data:n,status:s}=await T(e.auth_details.token);if(s!=="success"){t.setComponent("task-id",a=>{t.util.isSelectComponent(a)&&(a.status=s,a.invalidMessage=n)}),console.error("Failed to fetch Tasks list");return}if(!((o=n==null?void 0:n.data)!=null&&o.items))return;t.setComponent("task-id",a=>{t.util.isSelectComponent(a)&&(a.options=n.data.items.map(r=>({label:r.name,value:r.id})))})}finally{t.setComponent("task-id",e=>{t.util.isSelectComponent(e)&&(e.loading=!1)})}},j=async(t,o)=>{var m,p,f,y,C;const e=await t.getConnection();if(!e||!((m=e.auth_details)!=null&&m.token))return;const n=(p=t.getComponent("type"))==null?void 0:p.value,s=(f=t.getComponent("status"))==null?void 0:f.value,r={eventTypes:Array.isArray(s)?s:[s],requestUrl:o},c={};if(n==="actor"){const i=(y=t.getComponent("actor-id"))==null?void 0:y.value;i&&(c.actorId=i)}else if(n==="task"){const i=(C=t.getComponent("task-id"))==null?void 0:C.value;i&&(c.actorTaskId=i)}Object.keys(c).length>0&&(r.condition=c);const{data:l,status:k}=await b(e.auth_details.token,r);let d=g("We have attached the webhook to your Apify account.");(k!=="success"||"error"in l&&l.error)&&(d="error"in l?l.error.message:g("Failed to create webhook. Please try again.")),t.setComponent("note-id",i=>{t.util.isNoteComponent(i)&&(i.note=[d],i.render=!0)})};export{w as a,S as f,j as h};
