"use strict";
var __webpack_require__ = {};
(()=>{
    __webpack_require__.n = function(module) {
        var getter = module && module.__esModule ? function() {
            return module['default'];
        } : function() {
            return module;
        };
        __webpack_require__.d(getter, {
            a: getter
        });
        return getter;
    };
})();
(()=>{
    __webpack_require__.d = function(exports1, definition) {
        for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
            enumerable: true,
            get: definition[key]
        });
    };
})();
(()=>{
    __webpack_require__.o = function(obj, prop) {
        return Object.prototype.hasOwnProperty.call(obj, prop);
    };
})();
(()=>{
    __webpack_require__.r = function(exports1) {
        if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
            value: 'Module'
        });
        Object.defineProperty(exports1, '__esModule', {
            value: true
        });
    };
})();
var __webpack_exports__ = {};
__webpack_require__.r(__webpack_exports__);
__webpack_require__.d(__webpack_exports__, {
    InfiniteScroll: ()=>InfiniteScroll
});
const jsx_runtime_namespaceObject = require("react/jsx-runtime");
const external_react_namespaceObject = require("react");
const client_namespaceObject = require("@tachybase/utils/client");
const external_ahooks_namespaceObject = require("ahooks");
const external_antd_namespaceObject = require("antd");
const external_react_i18next_namespaceObject = require("react-i18next");
const external_style_js_namespaceObject = require("./style.js");
var external_style_js_default = /*#__PURE__*/ __webpack_require__.n(external_style_js_namespaceObject);
function isWindow(element) {
    return element === window;
}
const defaultProps = {
    threshold: 250,
    children: (hasMore, failed, retry)=>/*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(InfiniteScrollContent, {
            hasMore: hasMore,
            failed: failed,
            retry: retry
        })
};
const InfiniteScroll = (param)=>{
    let { threshold = defaultProps.threshold, loadMore, children = defaultProps.children, hasMore } = param;
    const { styles } = external_style_js_default()();
    const [failed, setFailed] = (0, external_react_namespaceObject.useState)(false);
    const doLoadMore = (0, external_ahooks_namespaceObject.useLockFn)(async (isRetry)=>{
        try {
            await loadMore(isRetry);
        } catch (e) {
            setFailed(true);
            throw e;
        }
    });
    const elementRef = (0, external_react_namespaceObject.useRef)(null);
    const [flag, setFlag] = (0, external_react_namespaceObject.useState)({});
    const nextFlagRef = (0, external_react_namespaceObject.useRef)(flag);
    const [scrollParent, setScrollParent] = (0, external_react_namespaceObject.useState)();
    const { run: check } = (0, external_ahooks_namespaceObject.useThrottleFn)(async ()=>{
        if (nextFlagRef.current !== flag) return;
        if (!hasMore) return;
        const element = elementRef.current;
        if (!element) return;
        if (!element.offsetParent) return;
        const parent = (0, client_namespaceObject.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, external_react_namespaceObject.useEffect)(()=>{
        check();
    });
    (0, external_react_namespaceObject.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, jsx_runtime_namespaceObject.jsx)("div", {
        className: styles.infiniteScroll,
        ref: elementRef,
        children: 'function' == typeof children ? children(hasMore, failed, retry) : children
    });
};
const InfiniteScrollContent = (props)=>{
    const { t } = (0, external_react_i18next_namespaceObject.useTranslation)();
    const { styles } = external_style_js_default()();
    if (!props.hasMore) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
        children: t('no more')
    });
    if (props.failed) return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)("span", {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                className: "".concat(styles.infiniteScroll, "-failed-text"),
                children: t('failed to load')
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("a", {
                onClick: ()=>{
                    props.retry();
                },
                children: t('retry')
            })
        ]
    });
    return /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsxs)(jsx_runtime_namespaceObject.Fragment, {
        children: [
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)("span", {
                children: t('loading')
            }),
            /*#__PURE__*/ (0, jsx_runtime_namespaceObject.jsx)(external_antd_namespaceObject.Spin, {})
        ]
    });
};
var __webpack_export_target__ = exports;
for(var __webpack_i__ in __webpack_exports__)__webpack_export_target__[__webpack_i__] = __webpack_exports__[__webpack_i__];
if (__webpack_exports__.__esModule) Object.defineProperty(__webpack_export_target__, '__esModule', {
    value: true
});
