function _define_property(obj, key, value) {
    if (key in obj) {
        Object.defineProperty(obj, key, {
            value: value,
            enumerable: true,
            configurable: true,
            writable: true
        });
    } else {
        obj[key] = value;
    }
    return obj;
}
function _object_spread(target) {
    for(var i = 1; i < arguments.length; i++){
        var source = arguments[i] != null ? arguments[i] : {};
        var ownKeys = Object.keys(source);
        if (typeof Object.getOwnPropertySymbols === "function") {
            ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
                return Object.getOwnPropertyDescriptor(source, sym).enumerable;
            }));
        }
        ownKeys.forEach(function(key) {
            _define_property(target, key, source[key]);
        });
    }
    return target;
}
function _object_without_properties(source, excluded) {
    if (source == null) return {};
    var target = _object_without_properties_loose(source, excluded);
    var key, i;
    if (Object.getOwnPropertySymbols) {
        var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceSymbolKeys.length; i++){
            key = sourceSymbolKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
    }
    return target;
}
function _object_without_properties_loose(source, excluded) {
    if (source == null) return {};
    var target = {};
    var sourceKeys = Object.keys(source);
    var key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        target[key] = source[key];
    }
    return target;
}
import { ConfigProvider, Tooltip } from "antd";
import classNames from "classnames";
import { AnimatePresence, motion } from "framer-motion";
import { isObject } from "lodash-es";
import React, { forwardRef, isValidElement, useContext, useImperativeHandle } from "react";
import { useScrollVisible } from "./hooks/useScrollVisible";
import { prefixCls, useStyle } from "./style";
var DEFAULT_VISIBLE_THRESHOLD = 400;
var getDefaultTarget = function() {
    return window;
};
var getShouldVisibleHandler = function(propsShouldVisible) {
    return function(scrollTop, container) {
        if (typeof propsShouldVisible === 'function') {
            return propsShouldVisible(scrollTop, container);
        }
        return scrollTop >= propsShouldVisible;
    };
};
var getTooltipProps = function(tooltip) {
    if (isObject(tooltip) && !/*#__PURE__*/ isValidElement(tooltip)) {
        return tooltip;
    }
    return {
        title: tooltip
    };
};
var EXIT_ANIMATION = {
    opacity: 0
};
/**
 * ScrollVisibleButton 组件
 *
 * 根据滚动位置显示/隐藏的按钮，支持平滑动画效果
 *
 * @example
 * ```tsx
 * <ScrollVisibleButton
 *   tooltip="返回顶部"
 *   shouldVisible={400}
 *   onClick={handleClick}
 * >
 *   <ArrowUpIcon />
 * </ScrollVisibleButton>
 * ```
 */ export var ScrollVisibleButton = /*#__PURE__*/ forwardRef(function(_param, ref) {
    var className = _param.className, style = _param.style, tmp = _param.shouldVisible, propsShouldVisible = tmp === void 0 ? DEFAULT_VISIBLE_THRESHOLD : tmp, target = _param.target, onClick = _param.onClick, tooltip = _param.tooltip, children = _param.children, rest = _object_without_properties(_param, [
        "className",
        "style",
        "shouldVisible",
        "target",
        "onClick",
        "tooltip",
        "children"
    ]);
    var context = useContext(ConfigProvider.ConfigContext);
    var baseCls = context === null || context === void 0 ? void 0 : context.getPrefixCls(prefixCls);
    var _useStyle = useStyle(baseCls), wrapSSR = _useStyle.wrapSSR, hashId = _useStyle.hashId;
    var internalRef = React.useRef(null);
    useImperativeHandle(ref, function() {
        return {
            nativeElement: internalRef.current
        };
    });
    var getTarget = target || getDefaultTarget;
    var shouldVisible = getShouldVisibleHandler(propsShouldVisible);
    var _useScrollVisible = useScrollVisible({
        target: getTarget,
        shouldVisible: shouldVisible
    }), visible = _useScrollVisible.visible, currentContainer = _useScrollVisible.currentContainer;
    var handleClick = function(e) {
        onClick === null || onClick === void 0 ? void 0 : onClick(e, currentContainer.current);
    };
    var button = /*#__PURE__*/ React.createElement("button", _object_spread({
        ref: internalRef,
        className: classNames(baseCls, className, hashId),
        style: style,
        type: "button",
        onClick: handleClick
    }, rest), /*#__PURE__*/ React.createElement("div", {
        className: "".concat(baseCls, "-content ").concat(hashId)
    }, children));
    var buttonWithTooltip = tooltip ? /*#__PURE__*/ React.createElement(Tooltip, getTooltipProps(tooltip), button) : button;
    return wrapSSR(/*#__PURE__*/ React.createElement(AnimatePresence, null, visible && /*#__PURE__*/ React.createElement(motion.div, {
        exit: EXIT_ANIMATION
    }, buttonWithTooltip)));
});
