'use strict'; var api = require('@flatfile/api'); var utilCommon = require('@flatfile/util-common'); var a=new api.FlatfileClient;async function R(e){let t=0;for(let d of e.sheets||[]){let r=await g(d,e.deleteSubmitted);t+=r;}let s=e.message??f(t),i;return !e.deleteSubmitted&&t>0&&(i=m(t,e.sheets[0].sheetId)),{outcome:{buttonText:"Close",heading:t>0?"Rejected Records":"Success!",acknowledge:!0,...i&&!e.deleteSubmitted&&{next:i},message:s}}}function f(e){return e>0?`During the data submission process, ${e} records were rejected. Please review and correct these records before resubmitting.`:"The data has been successfully submitted without any rejections. This task is now complete."}function m(e,t){return e>0?{type:"id",id:t,label:"View Rejected Records",query:"searchField=submissionStatus&searchValue=rejected"}:void 0}async function g(e,t){let s=e.sheetId;return t||await h(s),await utilCommon.processRecords(s,async(i,d)=>{if(i.length){if(!t){i.forEach(r=>{let n=e.rejectedRecords.find(o=>o.id===r.id);r.values.submissionStatus.value=n?"rejected":"submitted";});try{await a.records.update(s,i);}catch(r){throw console.error("Error updating records:",r),new Error("Error updating records")}}await new Promise(r=>setTimeout(r,250)),i.forEach(r=>{e.rejectedRecords.find(o=>o.id===r.id)?.values.forEach(o=>{r.values[o.field]&&(r.values[o.field].messages=[{type:"error",message:o.message}]);});});try{await a.records.update(s,i);}catch(r){throw console.error("Error updating records:",r),new Error("Error updating records")}}}),t&&await utilCommon.deleteRecords(s,{filter:"valid"}),e.rejectedRecords.length}async function h(e){try{let{data:t}=await a.sheets.get(e);t.config.fields.some(s=>s.key==="submissionStatus")||await a.sheets.addField(t.id,{body:{key:"submissionStatus",label:"Submission Status",type:"enum",readonly:!0,config:{allowCustom:!1,options:[{label:"Rejected",value:"rejected"},{label:"Submitted",value:"submitted"}]}}});}catch(t){throw console.error("Error adding rejection status field:",t),"Error adding rejection status field"}} exports.responseRejectionHandler = R; //# sourceMappingURL=index.cjs.map //# sourceMappingURL=index.cjs.map