import{c as H,cl as K,bF as N,cg as D,ci as A,cK as q,e4 as S,cd as E,hq as Q,h0 as U,h1 as I,bS as L,hr as Y,h2 as G,gY as J}from"./index.DVv6B8Wq.js";function O(t,n){return!!([t,n].every(g=>g.transfer_id==null)&&t.account!==n.account&&t.amount+n.amount===0)}function ot(){const t=H.c(18),n=K(),{t:g}=N();let w;t[0]!==n?(w=async $=>{const{name:c,ids:i,onSuccess:r}=$,{data:u}=await D(A("transactions").filter({id:{$oneof:i}}).select("*").options({splits:"grouped"})),p=q(u),s=async(a,e,o)=>{let M=p;e=e===null?"":e;const h={added:[],deleted:[],updated:[]};a==="cleared"&&(e=!!M.find(nt));const z=new Set(i);M.forEach(y=>{if(a==="cleared"&&y.reconciled||!z.has(y.id))return;let B=e;a==="notes"&&(o==="prepend"?B=y.notes===null?e:`${e}${y.notes}`:o==="append"?B=y.notes===null?e:`${y.notes}${e}`:o==="replace"?B=e:o==="findAndReplace"&&typeof e=="object"&&"useRegex"in e&&(B=Q(y.notes,e.find,e.replace,e.useRegex)));const P={...y,[a]:B};a==="account"&&y.account!==e&&(P.reconciled=!1);const{diff:_}=U(M,P);M=I(_,M),h.deleted=h.deleted?h.deleted.concat(_.deleted):_.deleted,h.updated=h.updated?h.updated.concat(_.updated):_.updated,h.added=h.added?h.added.concat(_.added):_.added}),await E("transactions-batch-update",h),r?.(i,a,e,o)},l=()=>{n(S({modal:{name:"payee-autocomplete",options:{onSelect:a=>s(c,a)}}}))},d=()=>{n(S({modal:{name:"account-autocomplete",options:{onSelect:a=>s(c,a)}}}))},f=()=>{c!=="date"&&c!=="amount"&&c!=="notes"||n(S({modal:{name:"edit-field",options:{name:c,onSubmit:(a,e,o)=>s(a,e,o)}}}))},m=()=>{const a=p[0]?.date?L(p[0]?.date):null,e=a&&p.every(o=>L(o.date)===a);n(S({modal:{name:"category-autocomplete",options:{month:e?a:void 0,onSelect:o=>s(c,o)}}}))};if((c==="amount"||c==="payee"||c==="account"||c==="date")&&p.filter(et).length>0){n(S({modal:{name:"confirm-transaction-edit",options:{onConfirm:()=>{c==="payee"?l():c==="account"?d():f()},confirmReason:"batchEditWithReconciled"}}}));return}c==="cleared"?s("cleared",null):c==="category"?m():c==="payee"?l():c==="account"?d():f()},t[0]=n,t[1]=w):w=t[1];const x=w;let T,b,C,R,F;if(t[2]!==n||t[3]!==g){b=async i=>{const{ids:r,onSuccess:u}=i;await c(r,"batchDuplicateWithReconciled",async s=>{const{data:l}=await D(A("transactions").filter({id:{$oneof:s}}).select("*").options({splits:"grouped"})),f={added:l.reduce(tt,[])};await E("transactions-batch-update",f),u?.(s)})},T=async i=>{const{ids:r,onSuccess:u}=i;await c(r,"batchDeleteWithReconciled",s=>{n(S({modal:{name:"confirm-delete",options:{message:s.length>1?g("Are you sure you want to delete these {{count}} transactions?",{count:s.length}):g("Are you sure you want to delete the transaction?"),onConfirm:async()=>{const{data:l}=await D(A("transactions").filter({id:{$oneof:s}}).select("*").options({splits:"grouped"}));let d=q(l);const f=new Set(s),m={added:[],deleted:[],updated:[]};d.forEach(a=>{const e=a.parent_id;if(!f.has(a.id)||e&&f.has(e))return;const{diff:o}=Y(d,a.id);d=I(o,d),m.deleted=o.deleted?m.deleted.concat(o.deleted):o.deleted,m.updated=o.updated?m.updated.concat(o.updated):o.updated}),await E("transactions-batch-update",m),u?.(s)}}}}))})};let $;t[9]!==n?($=async i=>{const{ids:r,account:u,onSuccess:p}=i,{data:s}=await D(A("transactions").filter({id:{$oneof:r}}).select("*").options({splits:"grouped"}));n(S({modal:{name:"schedule-link",options:{transactionIds:r,getTransaction:l=>s.find(d=>d.id===l),accountName:u?.name??"",onScheduleLinked:l=>{p?.(r,l)}}}}))},t[9]=n,t[10]=$):$=t[10],C=$,R=Z;const c=async(i,r,u)=>{const{data:p}=await D(A("transactions").filter({id:{$oneof:i},reconciled:!0}).select("*").options({splits:"grouped"}));q(p).length>0?n(S({modal:{name:"confirm-transaction-edit",options:{onConfirm:()=>{u(i)},confirmReason:r}}})):u(i)};F=async(i,r,u)=>{await c(i,"batchEditWithReconciled",async s=>{const{data:l}=await D(A("transactions").filter({id:{$oneof:s}}).select("*")),[d,f]=l;if(l.length===2&&O(d,f)){const m=r.find(o=>o.transfer_acct===d.account),a=r.find(o=>o.transfer_acct===f.account),e={updated:[{...d,category:null,payee:a?.id,transfer_id:f.id},{...f,category:null,payee:m?.id,transfer_id:d.id}],runTransfers:!1};await E("transactions-batch-update",e)}u?.(s)})},t[2]=n,t[3]=g,t[4]=T,t[5]=b,t[6]=C,t[7]=R,t[8]=F}else T=t[4],b=t[5],C=t[6],R=t[7],F=t[8];const W=F,j=V;let k;return t[11]!==T||t[12]!==b||t[13]!==x||t[14]!==C||t[15]!==R||t[16]!==W?(k={onBatchEdit:x,onBatchDuplicate:b,onBatchDelete:T,onBatchLinkSchedule:C,onBatchUnlinkSchedule:R,onSetTransfer:W,onMerge:j},t[11]=T,t[12]=b,t[13]=x,t[14]=C,t[15]=R,t[16]=W,t[17]=k):k=t[17],k}async function V(t,n){await E("transactions-merge",t.map(X)),n()}function X(t){return{id:t}}async function Z(t){const{ids:n,onSuccess:g}=t,w={updated:n.map(v)};await E("transactions-batch-update",w),g?.(n)}function v(t){return{id:t,schedule:null}}function tt(t,n){return t.concat(G(J(n)))}function et(t){return t.reconciled}function nt(t){return!t.cleared}export{ot as u,O as v};
//# sourceMappingURL=useTransactionBatchActions.Ddm6PcCQ.chunk.js.map
