/*
 * Copyright (c) 2023-2025 Datalayer, Inc.
 * Distributed under the terms of the Modified BSD License.
 */
import { createElement, forwardRef, lazy as reactLazy, } from 'react';
export function lazyWithPreload(factory) {
    const LazyComponent = reactLazy(factory);
    let factoryPromise;
    let LoadedComponent;
    const Component = forwardRef(function LazyWithPreload(props, ref) {
        return createElement(LoadedComponent ?? LazyComponent, Object.assign(ref ? { ref } : {}, props));
    });
    Component.preload = () => {
        if (!factoryPromise) {
            factoryPromise = factory().then(module => {
                LoadedComponent = module.default;
            });
        }
        return factoryPromise;
    };
    return Component;
}
export default lazyWithPreload;
