import $ from "jquery";

export class InobyFunctions {
  public static wpSendData(action: string, data: object) {
    return $.ajax({
      type: "POST",
      url: window.inoby_vars.ajax.url,
      data: { action: action, nonce: window.inoby_vars.ajax.nonce, ...data },
      dataType: "json",
    });
  }

  public static wpSendForm(action: string, form: HTMLFormElement) {
    const data = Object.fromEntries(new FormData(form).entries());
    form.classList.add("loading");
    return InobyFunctions.wpSendData(action, data).always(() =>
      form.classList.remove("loading")
    );
  }
}
