import{aa as r,ab as h}from"./_applist-tangy-colts-beg-36.js";import{da as u,_ as a}from"./main-tangy-colts-beg.js";import{s as m}from"./machine.-tangy-colts-beg-869.js";import{h as C,a as p,w as d,u as N}from"./commonut-tangy-colts-beg-567.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,n,o,i,c)=>u({bodyParams:{webhook:{events:n,name:o,sources:c,url:i}},headers:{"Api-Token":{encryption:"hmac_decrypt",value:t},"Content-Type":"application/json"},method:"POST",url:`${e}/api/3/webhooks`});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 T=async(t,e)=>{var i,c,l,s;const n=await t.getConnection();if(!n||!((i=n.auth_details)!=null&&i.value))return;const{data:o}=await E(n.auth_details.value,n.auth_details.extraData.url,(c=t.getComponent("eventname"))==null?void 0:c.value,(l=t.getComponent("name"))==null?void 0:l.value,e,(s=t.getComponent("source"))==null?void 0:s.value)};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 O=r(({helpers:t})=>({states:{components:[{componentName:t.componentName.webhook,extraComponents:[{addConnectionHelpingText:a("You can get your API key in your activeCampaign settings tab, developer sections"),componentName:t.componentName.connection,connectionsTypes:[{encryptKeys:["value"],label:a("API Key"),showKey:!1,type:"api_key",verifyConnection:{headers:{"Api-Token":"","Content-Type":"application/json"},method:"GET",responseKeyMatch:["user"],url:""}}],extraComponents:[{componentName:t.componentName.input,id:"url",label:a("Url"),onChange:"HANDLE_URL",placeholder:a("Enter Url"),render:!0,required:!0,value:void 0,wrapperClassName:"mb-2"}],extraData:{},fieldType:"config",id:"connection-id",label:a("Select Connection"),onConnectionAddChange:"CONNECTION_ADD_CHANGE",onConnectionChange:"SET_CONNECTION",render:!0,value:void 0},{componentName:t.componentName.input,helperText:a("Enter name of the webhook"),id:"name",label:a("Name"),onChange:"HANDLE_NAME",render:"IF_CONNECTION_SELECTED",value:void 0},{componentName:t.componentName.select,fieldType:"config",helperText:a("Select an event, that will attach to the webhook. The webhook will be triggered when the selected event occurs in activeCampaign."),id:"eventname",label:a("Select an Event"),mode:"multiple",onChange:"HANDLE_EVENT",optionFilterProp:"label",options:d,render:"IF_CONNECTION_SELECTED",required:!0,showSearch:!0,value:void 0},{componentName:t.componentName.select,helperText:a("Enter source for the webhook"),id:"source",label:a("Source"),mode:"multiple",onChange:"HANDLE_SOURCE",options:[{label:a("Public"),value:"public"},{label:a("System"),value:"system"}],render:"IF_CONNECTION_SELECTED",value:void 0}],fieldType:"config",id:"webhook-id",label:a("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 n;return h((n=e.getComponent("connection-id"))==null?void 0:n.value)},ON_MACHINE_LOAD:async({$:e})=>{m(e,[{db:"webhookId",id:"webhook-id"}]),N({$:e})},CONNECTION_ADD_CHANGE:p,HANDLE_NAME:({$:e,e:n})=>{e.setThisComponent(o=>{o.value=n.target.value})},HANDLE_SOURCE:({$:e,e:n})=>{e.setThisComponent(o=>{o.value=n})},HANDLE_URL:C,SET_CONNECTION:async({$:e,e:n})=>{e.setThisComponent(o=>{o.value=n}),e.setDb(o=>{o.connectionId=n,o.urlDb=""})},SET_WEBHOOK:async({$:e,e:{id:n}})=>{e.setThisComponent(o=>{o.value=n}),e.setDb(o=>{o.webhookId=n})},HANDLE_EVENT:async({$:e,e:n})=>{e.setThisComponent(o=>{o.value=n,e.util.isSelectComponent(o)&&(o.status=void 0)}),e.setDb(o=>{o.eventName=n})},HANDLE_VALIDATION:({$:e})=>{var n,o;if(!((n=e.getComponent("connection-id"))!=null&&n.value))return e.setComponent("connection-id",i=>{e.util.isConnectionComponent(i)&&(i.status="error",i.invalidMessage="Please select a connection")}),!1;if(!((o=e.getComponent("eventname"))!=null&&o.value))return e.setComponent("eventname",i=>{e.util.isSelectComponent(i)&&(i.status="error",i.invalidMessage="Please select an event")}),!1},CONNECT_WEBHOOK:async({$:e,e:n})=>{await T(e,n.url),e.setComponent("eventname",o=>{e.util.isSelectComponent(o)&&(o.value=void 0)}),e.setComponent("connection-id",o=>{e.util.isConnectionComponent(o)&&(o.value=void 0)})}}}));export{O as default};
