'use strict'; var api = require('@flatfile/api'); var pluginJobHandler = require('@flatfile/plugin-job-handler'); var utilCommon = require('@flatfile/util-common'); var modernAsync = require('modern-async'); var crypto = require('crypto'); var r=new api.FlatfileClient;function A(d){async function l(t=!1){let{data:p}=await r.spaces.list({archived:!1,namespace:d.namespace.split(":")[1],pageSize:1e3});console.log(`Fetched ${p.length} spaces`);let n=await modernAsync.asyncMap(p,async a=>{let{data:e}=await r.secrets.list({spaceId:a.id});return {space:a,secrets:e}},10);console.log(`Hydrated secrets for ${n.length} spaces`);let o=n.filter(({secrets:a})=>a.some(e=>e.name==="FF_AUTO_UPDATE"&&e.value==="true")),s=n.filter(({secrets:a})=>a.some(e=>e.name==="FF_AUTO_UPDATE_DEV"&&e.value==="true")),i=await modernAsync.asyncMap(t?s:o,({space:a})=>m(a));console.log(`Triggered schema for ${i.length} spaces`);}async function m(t){await r.jobs.create({type:"space",source:t.id,operation:"auto-update",trigger:"immediate",managed:!0,mode:"foreground",environmentId:t.environmentId});}let u=t=>{!!!process.env.LAMBDA_TASK_ROOT&&d.dev&&l(!0).then(()=>{console.log("running local dev refresh based update of spaces, suppress this with dev=false in the autoUpdate plugin config");});let n=async o=>{let s=crypto.randomUUID();console.log("Hooks updating =>",o);let a=(await utilCommon.Simplified.getAllRecords(o)).map(e=>(e.metadata={...e.metadata||{},_autoUpdateKey:s},e));await utilCommon.Simplified.updateAllRecords(o,a),console.log("Hooks updated =>",o);};t.use(pluginJobHandler.jobHandler("space:auto-update",async o=>{let{data:s}=await r.spaces.get(o.context.spaceId),{data:i}=await r.workbooks.list({spaceId:s.id}),a=await d.updater(s,i.filter(e=>!e.name.includes("[file]")));return a?.length&&await modernAsync.asyncMap(a,async e=>{console.log("workbook schema updated, triggering hooks",e.id),await modernAsync.asyncMap(e.sheets,g=>n(g.id),10);}),{info:"Successfully updated schema"}}));};return u.root=t=>{t.on("agent:created",()=>l(!1)),t.on("agent:updated",()=>l(!1));},u} exports.rollout = A; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map