"use strict";
const common_vendor = require("../../../../common/vendor.js");
const uni_modules_uviewPro_libs_index = require("../../libs/index.js");
require("../../libs/util/logger.js");
require("../../libs/config/theme-tokens.js");
const uni_modules_uviewPro_components_uSticky_types = require("./types.js");
const __default__ = {
  name: "u-sticky",
  options: {
    addGlobalClass: true,
    virtualHost: true,
    styleIsolation: "shared"
  }
};
const _sfc_main = /* @__PURE__ */ common_vendor.defineComponent({
  ...__default__,
  props: uni_modules_uviewPro_components_uSticky_types.StickyProps,
  emits: ["fixed", "unfixed"],
  setup(__props, { emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const instance = common_vendor.getCurrentInstance();
    const fixed = common_vendor.ref(false);
    const height = common_vendor.ref("auto");
    const stickyTop = common_vendor.ref(0);
    const elClass = common_vendor.ref("");
    const left = common_vendor.ref(0);
    const width = common_vendor.ref("auto");
    let contentObserver = null;
    elClass.value = uni_modules_uviewPro_libs_index.$u.guid();
    const uZIndex = common_vendor.computed(() => {
      var _a, _b;
      return props.zIndex ? props.zIndex : ((_b = (_a = uni_modules_uviewPro_libs_index.$u) == null ? void 0 : _a.zIndex) == null ? void 0 : _b.sticky) ?? 970;
    });
    common_vendor.watch(
      () => props.offsetTop,
      () => {
        initObserver();
      }
    );
    common_vendor.watch(
      () => props.enable,
      (val) => {
        if (val == false) {
          fixed.value = false;
          disconnectObserver("contentObserver");
        } else {
          initObserver();
        }
      }
    );
    common_vendor.onMounted(() => {
      initObserver();
    });
    common_vendor.onBeforeUnmount(() => {
      disconnectObserver("contentObserver");
    });
    function initObserver() {
      if (!props.enable)
        return;
      stickyTop.value = Number(props.offsetTop) !== 0 ? common_vendor.index.upx2px(Number(props.offsetTop)) : 0;
      disconnectObserver("contentObserver");
      uni_modules_uviewPro_libs_index.$u.getRect("." + elClass.value, instance).then((res) => {
        height.value = res.height;
        left.value = res.left;
        width.value = res.width;
        common_vendor.nextTick$1(() => {
          observeContent();
        });
      });
    }
    function observeContent() {
      disconnectObserver("contentObserver");
      contentObserver = common_vendor.index.createIntersectionObserver(instance == null ? void 0 : instance.proxy, {
        thresholds: [0.95, 0.98, 1]
      });
      contentObserver.relativeToViewport({
        top: -stickyTop.value
      });
      contentObserver.observe("." + elClass.value, (res) => {
        if (!props.enable)
          return;
        setFixed(res.boundingClientRect.top);
      });
    }
    function setFixed(top) {
      const isFixed = top < stickyTop.value;
      if (isFixed)
        emit("fixed", props.index);
      else if (fixed.value)
        emit("unfixed", props.index);
      fixed.value = isFixed;
    }
    function disconnectObserver(observerName) {
      if (observerName === "contentObserver" && contentObserver) {
        contentObserver.disconnect();
        contentObserver = null;
      }
    }
    return (_ctx, _cache) => {
      return {
        a: fixed.value ? "fixed" : "static",
        b: stickyTop.value + "px",
        c: left.value + "px",
        d: width.value == "auto" ? "auto" : width.value + "px",
        e: uZIndex.value,
        f: common_vendor.n(elClass.value),
        g: common_vendor.n(_ctx.customClass),
        h: common_vendor.s(common_vendor.unref(uni_modules_uviewPro_libs_index.$u).toStyle({
          height: fixed.value ? height.value + "px" : "auto",
          backgroundColor: _ctx.bgColor
        }, _ctx.customStyle))
      };
    };
  }
});
const Component = /* @__PURE__ */ common_vendor._export_sfc(_sfc_main, [["__scopeId", "data-v-7f30e390"]]);
wx.createComponent(Component);
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/uview-pro/components/u-sticky/u-sticky.js.map
