import{aa as m,ab as r}from"./_applist-tangy-colts-beg-36.js";import{da as p,_ as i}from"./main-tangy-colts-beg.js";import{s as d}from"./machine.-tangy-colts-beg-869.js";import"./lodash-tangy-colts-beg-516.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 h=async e=>p({headers:{"x-api-key":{encryption:"hmac_decrypt",value:e}},method:"GET",url:"https://api.systeme.io/api/tags"}),C=async e=>p({headers:{"x-api-key":{encryption:"hmac_decrypt",value:e}},method:"GET",url:"https://api.systeme.io/api/contact_fields"});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=(e,t)=>{const o=e.getComponent("tag-id"),a=e.getComponent("contact-data");!t.isSelectComponent(o)||!o.value||!o.options||o.options.some(n=>n.value===o.value)||!t.isSelectComponent(a)||!a.value||!a.options||a.options.some(n=>n.value===a.value)||(e.setComponent("tag-id",n=>{n.value=void 0}),e.setDb(n=>{n.tagId=void 0}))},c=async e=>{e.setComponent("tag-id",n=>{n.loading=!0});const t=await e.getConnection();if(!t){e.setComponent("tag-id",n=>{n.loading=!1});return}const{data:o,status:a}=await h(t.auth_details.value);if(a==="error"){e.setComponent("tag-id",n=>{e.util.isSelectComponent(n)&&(n.status=a,n.invalidMessage=o)}),console.error("Failed to fetch tags");return}e.setComponent("tag-id",n=>{var s;e.util.isSelectComponent(n)&&(n.options=(s=o==null?void 0:o.items)==null?void 0:s.map(l=>({label:l.name,value:l.id})),n.loading=!1,n.status=void 0)})},T=e=>{const t=[];return Array.isArray(e==null?void 0:e.items)?e.items.forEach(o=>{o!=null&&o.fieldName&&(o!=null&&o.slug)?t.push({label:o.fieldName,value:o.slug}):console.warn("Invalid field encountered:",o)}):console.warn("No ContactFields found in data or ContactFields is not an array."),t},u=async e=>{e.setComponent("contact-data",a=>{a.loading=!0});const t=await e.getConnection();if(!t){e.setComponent("contact-data",a=>{a.loading=!1});return}const{data:o}=await C(t.auth_details.value);e.setComponent("contact-data",a=>{if(!e.util.isRepeaterFieldComponent(a))return;const n=a.fieldsMetaData[0];n.options=T(o),a.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 f=m(({helpers:e})=>({states:{components:[{addConnectionHelpingText:i('You can get your API key from <a class="text-blue-600 underline" href="https://systeme.io/dashboard/profile/public-api-settings" target="_blank" rel="noopener noreferrer nofollow">here</a>'),componentName:e.componentName.connection,connectionsTypes:[{encryptKeys:["value"],label:i("API Key"),showKey:!1,type:"api_key",verifyConnection:{headers:{Authorization:""},method:"GET",responseKeyMatch:["items"],url:"https://api.systeme.io/api/contacts"}}],fieldType:"config",id:"connection-id",label:i("Select Connection"),onConnectionAddChange:"CONNECTION_ADD_CHANGE",onConnectionChange:"SET_CONNECTION",render:!0,value:void 0},{componentName:e.componentName.select,fieldType:"config",id:"tag-id",label:i("Tags"),onChange:"SELECT_TAG",onRefetch:"REFETCH_TAG",optionFilterProp:"label",options:[],placeholder:i("Select a tag"),render:"IF_CONNECTION_SELECTED",showSearch:!0,value:void 0},{addItemButtonLabel:"Add Column",componentName:e.componentName.repeaterField,fieldsMetaData:[{componentName:e.componentName.select,label:i("Column"),name:"column",options:[],required:!0,showSearch:!0,style:{width:200},value:""},{componentName:e.componentName.mixInput,label:i("Value"),name:"value",required:!0,value:[],wrapperClassName:"w-100"}],id:"contact-data",label:i("Field Mapping"),nonRemovableRows:[0],onChange:"ON_CHANGE_ROW_DATA",path:"rowData",render:"IF_CONNECTION_SELECTED",value:[[{disabled:!0,name:"column",value:"email"},{name:"value",value:[]}]]}]},actions:{CONNECTION_ADD_CHANGE:({$:t,e:o})=>{t.setThisComponent(a=>{t.util.isConnectionComponent(a)&&a.connectionsTypes.forEach(n=>{var s;n.type==="api_key"&&((s=n.verifyConnection)!=null&&s.headers)&&(n.verifyConnection.headers["x-api-key"]=o.value)})})},IF_CONNECTION_SELECTED:({$:t})=>{var o;return r((o=t.getComponent("connection-id"))==null?void 0:o.value)},IF_TAG_SELECTED:({$:t})=>{var o;return r((o=t.getComponent("tag-id"))==null?void 0:o.value)},ON_CHANGE_ROW_DATA:({$:t,e:o})=>{t.setThisComponent(a=>{a.value=o}),t.setDb(a=>{a.rowData=o})},ON_MACHINE_LOAD:async({$:t})=>{d(t,[{db:"connectionId",id:"connection-id"},{db:"tagId",id:"tag-id"},{db:"rowData",id:"contact-data"}]),await Promise.all([c(t),u(t)])},REFETCH_TAG:async({$:t})=>{await c(t)},SELECT_TAG:async({$:t,e:o})=>{t.setThisComponent(a=>{a.value=o}),t.setComponent("tag-title",a=>{a.value=void 0}),t.setDb(a=>{a.tagId=o,a.tagTitle=void 0})},SET_CONNECTION:async({$:t,e:o})=>{t.setThisComponent(a=>{a.value=o}),t.setDb(a=>{a.connectionId=o}),g(t,e),await c(t),await u(t)}}}));export{f as default};
