import{aa as r,ab as d}from"./_applist-tangy-colts-beg-36.js";import{da as m,_ as s}from"./main-tangy-colts-beg.js";import{s as u}from"./machine.-tangy-colts-beg-869.js";import{h as l,a as C,c as p,s as N}from"./commonut-tangy-colts-beg-867.js";import"./lodash-tangy-colts-beg-516.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 E=(t,e,o)=>m({bodyParams:{"enabled_events[]":e,url:o},headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:t}],"content-type":"application/x-www-form-urlencoded"},method:"POST",url:"https://api.stripe.com/v1/webhook_endpoints"});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 b=async(t,e)=>{var c,h;const o=await t.getConnection();if(!o||!((c=o.auth_details)!=null&&c.value))return;const{data:n}=await E(o.auth_details.value,(h=t.getComponent("eventname"))==null?void 0:h.value,e);let a="Success! We have attached the webhook to stripe.";n.id||(a="Something went wrong"),t.setComponent("note-id",i=>{t.util.isNoteComponent(i)&&(i.note=[a],i.render=!0)})};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 v=r(({helpers:t})=>({states:{components:[{componentName:t.componentName.webhook,extraComponents:[p(t),{componentName:t.componentName.select,fieldType:"config",helperText:s("Select an event, that will attach to the webhook. The webhook will be triggered when the selected event occurs in stripe."),id:"eventname",label:s("Select an Event"),onChange:"HANDLE_EVENT",optionFilterProp:"label",options:N,render:"IF_CONNECTION_SELECTED",required:!0,showSearch:!0,value:void 0}],fieldType:"config",id:"webhook-id",label:s("Webhook"),onSave:"CONNECT_WEBHOOK",onValidate:"HANDLE_VALIDATION",onWebhookChange:"SET_WEBHOOK",render:!0,value:void 0},{componentName:t.componentName.note,fieldType:"config",id:"note-id",note:[],render:!1}]},actions:{IF_CONNECTION_SELECTED:({$:e})=>{var o;return d((o=e.getComponent("connection-id"))==null?void 0:o.value)},ON_MACHINE_LOAD:async({$:e})=>{u(e,[{db:"webhookId",id:"webhook-id"}])},CONNECTION_ADD_CHANGE:C,SET_CONNECTION:l("connectionId"),SET_WEBHOOK:({$:e,e:o})=>{l("webhookId")({$:e,e:o.id})},HANDLE_EVENT:async({$:e,e:o})=>{e.setThisComponent(n=>{n.value=o,e.util.isSelectComponent(n)&&(n.status=void 0)}),e.setDb(n=>{n.eventName=o})},HANDLE_VALIDATION:({$:e})=>{var o,n;if(!((o=e.getComponent("connection-id"))!=null&&o.value))return e.setComponent("connection-id",a=>{e.util.isConnectionComponent(a)&&(a.status="error",a.invalidMessage="Please select a connection")}),!1;if(!((n=e.getComponent("eventname"))!=null&&n.value))return e.setComponent("eventname",a=>{e.util.isSelectComponent(a)&&(a.status="error",a.invalidMessage="Please select an event")}),!1},CONNECT_WEBHOOK:async({$:e,e:o})=>{await b(e,o.url),e.setComponent("eventname",n=>{e.util.isSelectComponent(n)&&(n.value=void 0)}),e.setComponent("connection-id",n=>{e.util.isConnectionComponent(n)&&(n.value=void 0)})}}}));export{v as default};
