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

/**
 * Runs a designer mutation and reports the outcome once.
 *
 * Every action on this page succeeds or fails the same way, so the toast
 * handling lives here rather than being repeated at each call site.
 */
export function useDesignerActions() {
  const { t } = useTranslation();

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