1 import type { NotificationModel } from '../../interfaces/calendar/Notification';
2 import { NOTIFICATION_UNITS, NOTIFICATION_WHEN } from '../constants';
3 import { transformBeforeAt } from './trigger';
5 const getValarmTriggerUnit = (unit: NOTIFICATION_UNITS) => {
8 [NOTIFICATION_UNITS.WEEK]: 'weeks',
9 [NOTIFICATION_UNITS.DAY]: 'days',
10 [NOTIFICATION_UNITS.HOUR]: 'hours',
11 [NOTIFICATION_UNITS.MINUTE]: 'minutes',
16 const getAllDayValarmTrigger = ({
23 unit: NOTIFICATION_UNITS;
27 const modifiedAt = isNegative ? transformBeforeAt(at) : at;
29 const hours = modifiedAt.getHours();
30 const minutes = modifiedAt.getMinutes();
32 const modifyNegativeDay = isNegative && (minutes > 0 || hours > 0);
34 const [weeks, days] = (() => {
35 const weeksValue = unit === NOTIFICATION_UNITS.WEEK ? value : 0;
36 const daysValue = unit === NOTIFICATION_UNITS.DAY ? value : 0;
38 if (modifyNegativeDay && weeksValue === 0) {
39 return [0, daysValue - 1];
41 if (modifyNegativeDay && weeksValue >= 1) {
42 return [weeksValue - 1, 6];
44 return [weeksValue, daysValue];
48 weeks: Math.max(0, weeks),
49 days: Math.max(0, days),
57 const getPartDayValarmTrigger = ({
63 unit: NOTIFICATION_UNITS;
72 [getValarmTriggerUnit(unit)]: value,
77 export const getValarmTrigger = ({ isAllDay, unit, when, value, at }: NotificationModel) => {
78 const isNegative = when === NOTIFICATION_WHEN.BEFORE;
81 throw new Error('Missing at');
83 return getAllDayValarmTrigger({ isNegative, unit, value, at });
85 return getPartDayValarmTrigger({ isNegative, unit, value });