1import { UnavailabilityError } from 'expo-modules-core';
2
3import NotificationScheduler from './NotificationScheduler';
4import { SchedulableNotificationTriggerInput } from './Notifications.types';
5import { parseTrigger } from './scheduleNotificationAsync';
6
7/**
8 * Allows you to check what will be the next trigger date for given notification trigger input.
9 * @param trigger The schedulable notification trigger you would like to check next trigger date for (of type [`SchedulableNotificationTriggerInput`](#schedulablenotificationtriggerinput)).
10 * @return If the return value is `null`, the notification won't be triggered. Otherwise, the return value is the Unix timestamp in milliseconds
11 * at which the notification will be triggered.
12 * @example Calculate next trigger date for a notification trigger:
13 * ```ts
14 * import * as Notifications from 'expo-notifications';
15 *
16 * async function logNextTriggerDate() {
17 *   try {
18 *     const nextTriggerDate = await Notifications.getNextTriggerDateAsync({
19 *       hour: 9,
20 *       minute: 0,
21 *     });
22 *     console.log(nextTriggerDate === null ? 'No next trigger date' : new Date(nextTriggerDate));
23 *   } catch (e) {
24 *     console.warn(`Couldn't have calculated next trigger date: ${e}`);
25 *   }
26 * }
27 * ```
28 * @header schedule
29 */
30export default async function getNextTriggerDateAsync(
31  trigger: SchedulableNotificationTriggerInput
32): Promise<number | null> {
33  if (!NotificationScheduler.getNextTriggerDateAsync) {
34    throw new UnavailabilityError('ExpoNotifications', 'getNextTriggerDateAsync');
35  }
36
37  return await NotificationScheduler.getNextTriggerDateAsync(parseTrigger(trigger));
38}
39