import emitter from './notification-emmiter';
import { NOTIFICATION_LEVEL, EVENTS } from './types';
function showNotification(message, level, options) {
  const notification = {
    ...options,
    message,
    level
  };
  if (options?.retry) {
    notification.title = options.title;
    notification.action = options.retry;
  }
  // Using 'any' since there are no existing type patterns
  emitter.emit(EVENTS.NOTIFICATION_SHOW, notification);
}
class NotificationServiceCreator {
  subscribe(event, callback) {
    this.subscriptionCallback = callback;
    emitter.on(event, callback);
    return {
      cancel: () => emitter.off(event, callback)
    };
  }
  info(message, options) {
    return showNotification(message, NOTIFICATION_LEVEL.INFO, options);
  }
  success(message, options) {
    return showNotification(message, NOTIFICATION_LEVEL.SUCCESS, options);
  }
  warn(message, options) {
    return showNotification(message, NOTIFICATION_LEVEL.WARNING, options);
  }
  error(message, options) {
    return showNotification(message, NOTIFICATION_LEVEL.FAILURE, options);
  }
  block() {
    if (this.subscriptionCallback) {
      emitter.off(EVENTS.NOTIFICATION_SHOW, this.subscriptionCallback);
    }
  }
  unblock() {
    if (this.subscriptionCallback) {
      emitter.on(EVENTS.NOTIFICATION_SHOW, this.subscriptionCallback);
    }
  }

  /**
   *dismiss all shown notifications at once
   */
  dismissAll() {
    // Using 'any' since there are no existing type patterns
    emitter.emit(EVENTS.NOTIFICATIONS_DISMISS_ALL);
  }
}
const NotificationService = new NotificationServiceCreator();
export { NotificationService, EVENTS };