'use strict'; var utilCommon = require('@flatfile/util-common'); var hooks = require('@flatfile/hooks'); var m=class{constructor(r){this.records=r;this.records=r;}toFlatfileRecords=()=>{if(this.records instanceof hooks.FlatfileRecords)return this.records;{let r=this.records;return new hooks.FlatfileRecords(r.map(t=>{let a={};for(let[l,s]of Object.entries(t.values))if(s.links?.length&&s.value){let f=s.links.map(c=>{let d={};for(let[g,F]of Object.entries(c.values))d[g]=F.value;return d});a[l]={value:s.value,links:f};}else a[l]=s.value;let n=t.metadata;return {rowId:t.id,rawData:a,metadata:n}}))}};toXRecords=()=>this.records[0]instanceof hooks.FlatfileRecord?this.records.map(o=>{let t=o.toJSON(),a={};for(let[l,s]of Object.entries(t.row.rawData)){let f=t.info.filter(c=>c.field===l).map(c=>({message:c.message,type:c.level,source:"custom-logic",path:c.path}));a[l]={value:s!==null&&typeof s=="object"&&!Array.isArray(s)?s.value:s,messages:f,valid:!0};}let n=t.row.metadata,i=t.config;return {id:String(o.rowId),values:a,metadata:n,config:i}}):this.records};function h(e,r,o){if(e===r)return !0;if(!w(e)||!w(r))return !1;let t=Object.keys(e),a=Object.keys(r);return t.length!==a.length?!1:t.every(n=>n==="values"?b(e,r):a.includes(n)&&h(e[n],r[n]))}function b(e,r){let o=e.values,t=r.values,a=Object.keys(o),n=!1,i=[];return a.forEach(l=>{h(o[l],t[l])?i.push(l):n=!0;}),i.forEach(l=>{delete o[l],delete t[l];}),Object.keys(o).length===0&&(delete e.values,delete r.values),!n}function w(e){return typeof e=="object"&&e!==null}function E(e){e&&(Object.keys(e).includes("valid")&&delete e.valid,L(e.values,["updatedAt"]));}function L(e,r){typeof e!="object"||e===null||Object.keys(e).forEach(o=>{r.includes(o)?delete e[o]:o==="messages"?Array.isArray(e.messages)&&(e.messages=e.messages.filter(t=>t.source==="custom-logic")):typeof e[o]=="object"&&e[o]!==null&&L(e[o],r);});}async function R(e,r){let{commitId:o}=e.context,{trackChanges:t}=e.payload;if(t)try{await e.fetch(`v1/commits/${o}/complete`,{method:"POST"}),r&&utilCommon.logInfo("@flatfile/plugin-record-hook","Commit completed successfully");}catch{utilCommon.logError("@flatfile/plugin-record-hook",`Error completing commit ${o}`);}}async function x(e){return new m(e).toXRecords()}async function O(e){return new m(e).toFlatfileRecords()}function u(e,r){r&&console.time(`\u23F1\uFE0F ${e}`);}function p(e,r){r&&console.timeEnd(`\u23F1\uFE0F ${e}`);}var W=async(e,r,o={})=>{let{concurrency:t=10}=o;return k(e,async(a,n)=>{let i=new Set;for(let l of a){let s=Promise.resolve(r(l,n)).finally(()=>i.delete(s));i.add(s),i.size>=t&&await Promise.race(i);}return await Promise.all(i)},o)},k=async(e,r,o={})=>{let{debug:t=!1}=o;try{u("fetch data",t);let a=await e.cache.init("data",async()=>{try{let i=await e.data;return i.records&&i.records.length?i.records:void 0}catch{throw new Error("Error fetching records")}});if(!a||a.length===0){utilCommon.logInfo("@flatfile/plugin-record-hook","No records to process"),await R(e,t);return}let n=await e.cache.init("records",async()=>await O(a));p("fetch data",t),u("run handler",t),await utilCommon.asyncBatch(n.records,r,o,e),p("run handler",t),e.afterAll(async()=>{u("filter modified records",t);let{records:i}=e.cache.get("records"),l=await x(i),s=await e.cache.get("data"),f=l.filter(c=>{let d=s.find(F=>F.id===c.id);return E(d),!h(c,d,{removeUnchanged:!0})});if(!f||f.length===0){utilCommon.logInfo("@flatfile/plugin-record-hook","No records modified"),await R(e,t);return}p("filter modified records",t),t&&utilCommon.logInfo("@flatfile/plugin-record-hook",`${f.length} modified records`);try{u("update modified records",t),await e.update(f),p("update modified records",t);return}catch{throw new Error("Error updating records")}});}catch(a){utilCommon.logError("@flatfile/plugin-record-hook",a.message),await R(e,t);return}};var q=(e,r,o={})=>t=>{t.on("commit:created",{sheetSlug:e},a=>W(a,r,o));},z=(e,r,o={})=>t=>{t.on("commit:created",{sheetSlug:e},a=>k(a,r,o));}; exports.BulkRecordHook = k; exports.RecordHook = W; exports.bulkRecordHook = z; exports.bulkRecordHookPlugin = z; exports.recordHook = q; exports.recordHookPlugin = q; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map