const procesando = () => {
  return new Fancybox([
    {
      src: '<button class="btn btn-success" type="button" disabled><span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span> Procesando...</button>',
      type: "html",
      closeButton: false,
      width: 640,
      height: 360,
    },
  ]);
};

const xhr = (url, method = "post", formData) => {
  process_lighbox = procesando();
  return fetch(url, {
    method: method,
    body: formData,
    credentials: "same-origin",
    cache: "no-store",
  })
    .then((response) => {
      process_lighbox.close();
      return response.json();
    })
    .then((json) => {
      return json;
    })
    .catch((error) => console.warn(error));
};

const sendForm = (formObj) => {
  let url = formObj.getAttribute("action");
  let method = formObj.getAttribute("method");
  let formData = new FormData(formObj);
  let result = xhr(url, method, formData);
  return result;
};
