var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
  isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
  mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var stdin_exports = {};
__export(stdin_exports, {
  default: () => stdin_default
});
module.exports = __toCommonJS(stdin_exports);
var import_react = __toESM(require("react"));
var import_classnames = __toESM(require("classnames"));
var import_use_scroll_parent = __toESM(require("../hooks/use-scroll-parent"));
var import_use_event_listener = __toESM(require("../hooks/use-event-listener"));
var import_utils = require("../utils");
var import_hooks = require("../hooks");
var import_get_rect = require("../hooks/get-rect");
var import_ConfigProviderContext = __toESM(require("../config-provider/ConfigProviderContext"));
const Sticky = (props) => {
  const {
    prefixCls,
    createNamespace
  } = (0, import_react.useContext)(import_ConfigProviderContext.default);
  const [bem] = createNamespace("sticky", prefixCls);
  const [state, updateState] = (0, import_hooks.useSetState)({
    fixed: false,
    width: 0,
    height: 0,
    transform: 0
  });
  const root = (0, import_react.useRef)(null);
  const scrollParent = (0, import_use_scroll_parent.default)(root);
  const offset = (0, import_react.useMemo)(() => (0, import_utils.unitToPx)(props.position === "top" ? props.offsetTop : props.offsetBottom), [props.position, props.offsetTop, props.offsetBottom]);
  const rootStyle = (0, import_react.useMemo)(() => {
    const {
      fixed,
      height,
      width
    } = state;
    if (fixed) {
      return {
        width: `${width}px`,
        height: `${height}px`
      };
    }
    return null;
  }, [state.fixed, state.height, state.width]);
  const stickyStyle = (0, import_react.useMemo)(() => {
    if (!state.fixed) {
      return null;
    }
    const style = (0, import_utils.extend)((0, import_utils.getZIndexStyle)(props.zIndex), {
      width: `${state.width}px`,
      height: `${state.height}px`,
      [props.position]: `${offset}px`
    });
    if (state.transform) {
      style.transform = `translate3d(0, ${state.transform}px, 0)`;
    }
    return style;
  }, [props.position, state.fixed, offset, state.width, state.height, state.transform]);
  const emitScroll = (scrollTop, isFixed) => {
    if (props.onScroll) {
      props.onScroll({
        scrollTop,
        isFixed
      });
    }
  };
  const onScroll = () => {
    if (!root.current || (0, import_utils.isHidden)(root.current)) {
      return;
    }
    const {
      container,
      position
    } = props;
    const rootRect = (0, import_get_rect.getRect)(root.current);
    const scrollTop = (0, import_utils.getScrollTop)(window);
    const newState = {};
    newState.width = rootRect.width;
    newState.height = rootRect.height;
    if (position === "top") {
      if (container) {
        const containerRect = (0, import_get_rect.getRect)(container.current);
        const difference = containerRect.bottom - offset - newState.height;
        newState.fixed = offset > rootRect.top && containerRect.bottom > 0;
        newState.transform = difference < 0 ? difference : 0;
      } else {
        newState.fixed = offset > rootRect.top;
      }
    } else {
      const {
        clientHeight
      } = document.documentElement;
      if (container) {
        const containerRect = (0, import_get_rect.getRect)(container.current);
        const difference = clientHeight - containerRect.top - offset - newState.height;
        newState.fixed = clientHeight - offset < rootRect.bottom && clientHeight > containerRect.top;
        newState.transform = difference < 0 ? -difference : 0;
      } else {
        newState.fixed = clientHeight - offset < rootRect.bottom;
      }
    }
    updateState(newState);
    emitScroll(scrollTop, newState.fixed);
  };
  (0, import_use_event_listener.default)("scroll", onScroll, {
    target: scrollParent
  });
  (0, import_hooks.useVisibilityChange)(root, onScroll);
  (0, import_hooks.useUpdateEffect)(() => {
    var _a;
    (_a = props.onChange) == null ? void 0 : _a.call(props, state.fixed);
  }, [state.fixed]);
  return /* @__PURE__ */ import_react.default.createElement("div", {
    ref: root,
    style: rootStyle
  }, /* @__PURE__ */ import_react.default.createElement("div", {
    className: (0, import_classnames.default)(bem({
      fixed: state.fixed
    })),
    style: stickyStyle
  }, props.children));
};
Sticky.defaultProps = {
  offsetTop: 0,
  offsetBottom: 0,
  position: "top"
};
var stdin_default = Sticky;
