"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "ListView", {
    enumerable: true,
    get: function() {
        return ListView;
    }
});
const _jsxruntime = require("react/jsx-runtime");
const _react = /*#__PURE__*/ _interop_require_wildcard(require("react"));
const _emptyView = require("./emptyView");
const _css = require("../utils/css");
const _spinner = require("../spinner");
const _useEndReached = require("./useEndReached");
function _getRequireWildcardCache(nodeInterop) {
    if (typeof WeakMap !== "function") return null;
    var cacheBabelInterop = new WeakMap();
    var cacheNodeInterop = new WeakMap();
    return (_getRequireWildcardCache = function(nodeInterop) {
        return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
    })(nodeInterop);
}
function _interop_require_wildcard(obj, nodeInterop) {
    if (!nodeInterop && obj && obj.__esModule) {
        return obj;
    }
    if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
        return {
            default: obj
        };
    }
    var cache = _getRequireWildcardCache(nodeInterop);
    if (cache && cache.has(obj)) {
        return cache.get(obj);
    }
    var newObj = {
        __proto__: null
    };
    var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
    for(var key in obj){
        if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
            var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
            if (desc && (desc.get || desc.set)) {
                Object.defineProperty(newObj, key, desc);
            } else {
                newObj[key] = obj[key];
            }
        }
    }
    newObj.default = obj;
    if (cache) {
        cache.set(obj, newObj);
    }
    return newObj;
}
const ListViewInner = (props, ref)=>{
    const sentinelRef = (0, _react.useRef)(null);
    const containerRef = (0, _react.useRef)(null);
    (0, _useEndReached.useEndReached)(sentinelRef, ()=>{
        if (!props.isLoading) {
            props.loadMore?.();
        }
    });
    const listViewElement = (0, _react.useMemo)(()=>{
        if (!props.dataSource) {
            return null;
        }
        if (Array.isArray(props.dataSource) && props.dataSource.length <= 0) {
            // @ts-ignore
            return props.emptyView || /*#__PURE__*/ (0, _jsxruntime.jsx)(_emptyView.EmptyView, {
                visible: true
            });
        }
        return props.dataSource.map((item, index)=>/*#__PURE__*/ (0, _jsxruntime.jsx)(_react.default.Fragment, {
                children: props.renderItem(item, index, props.extraData)
            }, index));
    }, [
        props.dataSource,
        props.extraData,
        props.emptyView
    ]);
    const loadingViewElement = (0, _react.useMemo)(()=>{
        if (!props.isLoading && !!props.dataSource) {
            return null;
        }
        return /*#__PURE__*/ (0, _jsxruntime.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__*/ (0, _jsxruntime.jsx)(_spinner.Spinner, {})
        });
    }, [
        props.isLoading,
        props.dataSource
    ]);
    (0, _react.useImperativeHandle)(ref, ()=>{
        return {
            scroll: (direction)=>{
                containerRef.current?.scroll({
                    left: direction.x,
                    top: direction.y,
                    behavior: "smooth"
                });
            }
        };
    });
    return /*#__PURE__*/ (0, _jsxruntime.jsxs)("div", {
        style: props.style,
        ref: containerRef,
        className: (0, _css.cn)("orderly-relative orderly-min-h-[180px]", props.className),
        children: [
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                className: (0, _css.cn)("orderly-list-view-inner orderly-space-y-3", props.contentClassName),
                children: listViewElement
            }),
            /*#__PURE__*/ (0, _jsxruntime.jsx)("div", {
                ref: sentinelRef,
                className: "orderly-relative orderly-invisible orderly-h-[1px] orderly-top-[-300px]"
            }),
            loadingViewElement
        ]
    });
};
const ListView = /*#__PURE__*/ (0, _react.forwardRef)(ListViewInner);

//# sourceMappingURL=listView.js.map