1 import { DAILY_TYPE, FREQUENCY, WEEKLY_TYPE, YEARLY_TYPE } from '@proton/shared/lib/calendar/constants';
2 import { getIsRruleCustom, getIsRruleSimple } from '@proton/shared/lib/calendar/recurrence/rrule';
8 } from '@proton/shared/lib/calendar/recurrence/rruleProperties';
9 import { fromUTCDate, toLocalDate } from '@proton/shared/lib/date/timezone';
10 import type { DateTimeModel, FrequencyModel } from '@proton/shared/lib/interfaces/calendar';
11 import type { VcalRruleFreqValue, VcalRruleProperty } from '@proton/shared/lib/interfaces/calendar/VcalModel';
12 import unique from '@proton/utils/unique';
14 const getSafeWeeklyDays = (startDate: Date, byday?: string | string[]) => {
15 const DEFAULT = [startDate.getDay()];
16 const days = getWeeklyDays(byday);
17 return unique([...DEFAULT].concat(days));
20 const getSafeFrequency = (freq: VcalRruleFreqValue): FREQUENCY | undefined => {
24 return Object.values(FREQUENCY).find((value) => value.toLowerCase() === freq.toLowerCase());
27 const getType = (isSimple: boolean, isCustom: boolean, freq: VcalRruleFreqValue) => {
28 if (!isSimple && !isCustom) {
29 return FREQUENCY.OTHER;
32 return FREQUENCY.CUSTOM;
34 return getSafeFrequency(freq) || FREQUENCY.ONCE;
37 const getFrequency = (freq: VcalRruleFreqValue) => {
38 return getSafeFrequency(freq) || FREQUENCY.WEEKLY;
42 * Given a parsed recurrence rule in standard format,
43 * parse it into the object that goes in the Event model
45 export const propertiesToFrequencyModel = (
46 rrule: VcalRruleProperty | undefined,
47 { date: startDate, tzid: startTzid }: DateTimeModel
48 ): FrequencyModel => {
49 const rruleValue = rrule?.value;
50 const { freq, count, interval, until, bysetpos, byday } = rruleValue || {};
51 const isSimple = getIsRruleSimple(rruleValue);
52 const isCustom = !isSimple ? getIsRruleCustom(rruleValue) : false;
53 const type = !rrule ? FREQUENCY.ONCE : getType(isSimple, isCustom, freq);
54 const frequency = getFrequency(freq);
55 const endType = getEndType(count, until);
56 const monthType = getMonthType(byday, bysetpos);
57 const utcUntilDate = getUntilDate(until, startTzid);
58 const untilDate = utcUntilDate ? toLocalDate(fromUTCDate(utcUntilDate)) : undefined;
59 const weeklyDays = getSafeWeeklyDays(startDate, byday);
64 interval: interval || 1, // INTERVAL=1 is ignored when parsing a recurring rule
66 type: DAILY_TYPE.ALL_DAYS,
69 type: WEEKLY_TYPE.ON_DAYS,
76 type: YEARLY_TYPE.BY_MONTH_ON_MONTH_DAY,
83 vcalRruleValue: rruleValue,