import { useCallback } from 'react';
import { notifications } from '@mantine/notifications';
import { useTranslation } from 'react-i18next';
import { extractErrorMessage } from '@ema-platform/api';

/**
 * Runs a mutation and reports the outcome once — the same "one path, every
 * action" pattern as the certificate designer's `useDesignerActions`.
 */
export function useRequirementActions() {
  const { t } = useTranslation();

  return useCallback(
    async (action: () => Promise<unknown>, success: string) => {
      try {
        await action();
        notifications.show({ color: 'teal', title: success, message: '' });
        return true;
      } catch (err) {
        notifications.show({
          color: 'red',
          title: t('certReq.actionFailed', 'Action failed'),
          message: extractErrorMessage(err),
        });
        return false;
      }
    },
    [t],
  );
}
