import 'package:flutter/material.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';
import 'package:pweb/utils/snackbar.dart';
import 'package:pweb/utils/error/snackbar.dart';


Future<void> invokeAndNotify<T>(
  BuildContext context, {
  required Future<T> Function() operation,
  String? operationSuccess,
  String? operationError,
  void Function(Object)? onError,
  void Function(T)? onSuccess,
}) async {
  final locs = AppLocalizations.of(context)!;
  try {
    final res = await operation();
    if (operationSuccess != null) {
      await notifyUser(context, operationSuccess);
    }
    if (onSuccess != null) {
      onSuccess(res);
    }
  } catch (e) {
    notifyUserOfErrorX(
      context: context,
      errorSituation: operationError ?? locs.errorInternalError, 
      exception: e, 
      appLocalizations: locs,
    );
    if (onError != null) {
      onError(e);
    }
    rethrow;
  }
}
