import React from "react";
import { createLogger } from "../../lib/logger/index.js";
const logger = createLogger("dashboard").child({ component: "widgets" });
function lazyDashboardWidget(loader) {
  let cached = null;
  let pending = null;
  const load = () => {
    if (cached) return Promise.resolve();
    if (!pending) {
      pending = loader().then((mod) => {
        cached = mod.default ?? mod;
      }).catch((err) => {
        pending = null;
        throw err;
      });
    }
    return pending;
  };
  const LazyWidget = (props) => {
    const [, setTick] = React.useState(0);
    React.useEffect(() => {
      let cancelled = false;
      void load().then(() => {
        if (!cancelled) setTick((value) => value + 1);
      }).catch((err) => {
        if (!cancelled) {
          try {
            logger.error("Failed to load dashboard widget component", { err });
          } catch {
          }
          setTick((value) => value + 1);
        }
      });
      return () => {
        cancelled = true;
      };
    }, []);
    if (!cached) return null;
    return React.createElement(cached, props);
  };
  return LazyWidget;
}
export {
  lazyDashboardWidget
};
//# sourceMappingURL=widgets.js.map
