'use strict'; var pluginSpaceConfigure = require('@flatfile/plugin-space-configure'); var g = require('cross-fetch'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var g__default = /*#__PURE__*/_interopDefault(g); async function p(n){try{return {workbooks:await Promise.all(n.workbooks.map(async t=>{let i=await g__default.default(t.source);if(!i.ok)throw new Error(`API returned status ${i.status}: ${i.statusText}`);let o=await i.json(),a=o.components.schemas,l=(await Promise.all(t.sheets.map(async r=>{let s=r.model;delete r.model;let f=a[s];if(!f){console.error(`Schema not found for table name ${r.slug}`);return}let u=await d(f,a);return {name:s,slug:s,...r,fields:u}}))).filter(Boolean);return {name:o.info.title,...t,sheets:l}})),space:n.space}}catch(e){throw console.error(e),new Error(`Error fetching or processing schema: ${e.message}`)}}async function d(n,e){return !n||!n.properties?[]:(await Promise.all(Object.keys(n.properties).map(i=>c(n,n.properties[i],i,n.required&&n.required.includes(i)||!1,e)))).flat().filter(Boolean)}async function c(n,e,t="",i=!1,o){if(e.$ref)return c(n,await m(o,e.$ref),t,!1,o);if(e.type==="object"&&e.properties)return (await Promise.all(Object.keys(e.properties).map(async r=>c(e,e.properties[r],t?`${t}_${r}`:r,e.required&&e.required.includes(r)||!1,o)))).flat();if(e.allOf){let r={};for(let s of e.allOf)if(s.$ref){let f=await m(o,s.$ref);r={...r,...f};}else r={...r,...s};return c(n,r,t,i,o)}if(e.type==="string"&&e.enum)return [{key:t,type:"enum",label:t,...e?.description&&{description:e.description},...i&&{constraints:[{type:"required"}]},config:{options:e.enum.map(r=>({value:r,label:String(r)}))}}];let a={string:{key:t,type:"string"},number:{key:t,type:"number"},integer:{key:t,type:"number"},boolean:{key:t,type:"boolean"},array:{key:t,type:"string-list",description:"A column for string arrays!"},enum:{key:t,type:"enum",config:e?.enum?{options:e.enum.map(r=>({value:r,label:String(r)}))}:{options:[]}}},l={label:t,...e?.description&&{description:e.description},...i&&{constraints:[{type:"required"}]},...a[e.type]};return a[l.type]?[l]:[]}async function m(n,e){let t=e.split("/"),i=t[t.length-1],o=n[i];if(!o)throw new Error(`Cannot resolve reference: ${e}`);return o}function k(n,e){return pluginSpaceConfigure.configureSpace(()=>p(n),e)} exports.configureSpaceWithOpenAPI = k; exports.generateFields = d; exports.generateSetup = p; exports.getPropertyType = c; exports.resolveReference = m; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map