'use strict'; var pluginSpaceConfigure = require('@flatfile/plugin-space-configure'); var m = require('cross-fetch'); function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; } var m__default = /*#__PURE__*/_interopDefault(m); async function u(t){return {workbooks:await Promise.all(t.workbooks.map(async r=>{let i=await Promise.all(r.sheets.map(async n=>{let o=await g(n.source),s=await d(o);return {name:n?.name||o.title,...o?.description&&{description:o.description},fields:s,...n}}));return {...r,sheets:i}})),space:t.space}}async function g(t){return typeof t=="function"?await t():typeof t=="string"&&y(t)?await p(t):t}function y(t){try{return new URL(t),!0}catch{return !1}}async function d(t){if(!t||!t.properties)return [];let r=(n=>{try{return new URL(n).origin}catch{return ""}})(t.$id);return (await Promise.all(Object.keys(t.properties).map(async n=>await f(t,t.properties[n],n,t.required&&t.required.includes(n)||!1,r)))).flat().filter(Boolean)}async function f(t,e,r="",i=!1,n){if(e.$ref){let a=await P(t,e.$ref,n);return await f(t,a,r,!1,n)}if(e.type==="object"&&e.properties)return (await Promise.all(Object.keys(e.properties).map(async l=>await f(e,e.properties[l],r?`${r}_${l}`:l,e.required&&e.required.includes(l)||!1,n)))).flat();let o={string:{key:r,type:"string"},number:{key:r,type:"number"},integer:{key:r,type:"number"},boolean:{key:r,type:"boolean"},array:{key:r,type:"string-list"},enum:{key:r,type:"enum-list",config:e?.enum?{options:e.enum.map(a=>({value:a,label:String(a)}))}:{options:[]}}};return o[e.type]?[{label:r,...e?.description&&{description:e.description},...i&&{constraints:[{type:"required"}]},...o[e.type]}]:[]}async function P(t,e,r){let i=e.indexOf("#");if(e.startsWith("#/"))return c(t,e);let n=i>=0?e.substring(0,i):e,o=i>=0?e.substring(i):"",s=await p(`${r}${n}`);return o?c(s,o):s}function c(t,e){let r=e.split("/").slice(1).reduce((i,n)=>i&&(i[n]||i.$defs?.[n]||i.definitions?.[n]),t);if(!r)throw new Error(`Cannot resolve reference: ${e}`);return r}async function p(t){try{let e=await m__default.default(t);if(!e.ok)throw new Error(`API returned status ${e.status}: ${e.statusText}`);return await e.json()}catch(e){throw new Error(`Error fetching external reference: ${e.message}`)}}function F(t,e){return pluginSpaceConfigure.configureSpace(()=>u(t),e)} exports.configureSpaceWithJsonSchema = F; exports.fetchExternalReference = p; exports.generateFields = d; exports.generateSetup = u; exports.getPropertyType = f; exports.resolveLocalReference = c; exports.resolveReference = P; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map