import { openNotificationSuccess } from './successMessage';
import { openNotificationError } from './errorMessage';

export const runAsyncWithMessages = async (
  func: () => Promise<void>,
  messages: {
    okMessage: string;
    errorMessage: string;
    okDescription?: string;
    errorDescription?: string;
  },
  callbacks?: {
    onOk?: () => void;
    onError?: () => void;
  },
): Promise<void> => {
  try {
    await func();
    openNotificationSuccess({
      messageTitle: messages.okMessage,
      messageDescription: messages.okDescription,
    });
    if (callbacks?.onOk) {
      callbacks.onOk();
    }
  } catch {
    openNotificationError({
      messageTitle: messages.errorMessage,
      messageDescription: messages.errorDescription,
    });
    if (callbacks?.onError) {
      callbacks.onError();
    }
  }
};
