"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const Vue = require("vue");
const index_esm = require("../../../@vant/use/dist/index.esm.js");
const useVisibilityChange = require("../composables/use-visibility-change.js");
const props = require("../utils/props.js");
const format = require("../utils/format.js");
const dom = require("../utils/dom.js");
const create = require("../utils/create.js");
const basic = require("../utils/basic.js");
const [name, bem] = create.createNamespace("sticky");
const stickyProps = {
  zIndex: props.numericProp,
  position: props.makeStringProp("top"),
  container: Object,
  offsetTop: props.makeNumericProp(0),
  offsetBottom: props.makeNumericProp(0)
};
var stdin_default = Vue.defineComponent({
  name,
  props: stickyProps,
  emits: ["scroll", "change"],
  setup(props2, {
    emit,
    slots
  }) {
    const root = Vue.ref();
    const scrollParent = index_esm.useScrollParent(root);
    const state = Vue.reactive({
      fixed: false,
      width: 0,
      // root width
      height: 0,
      // root height
      transform: 0
    });
    const isReset = Vue.ref(false);
    const offset = Vue.computed(() => format.unitToPx(props2.position === "top" ? props2.offsetTop : props2.offsetBottom));
    const rootStyle = Vue.computed(() => {
      if (isReset.value) {
        return;
      }
      const {
        fixed,
        height,
        width
      } = state;
      if (fixed) {
        return {
          width: `${width}px`,
          height: `${height}px`
        };
      }
    });
    const stickyStyle = Vue.computed(() => {
      if (!state.fixed || isReset.value) {
        return;
      }
      const style = basic.extend(format.getZIndexStyle(props2.zIndex), {
        width: `${state.width}px`,
        height: `${state.height}px`,
        [props2.position]: `${offset.value}px`
      });
      if (state.transform) {
        style.transform = `translate3d(0, ${state.transform}px, 0)`;
      }
      return style;
    });
    const emitScroll = (scrollTop) => emit("scroll", {
      scrollTop,
      isFixed: state.fixed
    });
    const onScroll = () => {
      if (!root.value || dom.isHidden(root)) {
        return;
      }
      const {
        container,
        position
      } = props2;
      const rootRect = index_esm.useRect(root);
      const scrollTop = dom.getScrollTop(window);
      state.width = rootRect.width;
      state.height = rootRect.height;
      if (position === "top") {
        if (container) {
          const containerRect = index_esm.useRect(container);
          const difference = containerRect.bottom - offset.value - state.height;
          state.fixed = offset.value > rootRect.top && containerRect.bottom > 0;
          state.transform = difference < 0 ? difference : 0;
        } else {
          state.fixed = offset.value > rootRect.top;
        }
      } else {
        const {
          clientHeight
        } = document.documentElement;
        if (container) {
          const containerRect = index_esm.useRect(container);
          const difference = clientHeight - containerRect.top - offset.value - state.height;
          state.fixed = clientHeight - offset.value < rootRect.bottom && clientHeight > containerRect.top;
          state.transform = difference < 0 ? -difference : 0;
        } else {
          state.fixed = clientHeight - offset.value < rootRect.bottom;
        }
      }
      emitScroll(scrollTop);
    };
    Vue.watch(() => state.fixed, (value) => emit("change", value));
    index_esm.useEventListener("scroll", onScroll, {
      target: scrollParent,
      passive: true
    });
    useVisibilityChange.useVisibilityChange(root, onScroll);
    Vue.watch([dom.windowWidth, dom.windowHeight], () => {
      if (!root.value || dom.isHidden(root) || !state.fixed) {
        return;
      }
      isReset.value = true;
      Vue.nextTick(() => {
        const rootRect = index_esm.useRect(root);
        state.width = rootRect.width;
        state.height = rootRect.height;
        isReset.value = false;
      });
    });
    return () => {
      var _a;
      return Vue.createVNode("div", {
        "ref": root,
        "style": rootStyle.value
      }, [Vue.createVNode("div", {
        "class": bem({
          fixed: state.fixed && !isReset.value
        }),
        "style": stickyStyle.value
      }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])]);
    };
  }
});
exports.default = stdin_default;
exports.stickyProps = stickyProps;
