import { h, render } from "atomico";
import "./six-loading";

let loading = false;

export const execWithSixLoad = async <T extends unknown> (callback: () => Promise<T>): Promise<T|undefined> => {
  if (loading) {
    return;
  }
  const container = document.createElement("div");
  document.body.appendChild(container);

  loading = true;
  render(<host><six-loading /></host> as unknown as Vdom<"host", any>, container);

  try {
    const r = await callback() as unknown as T;
    // eslint-disable-next-line consistent-return
    return r;
  } catch (e) {
    // toastrError(String(e));
  } finally {
    document.body.removeChild(container);
    loading = false;
  }
};
