1 import { END_TYPE, FREQUENCY, MONTHLY_TYPE } from '@proton/shared/lib/calendar/constants';
2 import { getNegativeSetpos, getPositiveSetpos } from '@proton/shared/lib/calendar/recurrence/rrule';
3 import { getUntilProperty, numericDayToDay } from '@proton/shared/lib/calendar/vcalConverter';
4 import { fromLocalDate, toUTCDate } from '@proton/shared/lib/date/timezone';
5 import type { EventModel } from '@proton/shared/lib/interfaces/calendar';
6 import type { VcalRruleProperty } from '@proton/shared/lib/interfaces/calendar/VcalModel';
7 import unique from '@proton/utils/unique';
9 const modelToFrequencyProperties = ({ frequencyModel, start, isAllDay, isAttendee }: EventModel) => {
10 const { type, frequency, interval, weekly, monthly, ends, vcalRruleValue } = frequencyModel;
11 const { date: startDate, tzid } = start;
13 if ((isAttendee || type === FREQUENCY.OTHER) && vcalRruleValue) {
16 value: vcalRruleValue,
21 if ([FREQUENCY.DAILY, FREQUENCY.WEEKLY, FREQUENCY.MONTHLY, FREQUENCY.YEARLY].includes(type)) {
23 value: { freq: type },
30 if (type === FREQUENCY.CUSTOM) {
31 if (ends.type === END_TYPE.AFTER_N_TIMES && ends.count && ends.count < 1) {
38 } as VcalRruleProperty;
40 if (interval && interval > 1) {
41 rrule.value.interval = interval;
43 if (frequency === FREQUENCY.WEEKLY) {
44 // weekly.days may include repeated days (cf. function getFrequencyModelChange)
45 const weeklyDays = unique(weekly.days).sort();
46 if (!weeklyDays.length || !weeklyDays.includes(startDate.getDay())) {
47 throw new Error('Inconsistent weekly rrule');
49 if (weeklyDays.length > 1) {
50 rrule.value.byday = weeklyDays.map(numericDayToDay);
53 if (frequency === FREQUENCY.MONTHLY) {
54 const startFakeUtcDate = toUTCDate(fromLocalDate(startDate));
55 if (monthly.type === MONTHLY_TYPE.ON_NTH_DAY) {
56 rrule.value.byday = numericDayToDay(startFakeUtcDate.getUTCDay());
57 rrule.value.bysetpos = getPositiveSetpos(startFakeUtcDate);
59 if (monthly.type === MONTHLY_TYPE.ON_MINUS_NTH_DAY) {
60 rrule.value.byday = numericDayToDay(startFakeUtcDate.getUTCDay());
61 rrule.value.bysetpos = getNegativeSetpos(startFakeUtcDate);
64 if (frequency === FREQUENCY.YEARLY) {
65 // rrule.value.bymonthday = startDate.getDate();
66 // rrule.value.bymonth = startDate.getMonth() + 1;
68 if (ends.type === END_TYPE.AFTER_N_TIMES) {
69 rrule.value.count = ends.count;
71 if (ends.type === END_TYPE.UNTIL && ends.until) {
72 rrule.value.until = getUntilProperty(fromLocalDate(ends.until), isAllDay, tzid);
80 export default modelToFrequencyProperties;