import type {Client} from 'sentry/api';
import {t, tct} from 'sentry/locale';
import type {SentryFunction} from 'sentry/types/integrations';
import type {Organization} from 'sentry/types/organization';

import {
  addErrorMessage,
  addLoadingMessage,
  addSuccessMessage,
  clearIndicators,
} from './indicator';

export async function removeSentryFunction(
  client: Client,
  org: Organization,
  sentryFn: SentryFunction
) {
  addLoadingMessage();
  try {
    await client.requestPromise(
      `/organizations/${org.slug}/functions/${sentryFn.slug}/`,
      {
        method: 'DELETE',
      }
    );
    addSuccessMessage(tct('[name] successfully deleted.', {name: sentryFn.name}));
    return true;
  } catch (err) {
    clearIndicators();
    addErrorMessage(err?.responseJSON?.detail || t('Unknown Error'));
    return false;
  }
}
