import{Fb as U,e as h,m as d,o as w,yb as l,zb as r}from"./chunk-LDMD57YL.js";var p=function(t){return t[t.SERIES=0]="SERIES",t[t.INDIVIDUAL=1]="INDIVIDUAL",t}(p||{});function T(t,e){let I=s=>{switch(s){case l.DAILY:return{Amount:1,Unit:"d"};case l.WEEKLY:return{Amount:1,Unit:"w"};case l.MONTHLY:return{Amount:1,Unit:"M"};case l.QUARTERLY:return{Amount:1,Unit:"Q"};case l.YEARLY:return{Amount:1,Unit:"Y"}}},i=(s,a,f)=>{let n=[],D=I(e.pattern),u=t.dbDateToMomentDate(a);if(s)for(let o=0;o<s;o++)n.push(t.momentToDBDate(u)),u.add(D.Amount,D.Unit);else{let o=t.dbDateToMomentDate(f);for(;u.isSameOrBefore(o);)n.push(t.momentToDBDate(u)),u.add(D.Amount,D.Unit)}return n},P=(s,a,f)=>{let n=(o,A)=>{switch(A){case r.MONDAY:return o.day()===1;case r.TUESDAY:return o.day()===2;case r.WEDNESDAY:return o.day()===3;case r.THURSDAY:return o.day()===4;case r.FRIDAY:return o.day()===5;case r.SATURDAY:return o.day()===6;default:return o.day()===0}};return e.weeklyDays.filter(o=>!!o).reduce((o,A)=>{let c=t.dbDateToMomentDate(a);for(;!n(c,A);)c.add(1,"d");let Y=i(s,t.momentToDBDate(c),f);return o.concat(Y)},[])},m;return e.pattern===l.WEEKLY&&e.weeklyDays.length>0?m=P(e.count,e.startDate,e.endDate):m=i(e.count,e.startDate,e.endDate),m}function R(t,e,I,i,P,m){return h(this,null,function*(){let s=yield i.toPromise();if(t.participants=yield P.toPromise(),s.shouldCreateParent||s.shouldUpdateChildren){let a;if(s.shouldCreateParent){let o=T(I,t.period);t.entryDate=I.dbDateToDate(o[o.length-1]),t.localID?a=e.update(t,U.USER_MANIPULATION).pipe(w(A=>d(t.localID))):(t.isParent=!0,a=e.insert(t,U.USER_MANIPULATION))}else t.isParent=!1,t.localID=t.parentId||t.localID,t.parentId=null,a=e.update(t,U.USER_MANIPULATION);let f=yield a.toPromise();t.localID=f;let n;s.shouldUpdateChildren?(yield e.deleteByParentId(t.parentId||t.localID,m).toPromise(),n=t.parentId||t.localID):n=f;let D=M(I,t,n);D.forEach((o,A)=>{o._id=null,o.version=t.version});let u;return u=yield e.bulkInsert(D,U.USER_MANIPULATION).toPromise(),u.push(t.localID),p.SERIES}else return p.INDIVIDUAL})}function M(t,e,I){let i;return e.period?i=T(t,e.period).map(m=>{let s=e.clone();return s.isPeriodic=e.period.isPeriodical,s.entryDate=t.dbDateToDate(m),s.outlookID=null,s.lastOutlookSync=null,s.parentId=I,s}):i=[e],i}export{p as a,R as b};
