window.appAuthPostJson = async function (url, body) {
  let response;
  try {
    response = await fetch(url, {
      method: "POST",
      credentials: "same-origin",
      headers: {
        "Content-Type": "application/json"
      },
      body: JSON.stringify(body)
    });
  } catch (error) {
    return {
      ok: false,
      status: 0,
      data: {
        error: "No se ha podido conectar con el servidor de la aplicacion. " + (error?.message ?? "")
      }
    };
  }

  let data = null;
  try {
    data = await response.json();
  } catch {
    data = null;
  }

  return {
    ok: response.ok,
    status: response.status,
    data
  };
};

window.appAuthPost = async function (url) {
  let response;
  try {
    response = await fetch(url, {
      method: "POST",
      credentials: "same-origin"
    });
  } catch (error) {
    return {
      ok: false,
      status: 0,
      data: {
        error: "No se ha podido conectar con el servidor de la aplicacion. " + (error?.message ?? "")
      }
    };
  }

  let data = null;
  try {
    data = await response.json();
  } catch {
    data = null;
  }

  return {
    ok: response.ok,
    status: response.status,
    data
  };
};

window.appSetBodyScrollLock = function (locked) {
  document.documentElement.classList.toggle("app-scroll-locked", Boolean(locked));
  document.body.classList.toggle("app-scroll-locked", Boolean(locked));
};
