import "core-js/modules/es.promise.js";
import React, { Suspense, lazy, useMemo, useCallback, useEffect, useRef } from 'react';
import Fallback from './fallback';
import './style';
import { jsx as _jsx } from "react/jsx-runtime";
function LazyLoad(props) {
  const {
    loader,
    onLoadStart,
    onLoadEnd,
    onError,
    spin,
    containerStyle,
    containerClassName,
    ...restProps
  } = props;
  const ref = useRef(onError);
  useEffect(() => {
    ref.current = onError;
  }, [onError]);
  const loaderHandle = useCallback(async () => {
    try {
      const lazyImport = await loader();
      return lazyImport;
    } catch (error) {
      if (typeof ref.current === 'function') {
        ref.current(error);
      }
      throw Error;
    }
  }, [loader]);
  const LazyLoadComponent = useMemo(() => /*#__PURE__*/lazy(loaderHandle), [loaderHandle]);
  return /*#__PURE__*/_jsx(Suspense, {
    fallback: /*#__PURE__*/_jsx(Fallback, {
      onLoadStart: onLoadStart,
      onLoadEnd: onLoadEnd,
      spin: spin,
      containerStyle: containerStyle,
      containerClassName: containerClassName
    }),
    children: /*#__PURE__*/_jsx(LazyLoadComponent, {
      ...restProps
    })
  });
}
export default LazyLoad;