"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "Sticky", {
    enumerable: true,
    get: function() {
        return Sticky;
    }
});
var _interop_require_default = require("@swc/helpers/_/_interop_require_default");
var _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
var _define_property = require("@swc/helpers/_/_define_property");
var _object_spread = require("@swc/helpers/_/_object_spread");
var _object_spread_props = require("@swc/helpers/_/_object_spread_props");
var _object_without_properties = require("@swc/helpers/_/_object_without_properties");
var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
var _classnames = /*#__PURE__*/ _interop_require_default._(require("classnames"));
var _getscrollparent = require("../../utils/get-scroll-parent");
var _useclientrect = require("../../hooks/use-client-rect");
var _usewatch = /*#__PURE__*/ _interop_require_default._(require("../../hooks/use-watch"));
var _typings = require("../../utils/typings");
var defaultProps = (0, _object_spread_props._)((0, _object_spread._)({}, _typings.ComponentDefaults), {
    position: 'top',
    threshold: 0,
    zIndex: 900
});
var classPrefix = 'nut-sticky';
var Sticky = function Sticky(props) {
    var _ref = (0, _object_spread._)({}, defaultProps, props), position = _ref.position, zIndex = _ref.zIndex, children = _ref.children, container = _ref.container, style = _ref.style, className = _ref.className, threshold = _ref.threshold, onChange = _ref.onChange, rest = (0, _object_without_properties._)(_ref, [
        "position",
        "zIndex",
        "children",
        "container",
        "style",
        "className",
        "threshold",
        "onChange"
    ]);
    var stickyRef = (0, _react.useRef)(null);
    var rootRef = (0, _react.useRef)(null);
    var _useState = (0, _sliced_to_array._)((0, _react.useState)(false), 2), isFixed = _useState[0], setIsFixed = _useState[1];
    var _obj;
    var _useState1 = (0, _sliced_to_array._)((0, _react.useState)((_obj = {}, (0, _define_property._)(_obj, position, "".concat(threshold, "px")), (0, _define_property._)(_obj, "zIndex", zIndex), _obj)), 2), stickyStyle = _useState1[0], setStickyStyle = _useState1[1];
    (0, _react.useEffect)(function() {
        var _obj;
        setStickyStyle((0, _object_spread_props._)((0, _object_spread._)({}, stickyStyle), (_obj = {}, (0, _define_property._)(_obj, position, "".concat(threshold, "px")), (0, _define_property._)(_obj, "zIndex", zIndex), _obj)));
    }, [
        threshold,
        position,
        zIndex
    ]);
    var _useState2 = (0, _sliced_to_array._)((0, _react.useState)({}), 2), rootStyle = _useState2[0], setRootStyle = _useState2[1];
    var getElement = (0, _react.useCallback)(function() {
        return (0, _getscrollparent.getScrollParent)(rootRef.current);
    }, []);
    (0, _react.useEffect)(function() {
        if (position === 'top') return;
        var containerEle = container && container.current;
        var rootEle = rootRef.current;
        var stickyEle = stickyRef.current;
        if (!rootEle && !containerEle) return;
        var rootRect = (0, _useclientrect.getRect)(rootEle);
        var containerRect = (0, _useclientrect.getRect)(containerEle);
        var clientHeight = document.documentElement.clientHeight;
        var stickyRect = (0, _useclientrect.getRect)(stickyEle);
        var fixed = clientHeight - threshold < rootRect.bottom;
        if (containerEle) {
            fixed = containerRect.bottom > clientHeight - threshold - stickyRect.height && clientHeight - threshold - stickyRect.height > containerRect.top;
        }
        var defaultPostVal = fixed ? 'fixed' : 'inherit';
        setStickyStyle(function(prev) {
            return (0, _object_spread_props._)((0, _object_spread._)({}, prev), {
                position: defaultPostVal
            });
        });
        setIsFixed(fixed);
    }, [
        position,
        container,
        threshold
    ]);
    var handleScroll = (0, _react.useCallback)(function() {
        var containerEle = container && container.current;
        var rootEle = rootRef.current;
        var stickyEle = stickyRef.current;
        if (!rootEle && !containerEle) return;
        var rootRect = (0, _useclientrect.getRect)(rootEle);
        var stickyRect = (0, _useclientrect.getRect)(stickyEle);
        var containerRect = (0, _useclientrect.getRect)(containerEle);
        if (rootRect.height) {
            setRootStyle(function(prev) {
                return (0, _object_spread_props._)((0, _object_spread._)({}, prev), {
                    height: rootRect.height
                });
            });
        }
        var getFixed = function getFixed() {
            var fixed = false;
            if (position === 'top') {
                fixed = containerEle ? threshold > rootRect.top && containerRect.bottom > 0 : threshold > rootRect.top;
            } else {
                var clientHeight = document.documentElement.clientHeight;
                fixed = containerEle ? containerRect.bottom > 0 && clientHeight - threshold - stickyRect.height > containerRect.top : clientHeight - threshold < rootRect.bottom;
            }
            return {
                position: fixed ? 'fixed' : 'inherit',
                fixed: fixed
            };
        };
        var getTransform = function getTransform() {
            if (position === 'top' && containerEle) {
                var diff = containerRect.bottom - threshold - stickyRect.height;
                var transform = diff < 0 ? diff : 0;
                return {
                    transform: "translate3d(0, ".concat(transform, "px, 0)")
                };
            }
            if (position === 'bottom' && containerEle) {
                var clientHeight = document.documentElement.clientHeight;
                var diff1 = containerRect.bottom - (clientHeight - threshold);
                var transform1 = diff1 < 0 ? diff1 : 0;
                return {
                    transform: "translate3d(0, ".concat(transform1, "px, 0)")
                };
            }
            return {};
        };
        var fixed = getFixed();
        setStickyStyle(function(prev) {
            return (0, _object_spread_props._)((0, _object_spread._)({}, prev, getTransform()), {
                position: fixed.position
            });
        });
        setIsFixed(fixed.fixed);
    }, [
        position,
        threshold,
        container
    ]);
    (0, _usewatch.default)(isFixed, function() {
        onChange && onChange(isFixed);
    });
    (0, _react.useEffect)(function() {
        var el = getElement();
        el === null || el === void 0 ? void 0 : el.addEventListener('scroll', handleScroll, false);
        return function() {
            el === null || el === void 0 ? void 0 : el.removeEventListener('scroll', handleScroll);
        };
    }, [
        getElement,
        handleScroll
    ]);
    return /*#__PURE__*/ _react.default.createElement("div", (0, _object_spread._)({
        ref: rootRef,
        style: (0, _object_spread._)({}, style, rootStyle),
        className: (0, _classnames.default)(classPrefix, className)
    }, rest), /*#__PURE__*/ _react.default.createElement("div", {
        className: "nut-sticky-box",
        ref: stickyRef,
        style: stickyStyle
    }, children));
};
Sticky.displayName = 'NutSticky';
