import{da as u}from"./main-tangy-colts-beg.js";globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,e){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,e),e)}};const p="https://api.woodpecker.co/rest/v1",h=async t=>{try{const e=await u({headers:{Authorization:["Basic ",{encryption:"base64_encode",value:{encryption:"hmac_decrypt",value:t}}],"Content-Type":"application/json"},method:"GET",url:`${p}/prospects`});if(e.status!=="success")throw new Error(`API request failed: ${e.status}, ${e}`);return e}catch(e){throw console.error("Error in getProspects:",e),e}},d=async t=>{try{const e=await u({headers:{Authorization:["Basic ",{encryption:"base64_encode",value:{encryption:"hmac_decrypt",value:t}}]},method:"GET",url:`${p}/campaign_list`});if(e.status!=="success")throw new Error(`campaign request failed: ${e.status}, ${e.data}`);return e}catch(e){throw console.error("Error in getCampaign:",e),e}},m=async(t,e)=>{try{const s=await u({headers:{Authorization:["Basic ",{encryption:"base64_encode",value:{encryption:"hmac_decrypt",value:e}}]},method:"GET",url:`${p}/prospects?id=${t}`});if(s.status!=="success")throw new Error(`prospects request failed: ${s.status}, ${s.data}`);return s}catch(s){throw console.error("Error in getProspects by email:",s),s}};globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(t,e){return this.cache.has(t)?this.cache.get(t):(this.cache.set(t,e),e)}};const f=async t=>{t.setComponent("prospect-id",s=>{s.loading=!0});const e=await t.getConnection();if(e)try{const s=await h(e.auth_details.value),{data:a,status:o}=s;if(o!=="success"||!Array.isArray(a))throw new Error("Invalid response format");const n=a;t.setComponent("prospect-id",r=>{t.util.isSelectComponent(r)&&(r.options=n.map(i=>({label:`${i.email} (${i.id})`,value:i.id})),r.loading=!1)})}catch(s){t.setComponent("prospect-id",a=>{t.util.isSelectComponent(a)&&(a.loading=!1,a.status="error",a.invalidMessage=s.message)})}},y=async t=>{t.setComponent("campaign-id",s=>{s.loading=!0});const e=await t.getConnection();if(!e){console.warn("no connection");return}try{const{data:s,status:a}=await d(e.auth_details.value);if(a!=="success"||!Array.isArray(s))throw new Error("Invalid response format");const o=s;t.setComponent("campaign-id",n=>{t.util.isSelectComponent(n)&&(n.options=o.map(r=>({label:`${r.name}`,value:r.id})),n.loading=!1)})}catch(s){t.setComponent("campaign-id",a=>{t.util.isSelectComponent(a)&&(a.loading=!1,a.status="error",a.invalidMessage=s.message)})}},w=async(t,e)=>{e.setComponent("prospects-entry",a=>{a.loading=!0});const s=await e.getConnection();if(!s){console.warn("no connection");return}try{const{data:a,status:o}=await m(t,s.auth_details.value);if(o!=="success")throw new Error("Invalid response format");const n=new Set(["id","updated"]),i=Object.entries(a[0]).filter(([c,l])=>!n.has(c)&&l).map(([c,l])=>[{name:"woodpeckerField",value:c},{name:"value",value:[{type:"string",value:l}]}]);return e.setComponent("prospects-entry",c=>{e.util.isSelectComponent(c)&&(c.options=i,c.loading=!1)}),i}catch(a){e.setComponent("prospects-entry",o=>{e.util.isSelectComponent(o)&&(o.loading=!1,o.status="error",o.invalidMessage=a.message)})}};export{f as a,w as b,y as f};
