import 'package:flutter/material.dart';

import 'package:pweb/generated/i18n/app_localizations.dart';
import 'package:pweb/utils/snackbar.dart';
import 'package:pweb/widgets/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 sm = ScaffoldMessenger.of(context);
  final locs = AppLocalizations.of(context)!;
  try {
    final res = await operation();
    if (operationSuccess != null) {
      notifyUserX(sm, operationSuccess);
    }
    if (onSuccess != null) {
      onSuccess(res);
    }
  } catch (e) {
    notifyUserOfErrorX(
      scaffoldMessenger: sm, 
      errorSituation: operationError ?? locs.errorInternalError, 
      exception: e, 
      appLocalizations: locs,
    );
    if (onError != null) {
      onError(e);
    }
    rethrow;
  }
}
