import { BE_SETTINGS_SMTP_URL } from '@/utils/Url';
import { errorHandler, requestResponseHandler } from './actionsHandlers';

export const PENDING = 'pending';
export const SUBSCRIBED = 'subscribed';
export const ARCHIVED = 'archived';

export const fetchSmtpSettings = (csrfToken, establishment_id = null) => {
  let url = `${BE_SETTINGS_SMTP_URL}/`;
  if (establishment_id) {
    url += `?establishment_id=${establishment_id}`;
  }
  return fetch(`${url}`, {
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};

export const editSmtpSettings = (data, csrfToken) => {
  return fetch(`${BE_SETTINGS_SMTP_URL}/`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'X-CSRFToken': csrfToken,
    },
    body: JSON.stringify(data),
    credentials: 'include',
  })
    .then(requestResponseHandler)
    .catch(errorHandler);
};
