import{r as B,gw as K,cm as V,hn as A,cz as P,bQ as q,b_ as N,ho as G,gM as H,gN as J,hp as W,f_ as X,cd as j,cK as Y,h9 as z,c as Z,cL as ee,eT as Q,cM as ne,fs as te,fr as se,eU as ae}from"./index.DVv6B8Wq.js";function ce({filter:l,options:e}={}){const[t,p]=B.useState([]),{isLoading:r,error:w,schedules:u,statuses:g}=K(),[I,s]=B.useState(r),[d,v]=B.useState(void 0),[i,E]=B.useState(new Map),[x]=V("upcomingScheduledTransactionLength"),S=B.useRef(e);S.current=e;const R=B.useMemo(()=>{if(r)return[];const T=u.filter(n=>oe(n,g)).filter(l||(()=>!0)),m=A(P(q())),y=A(P(N(m,G(x))));return T.map(n=>{const{date:c}=H(n._conditions),f=g.get(n.id),b=J(c),h=[n.next_date];let _=A(P(n.next_date));if(b)for(;_<=y;){const a=W(c,_);if(A(P(a))>y)break;if(h.includes(a)){_=A(P(N(_,1)));continue}h.push(a),_=A(P(N(a,1)))}f==="paid"&&h.shift();const o=[];return h.forEach(a=>{o.push({id:"preview/"+n.id+`/${a}`,payee:n._payee,account:n._account,amount:X(n._amount),date:a,schedule:n.id,forceUpcoming:a!==n.next_date||f==="paid"})}),o}).flat().sort((n,c)=>P(c.date).getTime()-P(n.date).getTime()||n.amount-c.amount)},[l,r,u,g,x]);B.useEffect(()=>{let T=!1;if(v(void 0),R.length===0){s(!1),p([]);return}return s(!0),Promise.all(R.map(m=>j("rules-run",{transaction:m}))).then(m=>{if(!T){const y=m.map(c=>({...c,category:c.schedule!=null?g.get(c.schedule):void 0,schedule:c.schedule,subtransactions:c.subtransactions?.map(f=>({...f,id:"preview/"+f.id,schedule:c.schedule}))})),n=Y(y);p(n),S.current?.calculateRunningBalances&&E(z(n,"all",S.current?.startingBalance)),s(!1)}}).catch(m=>{T||(v(m),s(!1))}),()=>{T=!0}},[R,u,g,x]);const U=d||w;return{previewTransactions:t,runningBalances:i,isLoading:I||r,...U&&{error:U}}}function oe(l,e){const t=e.get(l.id);return!l.completed&&["due","upcoming","missed","paid"].includes(t)}function le(l){const e=Z.c(46),{accountId:t}=l,p=ee();let r;e[0]!==p?(r=Q(p),e[0]=p,e[1]=r):r=e[1];const w=r,u=ne();let g;e[2]!==u?(g=Q(u),e[2]=u,e[3]=g):g=e[3];const I=g;let s;e[4]!==u?(s=o=>u.find(a=>a.transfer_acct===o)||null,e[4]=u,e[5]=s):s=e[5];const d=s;let v;e[6]!==w||e[7]!==I?(v=o=>{if(!o)return null;const a=I[o]?.transfer_acct;return a?w[a]:null},e[6]=w,e[7]=I,e[8]=v):v=e[8];const i=v;let E;e[9]!==t||e[10]!==i?(E=o=>!t||o._account===t||i(o._payee)?.id===t,e[9]=t,e[10]=i,e[11]=E):E=e[11];const x=E;let S;e[12]!==t?(S=t?te(t):se(),e[12]=t,e[13]=S):S=e[13];const R=ae(S),[U]=V(`show-balances-${t}`),T=U==="true",m=R??0;let y;e[14]!==T||e[15]!==m?(y={calculateRunningBalances:T,startingBalance:m},e[14]=T,e[15]=m,e[16]=y):y=e[16];let n;e[17]!==x||e[18]!==y?(n={filter:x,options:y},e[17]=x,e[18]=y,e[19]=n):n=e[19];const{previewTransactions:c,runningBalances:f,isLoading:b,error:h}=ce(n);let _;e:{if(!t){let L;e[20]!==c||e[21]!==f||e[22]!==h||e[23]!==b?(L={previewTransactions:c,runningBalances:f,isLoading:b,error:h},e[20]=c,e[21]=f,e[22]=h,e[23]=b,e[24]=L):L=e[24],_=L;break e}let o,a,D,M;if(e[25]!==R||e[26]!==t||e[27]!==c||e[28]!==f||e[29]!==d||e[30]!==i){const{transactions:L,runningBalances:$}=ue({accountId:t,transactions:c,runningBalances:f,startingBalance:R??0,getPayeeByTransferAccount:d,getTransferAccountByPayee:i});o=L,M=new Set(o.map(re)),D=Map,a=[...$.entries()],e[25]=R,e[26]=t,e[27]=c,e[28]=f,e[29]=d,e[30]=i,e[31]=o,e[32]=a,e[33]=D,e[34]=M}else o=e[31],a=e[32],D=e[33],M=e[34];let C;e[35]!==a||e[36]!==M?(C=a.filter(L=>{const[$]=L;return M.has($)}),e[35]=a,e[36]=M,e[37]=C):C=e[37];let k;e[38]!==D||e[39]!==C?(k=new D(C),e[38]=D,e[39]=C,e[40]=k):k=e[40];const O=k;let F;e[41]!==h||e[42]!==b||e[43]!==o||e[44]!==O?(F={isLoading:b,previewTransactions:o,runningBalances:O,error:h},e[41]=h,e[42]=b,e[43]=o,e[44]=O,e[45]=F):F=e[45],_=F}return _}function re(l){return l.id}function ue({accountId:l,transactions:e,runningBalances:t,startingBalance:p,getPayeeByTransferAccount:r,getTransferAccountByPayee:w}){const u=e.map(s=>{const d=s.account!==l,v=s.subtransactions?.map(i=>({...i,amount:d?-i.amount:i.amount,payee:(d?r(i.account)?.id:i.payee)||"",account:d?w(i.payee)?.id||"":i.account}));return{inversed:d,...s,amount:d?-s.amount:s.amount,payee:(d?r(s.account)?.id:s.payee)||"",account:d?w(s.payee)?.id||"":s.account,...v&&{subtransactions:v}}}),I=u.some(s=>s.inversed)?z(u,"all",p??0):t;return{transactions:u,runningBalances:I}}function de(){const[l,e]=B.useState(new Map),[t,p]=B.useState(!0),r=B.useCallback(async()=>{p(!0);try{const w=await j("payees-get-rule-counts"),u=new Map(Object.entries(w));e(u)}finally{p(!1)}},[]);return B.useEffect(()=>{r()},[r]),{ruleCounts:l,isLoading:t,refetch:r}}export{ce as a,de as b,le as u};
//# sourceMappingURL=usePayeeRuleCounts.ARWFIrSx.chunk.js.map
