import { lazy, createElement } from 'react';
function handleChunkLoadError(error) {
    if (!error.message.includes('Failed to fetch dynamically')) {
        throw error;
    }
    const lastReloadTime = Number(localStorage.getItem('lastLocationReload') || '0');
    if (Date.now() - Number(lastReloadTime) >= 60000) {
        window.location.reload();
        localStorage.setItem('lastLocationReload', Date.now().toString());
    }
}
export function loadable(factory, timeout) {
    const Component = lazy(async () => {
        try {
            return await factory();
        }
        catch (error) {
            handleChunkLoadError(error);
        }
        return {
            default: () => createElement('div', null, null),
        };
    });
    Component.loaded = false;
    Component.load = async (toggle, action) => {
        if (Component.loaded) {
            action?.();
            return;
        }
        toggle?.();
        try {
            await factory();
        }
        catch (error) {
            handleChunkLoadError(error);
        }
        toggle?.();
        Component.loaded = true;
        action?.();
    };
    if (timeout) {
        setTimeout(() => {
            if (!Component.loaded) {
                void Component.load();
            }
        }, timeout);
    }
    return Component;
}
//# sourceMappingURL=loadable.js.map