import{da as y,_ as c}from"./main-tangy-colts-beg.js";globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(e,t){return this.cache.has(e)?this.cache.get(e):(this.cache.set(e,t),t)}};const C=(e,t)=>y({bodyParams:{api_key:{encryption:"hmac_decrypt",value:t}},headers:{"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`${e}/api/brands/get-brands.php`}),m=(e,t,i)=>y({bodyParams:{api_key:{encryption:"hmac_decrypt",value:t},brand_id:i},headers:{"Content-Type":"application/x-www-form-urlencoded"},method:"POST",url:`${e}/api/lists/get-lists.php`});globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(e,t){return this.cache.has(e)?this.cache.get(e):(this.cache.set(e,t),t)}};const g=async e=>{var t,i;e.setComponent("brand-id",n=>{e.util.isSelectComponent(n)&&(n.loading=!0)});try{const n=await e.getConnection();if(!n)return;const o=n.auth_details.value,u=(i=(t=n.auth_details)==null?void 0:t.extraData)==null?void 0:i.sendyUrl,{data:h,status:r}=await C(u,o);if(r!=="success"){e.setComponent("brand-id",s=>{e.util.isSelectComponent(s)&&(s.status=r,s.invalidMessage=c("Failed to fetch brands"))});return}const d=Object.values(h);e.setComponent("brand-id",s=>{e.util.isSelectComponent(s)&&(s.options=d.map(l=>({label:l.name,value:l.id})))})}finally{e.setComponent("brand-id",n=>{e.util.isSelectComponent(n)&&(n.loading=!1)})}},b=async e=>{var t,i,n;e.setComponent("list-id",o=>{e.util.isSelectComponent(o)&&(o.loading=!0)});try{const o=await e.getConnection();if(!o)return;const u=o.auth_details.value,h=(i=(t=o.auth_details)==null?void 0:t.extraData)==null?void 0:i.sendyUrl,r=(n=e.getComponent("brand-id"))==null?void 0:n.value;if(!r){e.setComponent("list-id",a=>{e.util.isSelectComponent(a)&&(a.status="error",a.invalidMessage=c("Please select a brand first"))});return}const{data:d,status:s}=await m(h,u,r);if(s!=="success"){e.setComponent("list-id",a=>{e.util.isSelectComponent(a)&&(a.status=s,a.invalidMessage=d)});return}const l=Object.values(d);if(l.length===0){e.setComponent("list-id",a=>{e.util.isSelectComponent(a)&&(a.status="error",a.invalidMessage=c("No lists found for this brand"))});return}e.setComponent("list-id",a=>{e.util.isSelectComponent(a)&&(a.options=l.map(p=>({label:p.name,value:p.id})))})}finally{e.setComponent("list-id",o=>{e.util.isSelectComponent(o)&&(o.loading=!1)})}};globalThis.jotaiAtomCache=globalThis.jotaiAtomCache||{cache:new Map,get(e,t){return this.cache.has(e)?this.cache.get(e):(this.cache.set(e,t),t)}};const T=e=>({addConnectionHelpingText:c("Enter your Sendy installation URL and API key. You can find your API key in your Sendy settings."),componentName:e.componentName.connection,connectionsTypes:[{encryptKeys:["value"],label:c("API Key"),showKey:!1,type:"api_key",verifyConnection:{bodyParams:{api_key:""},headers:{"Content-Type":"application/x-www-form-urlencoded"},method:"POST",responseKeyMatch:["brand1"],url:""}}],extraComponents:[{componentName:e.componentName.input,id:"sendy-url",label:c("Sendy URL"),onChange:"HANDLE_SENDY_URL",placeholder:c("https://your-sendy-domain.com"),render:!0,required:!0,value:void 0,wrapperClassName:"mb-2"}],fieldType:"config",id:"connection-id",label:c("Select Connection"),onConnectionAddChange:"CONNECTION_ADD_CHANGE",onConnectionChange:"SET_CONNECTION",render:!0,value:void 0}),_=({$:e,e:t})=>{e.setThisComponent(i=>{e.util.isConnectionComponent(i)&&i.connectionsTypes.forEach(n=>{var o;n.type==="api_key"&&((o=n.verifyConnection)!=null&&o.bodyParams)&&t.value&&(n.verifyConnection.bodyParams.api_key=t.value)})})},S=({$:e,e:t})=>{const i=t.target.value;e.setComponent("connection-id",n=>{var o;e.util.isConnectionComponent(n)&&((o=n.connectionsTypes[0])!=null&&o.verifyConnection&&(n.connectionsTypes[0].verifyConnection.url=`${i}/api/brands/get-brands.php`),n.extraData||(n.extraData={}),n.extraData.sendyUrl=i)})};export{g as a,_ as b,T as c,b as f,S as h};
