import{_ as h,da as b}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 T=e=>({addConnectionHelpingText:h("You can find your API key from ")+`<a target="_blank" class="text-blue-600 underline" rel="noopener noreferrer nofollow" href="https://airtable.com/create/tokens">${h("Here")}</a>`,componentName:e.componentName.connection,connectionsTypes:[{encryptKeys:["token"],label:h("API Key"),type:"bearer_token",verifyConnection:{headers:{Authorization:""},method:"GET",responseKeyMatch:["bases"],url:"https://api.airtable.com/v0/meta/bases"}}],fieldType:"config",id:"connection-id",label:h("Select Connection"),onConnectionAddChange:"CONNECTION_ADD_CHANGE",onConnectionChange:"SET_CONNECTION",render:!0,value:void 0}),A=({$:e,e:t})=>{e.setThisComponent(o=>{e.util.isConnectionComponent(o)&&o.connectionsTypes.forEach(n=>{var a;n.type==="bearer_token"&&((a=n.verifyConnection)!=null&&a.headers)&&t.token&&(n.verifyConnection.headers.Authorization="Bearer "+t.token)})})};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=>b({headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:e}]},method:"GET",url:"https://api.airtable.com/v0/meta/bases"}),p=(e,t)=>b({headers:{Authorization:["Bearer ",{encryption:"hmac_decrypt",value:e}]},method:"GET",url:`https://api.airtable.com/v0/meta/bases/${t}/tables`});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 _=(e,t)=>{const o=e.getComponent("base-id");!t.isSelectComponent(o)||!o.value||!o.options||o.options.some(n=>n.value===o.value)||(e.setComponent("base-id",n=>{n.value=void 0}),e.setComponent("table-id",n=>{n.value=void 0}),e.setDb(n=>{n.baseId=void 0,n.tableId=void 0}))},k=async e=>{e.setComponent("base-id",t=>{e.util.isSelectComponent(t)&&(t.loading=!0)});try{const t=await e.getConnection();if(!t)return;const{data:o,status:n}=await g(t.auth_details.token);if(n!=="success"){e.setComponent("base-id",a=>{e.util.isSelectComponent(a)&&(a.status=n,a.invalidMessage=o)}),console.error("Failed to fetch bases");return}if(!(o!=null&&o.bases))return;e.setComponent("base-id",a=>{e.util.isSelectComponent(a)&&(a.options=o.bases.map(s=>({label:s.name,value:s.id})),a.status=void 0)})}finally{e.setComponent("base-id",t=>{e.util.isSelectComponent(t)&&(t.loading=!1)})}},S=async e=>{var t;e.setComponent("table-id",o=>{e.util.isSelectComponent(o)&&(o.loading=!0)});try{const o=await e.getConnection();if(!o)return;const n=o.auth_details.token,a=(t=e.getComponent("base-id"))==null?void 0:t.value;if(!a)return;const{data:s,status:l}=await p(n,a);if(l!=="success"){e.setComponent("table-id",i=>{e.util.isSelectComponent(i)&&(i.status=l,i.invalidMessage=s)}),console.error("Failed to fetch tables");return}if(!(s!=null&&s.tables))return;e.setComponent("table-id",i=>{e.util.isSelectComponent(i)&&(i.options=s.tables.map(d=>({label:d.name,value:d.id})),i.status=void 0)})}finally{e.setComponent("table-id",o=>{e.util.isSelectComponent(o)&&(o.loading=!1)})}},w=async e=>{var t,o;e.setComponent("record-fields",n=>{e.util.isRepeaterFieldComponent(n)&&(n.loading=!0,n.labelActionLoading=!0)});try{const n=await e.getConnection();if(!n)return;const a=n.auth_details.token,s=(t=e.getComponent("base-id"))==null?void 0:t.value,l=(o=e.getComponent("table-id"))==null?void 0:o.value;if(!s||!l)return;const{data:i,status:d}=await p(a,s);if(d!=="success"){console.error("Failed to fetch table fields");return}if(!(i!=null&&i.tables))return;const u=i.tables.find(c=>c.id===l||c.name===l);if(!(u!=null&&u.fields))return;const m=u.fields,C={};m.forEach(c=>{C[c.name]=c.type}),e.setDb(c=>{c.fieldTypes=C}),e.setComponent("record-fields",c=>{if(e.util.isRepeaterFieldComponent(c)&&c.fieldsMetaData){const f=c.fieldsMetaData.find(r=>r.name==="airtableFieldKey");f&&f.componentName==="select"&&(f.options=m.map(r=>({fieldType:r.type,label:r.name,value:r.name})))}})}finally{e.setComponent("record-fields",n=>{e.util.isRepeaterFieldComponent(n)&&(n.loading=!1,n.labelActionLoading=!1)})}};export{w as a,k as b,T as c,_ as d,S as f,A as h};
