import { forwardRef, memo, useRef, useState } from "@lynx-js/react";
import { useGlobalEventListener } from "@lynx-js/lynx-ui-common";
import "./styles.css";
const LazyComponent = memo(forwardRef(LazyComponentImpl));
function LazyComponentImpl(props, _ref) {
    const { pid, scene, estimatedStyle, top = '10px', bottom = '10px', left = '10px', right = '10px', unmountOnExit, unloadable = false, children } = props;
    const shouldUnmountOnExit = unmountOnExit ?? unloadable;
    const [show, setShow] = useState(false);
    const cacheSizeRef = useRef(void 0);
    useGlobalEventListener('exposure', (events)=>{
        'background-only';
        events.forEach((event)=>{
            if (!show && event['exposure-scene'] === scene && event['exposure-id'] === pid) setShow(true);
        });
    }, [
        show
    ]);
    const onLayoutChange = ({ detail })=>{
        if (show) cacheSizeRef.current = {
            width: `${detail.width}px`,
            height: `${detail.height}px`
        };
    };
    useGlobalEventListener('disexposure', (events)=>{
        'background-only';
        if (shouldUnmountOnExit) events.forEach((event)=>{
            if (event['exposure-scene'] === scene && event['exposure-id'] === pid) setShow(false);
        });
    }, [
        shouldUnmountOnExit
    ]);
    if (!shouldUnmountOnExit) return show ? children : <view id='component' className='lynx-ui-lazy-component__min' flatten={false} exposure-screen-margin-top={top} exposure-screen-margin-bottom={bottom} exposure-screen-margin-left={left} exposure-screen-margin-right={right} exposure-id={pid} exposure-scene={scene} style={estimatedStyle}/>;
    {
        const { width, height, ...restsStyles } = estimatedStyle;
        return <view id='component' className='lynx-ui-lazy-component__min' flatten={false} exposure-screen-margin-top={top} exposure-screen-margin-bottom={bottom} exposure-screen-margin-left={left} exposure-screen-margin-right={right} exposure-id={pid} exposure-scene={scene} bindlayoutchange={onLayoutChange} style={show ? {} : {
            width: cacheSizeRef.current?.width ?? width,
            height: cacheSizeRef.current?.height ?? height,
            ...restsStyles
        }}>
        {show && children}
      </view>;
    }
}
export { LazyComponent };

//# sourceMappingURL=index.jsx.map