1 import unique from '@proton/utils/unique';
3 import { convertUTCDateTimeToZone, fromUTCDate } from '../../date/timezone';
4 import type { VcalDateOrDateTimeValue, VcalDateTimeValue, VcalDays } from '../../interfaces/calendar/VcalModel';
5 import { END_TYPE, MONTHLY_TYPE } from '../constants';
6 import { dayToNumericDay, propertyToUTCDate } from '../vcalConverter';
7 import { getDayAndSetpos, getIsStandardBydayArray } from './rrule';
9 export const getEndType = (count?: number, until?: VcalDateOrDateTimeValue) => {
10 // count and until cannot occur at the same time (see https://tools.ietf.org/html/rfc5545#page-37)
11 if (count && count >= 1) {
12 return END_TYPE.AFTER_N_TIMES;
15 return END_TYPE.UNTIL;
17 return END_TYPE.NEVER;
20 export const getMonthType = (byday?: string | string[], bysetpos?: number | number[]) => {
21 if (typeof byday === 'string' && !Array.isArray(bysetpos)) {
22 const { setpos } = getDayAndSetpos(byday, bysetpos);
24 return setpos > 0 ? MONTHLY_TYPE.ON_NTH_DAY : MONTHLY_TYPE.ON_MINUS_NTH_DAY;
27 return MONTHLY_TYPE.ON_MONTH_DAY;
30 export const getUntilDate = (until?: VcalDateOrDateTimeValue, startTzid?: string) => {
34 if (!(until as VcalDateTimeValue).isUTC || !startTzid) {
35 const { year, month, day } = until;
36 return new Date(Date.UTC(year, month - 1, day));
38 const utcDate = propertyToUTCDate({ value: until as VcalDateTimeValue });
39 const localDate = convertUTCDateTimeToZone(fromUTCDate(utcDate), startTzid);
40 return new Date(Date.UTC(localDate.year, localDate.month - 1, localDate.day));
43 export const getWeeklyDays = (byday?: string | string[]) => {
44 if (byday === undefined) {
47 const bydayArray = Array.isArray(byday) ? byday : [byday];
48 if (!getIsStandardBydayArray(bydayArray)) {
51 const bydayArraySafe = bydayArray.map(dayToNumericDay).filter((value): value is VcalDays => value !== undefined);
52 // Ensure the start date is included in the list
53 return unique(bydayArraySafe);