1import { UnavailabilityError, Platform } from 'expo-modules-core';
2
3import BadgeModule from './BadgeModule';
4import { WebSetBadgeCountOptions } from './BadgeModule.types';
5
6export interface SetBadgeCountOptions {
7  /**
8   * A configuration object described [in the `badgin` documentation](https://github.com/jaulz/badgin#options).
9   */
10  web?: WebSetBadgeCountOptions;
11}
12
13/**
14 * Sets the badge of the app's icon to the specified number. Setting it to `0` clears the badge. On iOS, this method requires that you have requested
15 * the user's permission for `allowBadge` via [`requestPermissionsAsync`](#notificationsrequestpermissionsasyncpermissions),
16 * otherwise it will automatically return `false`.
17 * > **Note:** Not all Android launchers support application badges. If the launcher does not support icon badges, the method will resolve to `false`.
18 * @param badgeCount The count which should appear on the badge. A value of `0` will clear the badge.
19 * @param options An object of options configuring behavior applied in Web environment.
20 * @return It returns a Promise resolving to a boolean representing whether the setting of the badge succeeded.
21 * @header badge
22 */
23export default async function setBadgeCountAsync(
24  badgeCount: number,
25  options?: SetBadgeCountOptions
26): Promise<boolean> {
27  if (!BadgeModule.setBadgeCountAsync) {
28    throw new UnavailabilityError('ExpoNotifications', 'setBadgeCountAsync');
29  }
30
31  return await BadgeModule.setBadgeCountAsync(badgeCount, options?.[Platform.OS]);
32}
33