import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import React, { forwardRef, useImperativeHandle } from "react";
import { useMemo, useRef } from "react";
import { EmptyView } from "./emptyView";
import { cn } from "../utils/css";
import { Spinner } from "../spinner";
import { useEndReached } from "./useEndReached";
const ListViewInner = (props, ref)=>{
    const sentinelRef = useRef(null);
    const containerRef = useRef(null);
    useEndReached(sentinelRef, ()=>{
        if (!props.isLoading) {
            props.loadMore?.();
        }
    });
    const listViewElement = useMemo(()=>{
        if (!props.dataSource) {
            return null;
        }
        if (Array.isArray(props.dataSource) && props.dataSource.length <= 0) {
            // @ts-ignore
            return props.emptyView || /*#__PURE__*/ _jsx(EmptyView, {
                visible: true
            });
        }
        return props.dataSource.map((item, index)=>/*#__PURE__*/ _jsx(React.Fragment, {
                children: props.renderItem(item, index, props.extraData)
            }, index));
    }, [
        props.dataSource,
        props.extraData,
        props.emptyView
    ]);
    const loadingViewElement = useMemo(()=>{
        if (!props.isLoading && !!props.dataSource) {
            return null;
        }
        return /*#__PURE__*/ _jsx("div", {
            className: "orderly-absolute orderly-w-full orderly-h-full orderly-z-20 orderly-left-0 orderly-top-0 orderly-bottom-0 orderly-right-0 orderly-flex orderly-justify-center orderly-items-center",
            children: /*#__PURE__*/ _jsx(Spinner, {})
        });
    }, [
        props.isLoading,
        props.dataSource
    ]);
    useImperativeHandle(ref, ()=>{
        return {
            scroll: (direction)=>{
                containerRef.current?.scroll({
                    left: direction.x,
                    top: direction.y,
                    behavior: "smooth"
                });
            }
        };
    });
    return /*#__PURE__*/ _jsxs("div", {
        style: props.style,
        ref: containerRef,
        className: cn("orderly-relative orderly-min-h-[180px]", props.className),
        children: [
            /*#__PURE__*/ _jsx("div", {
                className: cn("orderly-list-view-inner orderly-space-y-3", props.contentClassName),
                children: listViewElement
            }),
            /*#__PURE__*/ _jsx("div", {
                ref: sentinelRef,
                className: "orderly-relative orderly-invisible orderly-h-[1px] orderly-top-[-300px]"
            }),
            loadingViewElement
        ]
    });
};
export const ListView = /*#__PURE__*/ forwardRef(ListViewInner);

//# sourceMappingURL=listView.js.map