function _array_like_to_array(arr, len) {
    if (len == null || len > arr.length) len = arr.length;
    for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
    return arr2;
}
function _array_with_holes(arr) {
    if (Array.isArray(arr)) return arr;
}
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 _iterable_to_array_limit(arr, i) {
    var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
    if (_i == null) return;
    var _arr = [];
    var _n = true;
    var _d = false;
    var _s, _e;
    try {
        for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
            _arr.push(_s.value);
            if (i && _arr.length === i) break;
        }
    } catch (err) {
        _d = true;
        _e = err;
    } finally{
        try {
            if (!_n && _i["return"] != null) _i["return"]();
        } finally{
            if (_d) throw _e;
        }
    }
    return _arr;
}
function _non_iterable_rest() {
    throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
}
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 = {}, sourceKeys, key, i;
    if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
        sourceKeys = Reflect.ownKeys(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[i];
            if (excluded.indexOf(key) >= 0) continue;
            if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
            target[key] = source[key];
        }
        return target;
    }
    target = _object_without_properties_loose(source, excluded);
    if (Object.getOwnPropertySymbols) {
        sourceKeys = Object.getOwnPropertySymbols(source);
        for(i = 0; i < sourceKeys.length; i++){
            key = sourceKeys[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 = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
    for(i = 0; i < sourceKeys.length; i++){
        key = sourceKeys[i];
        if (excluded.indexOf(key) >= 0) continue;
        if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
        target[key] = source[key];
    }
    return target;
}
function _sliced_to_array(arr, i) {
    return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
}
function _unsupported_iterable_to_array(o, minLen) {
    if (!o) return;
    if (typeof o === "string") return _array_like_to_array(o, minLen);
    var n = Object.prototype.toString.call(o).slice(8, -1);
    if (n === "Object" && o.constructor) n = o.constructor.name;
    if (n === "Map" || n === "Set") return Array.from(n);
    if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
}
import { ConfigProvider, Tooltip } from "antd";
import classNames from "clsx";
import { isObject } from "lodash-es";
import React, { forwardRef, isValidElement, useContext, useEffect, useImperativeHandle, useState } from "react";
import { useScrollVisible } from "./hooks/useScrollVisible";
import { prefixCls, useStyle } from "./style";
var DEFAULT_VISIBLE_THRESHOLD = 400;
var getDefaultTarget = function getDefaultTarget() {
    return window;
};
var getShouldVisibleHandler = function getShouldVisibleHandler(propsShouldVisible) {
    return function(scrollTop, container) {
        if (typeof propsShouldVisible === 'function') {
            return propsShouldVisible(scrollTop, container);
        }
        return scrollTop >= propsShouldVisible;
    };
};
var getTooltipProps = function getTooltipProps(tooltip) {
    if (isObject(tooltip) && !/*#__PURE__*/ isValidElement(tooltip)) {
        return tooltip;
    }
    return {
        title: tooltip
    };
};
/**
 * ScrollVisibleButton 组件
 *
 * 根据滚动位置显示/隐藏的按钮，支持平滑动画效果。
 * 注意：shouldVisible 是内部实现细节，请使用 BackTo.Top / BackTo.Bottom。
 *
 * @example
 * ```tsx
 * <BackTo.Top tooltip="返回顶部" />
 * <BackTo.Bottom tooltip="去底部" />
 * ```
 */ export var ScrollVisibleButton = /*#__PURE__*/ forwardRef(function(props, ref) {
    // shouldVisible 是内部 prop，不在公开类型中暴露，通过内部类型读取
    var className = props.className, style = props.style, tmp = props.shouldVisible, propsShouldVisible = tmp === void 0 ? DEFAULT_VISIBLE_THRESHOLD : tmp, target = props.target, onClick = props.onClick, tooltip = props.tooltip, children = props.children, dataTestId = props['data-testid'], rest = _object_without_properties(props, [
        "className",
        "style",
        "shouldVisible",
        "target",
        "onClick",
        "tooltip",
        "children",
        'data-testid'
    ]);
    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;
    // 显隐动画：presence wrapper 始终保留在 DOM 中（fixed 定位，完全脱离文档流），
    // 只通过 opacity + pointer-events 切换显隐，避免挂载/卸载触发文档流重排导致页面跳动。
    var _useState = _sliced_to_array(useState(visible ? 'enter' : 'exit'), 2), dataState = _useState[0], setDataState = _useState[1];
    useEffect(function() {
        if (visible) {
            // 下一帧切到 enter，确保浏览器已完成一次 layout 后再触发 opacity 过渡
            var raf = requestAnimationFrame(function() {
                return setDataState('enter');
            });
            return function() {
                return cancelAnimationFrame(raf);
            };
        }
        setDataState('exit');
        return undefined;
    }, [
        visible
    ]);
    var handleClick = function handleClick(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),
        "data-testid": dataTestId !== null && dataTestId !== void 0 ? dataTestId : baseCls,
        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;
    // presence wrapper 始终挂载（position:fixed 脱离文档流），不会触发布局重排
    return wrapSSR(/*#__PURE__*/ React.createElement(React.Fragment, null, /*#__PURE__*/ React.createElement("div", {
        className: "".concat(baseCls, "-presence ").concat(hashId),
        "data-state": dataState
    }, buttonWithTooltip)));
});
