'use strict'; var pluginJobHandler = require('@flatfile/plugin-job-handler'); var api = require('@flatfile/api'); var utilCommon = require('@flatfile/util-common'); var p=(s,o,e)=>{try{return s.reduce((r,t)=>{let{value:i}=t.values[o];return e.has(i)?[...r,t.id]:(e.add(i),r)},[])}catch{throw new Error("Failed to keep first record")}};var m=(s,o,e)=>{try{let r=s.reduce((t,i)=>{if(i.values[o].value==null)return t;let n=String(i.values[o].value);return e.add(n),t[n]==null?t[n]=[i.id]:t[n].push(i.id),t},{});return Object.keys(r).reduce((t,i)=>{let n=r[i];if(n.length>1){let l=n.slice(0,-1);return [...t,...l]}else return t},[])}catch{throw new Error("Failed to keep last record")}};var c=new api.FlatfileClient,g=async(s,o,e)=>{let{sheetId:r,workbookId:t}=s.context;if(!r)throw new Error("Dedupe must be called from a sheet-level action");await o(0,"Dedupe started");let i=["first","last"].includes(e.keep);if(i&&e.on===void 0)throw new Error(`\`on\` is required when \`keep\` is ${e.keep}`);if(i&&e.on!==void 0){let{data:{config:{fields:a}}}=await c.sheets.get(r);if(a.find(f=>f.key===e.on)===void 0)throw new Error(`Field "${e.on}" not found`)}let n=new Set,l=[];return await utilCommon.processRecords(r,async(a,d,f)=>{let u=[];e.keep==="first"?u=p(a,e.on,n):e.keep==="last"?u=m(a,e.on,n):e.custom&&(u=e.custom(a,n)),l=l.concat(u);let h=Math.min(d/f*100,99);await o(h,`Processing ${a.length} records on page ${d} of ${f}`);}),l.length===0?{info:"No duplicates found"}:(await c.jobs.create({type:"workbook",operation:"delete-records",trigger:"immediate",source:t,config:{sheet:r,filter:"none",exceptions:l}}),{info:"Successfully removed duplicates"})};var A=(s,o)=>e=>{e.use(pluginJobHandler.jobHandler({operation:s},async(r,t)=>{await g(r,t,o);}));}; exports.dedupePlugin = A; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map