import * as __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__ from "react/jsx-runtime";
import * as __WEBPACK_EXTERNAL_MODULE_react__ from "react";
import * as __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__ from "@tachybase/utils/client";
import * as __WEBPACK_EXTERNAL_MODULE_ahooks__ from "ahooks";
import * as __WEBPACK_EXTERNAL_MODULE_antd__ from "antd";
import * as __WEBPACK_EXTERNAL_MODULE_react_i18next__ from "react-i18next";
import * as __WEBPACK_EXTERNAL_MODULE__style_mjs__ from "./style.mjs";
function isWindow(element) {
    return element === window;
}
const defaultProps = {
    threshold: 250,
    children: (hasMore, failed, retry)=>/*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(InfiniteScrollContent, {
            hasMore: hasMore,
            failed: failed,
            retry: retry
        })
};
const InfiniteScroll = (param)=>{
    let { threshold = defaultProps.threshold, loadMore, children = defaultProps.children, hasMore } = param;
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__["default"])();
    const [failed, setFailed] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)(false);
    const doLoadMore = (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useLockFn)(async (isRetry)=>{
        try {
            await loadMore(isRetry);
        } catch (e) {
            setFailed(true);
            throw e;
        }
    });
    const elementRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(null);
    const [flag, setFlag] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)({});
    const nextFlagRef = (0, __WEBPACK_EXTERNAL_MODULE_react__.useRef)(flag);
    const [scrollParent, setScrollParent] = (0, __WEBPACK_EXTERNAL_MODULE_react__.useState)();
    const { run: check } = (0, __WEBPACK_EXTERNAL_MODULE_ahooks__.useThrottleFn)(async ()=>{
        if (nextFlagRef.current !== flag) return;
        if (!hasMore) return;
        const element = elementRef.current;
        if (!element) return;
        if (!element.offsetParent) return;
        const parent = (0, __WEBPACK_EXTERNAL_MODULE__tachybase_utils_client__.getScrollParent)(element);
        setScrollParent(parent);
        if (!parent) return;
        const rect = element.getBoundingClientRect();
        const elementTop = rect.top;
        const current = isWindow(parent) ? window.innerHeight : parent.getBoundingClientRect().bottom;
        if (current >= elementTop - threshold) {
            const nextFlag = {};
            nextFlagRef.current = nextFlag;
            try {
                await doLoadMore(false);
                setFlag(nextFlag);
            } catch (e) {
                console.error(e);
            }
        }
    }, {
        wait: 100,
        leading: true,
        trailing: true
    });
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        check();
    });
    (0, __WEBPACK_EXTERNAL_MODULE_react__.useEffect)(()=>{
        const element = elementRef.current;
        if (!element) return;
        if (!scrollParent) return;
        function onScroll() {
            check();
        }
        scrollParent.addEventListener('scroll', onScroll);
        return ()=>{
            scrollParent.removeEventListener('scroll', onScroll);
        };
    }, [
        scrollParent
    ]);
    async function retry() {
        setFailed(false);
        try {
            await doLoadMore(true);
            setFlag(nextFlagRef.current);
        } catch (e) {
            console.error(e);
        }
    }
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("div", {
        className: styles.infiniteScroll,
        ref: elementRef,
        children: 'function' == typeof children ? children(hasMore, failed, retry) : children
    });
};
const InfiniteScrollContent = (props)=>{
    const { t } = (0, __WEBPACK_EXTERNAL_MODULE_react_i18next__.useTranslation)();
    const { styles } = (0, __WEBPACK_EXTERNAL_MODULE__style_mjs__["default"])();
    if (!props.hasMore) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
        children: t('no more')
    });
    if (props.failed) return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)("span", {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                className: "".concat(styles.infiniteScroll, "-failed-text"),
                children: t('failed to load')
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("a", {
                onClick: ()=>{
                    props.retry();
                },
                children: t('retry')
            })
        ]
    });
    return /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsxs)(__WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.Fragment, {
        children: [
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)("span", {
                children: t('loading')
            }),
            /*#__PURE__*/ (0, __WEBPACK_EXTERNAL_MODULE_react_jsx_runtime__.jsx)(__WEBPACK_EXTERNAL_MODULE_antd__.Spin, {})
        ]
    });
};
export { InfiniteScroll };
