"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const event = require("../../../constants/event.js");
const error = require("../../../utils/error.js");
const style = require("../../../utils/dom/style.js");
const scroll = require("../../../utils/dom/scroll.js");
const index$2 = require("../../teleport/index.js");
const affix = require("./affix.js");
const index = require("../../../hooks/use-namespace/index.js");
const index$1 = require("../../../../node_modules/@vueuse/core/index.js");
const vue = require("vue");
const COMPONENT_NAME = "ElAffix";
var affix_vue_vue_type_script_setup_true_lang_default = /* @__PURE__ */ vue.defineComponent({
  name: COMPONENT_NAME,
  __name: "affix",
  props: affix.affixProps,
  emits: affix.affixEmits,
  setup(__props, { expose: __expose, emit: __emit }) {
    const props = __props;
    const emit = __emit;
    const ns = index.useNamespace("affix");
    const target = vue.shallowRef();
    const root = vue.shallowRef();
    const scrollContainer = vue.shallowRef();
    const { height: windowHeight } = index$1.useWindowSize();
    const { height: rootHeight, width: rootWidth, top: rootTop, bottom: rootBottom, left: rootLeft, update: updateRoot } = index$1.useElementBounding(root, { windowScroll: false });
    const targetRect = index$1.useElementBounding(target);
    const fixed = vue.ref(false);
    const scrollTop = vue.ref(0);
    const transform = vue.ref(0);
    const teleportDisabled = vue.computed(() => {
      return !props.teleported || !fixed.value;
    });
    const rootStyle = vue.computed(() => {
      return {
        display: "flow-root",
        height: fixed.value ? `${rootHeight.value}px` : "",
        width: fixed.value ? `${rootWidth.value}px` : ""
      };
    });
    const affixStyle = vue.computed(() => {
      if (!fixed.value) return {};
      const offset = style.addUnit(props.offset);
      return {
        height: `${rootHeight.value}px`,
        width: `${rootWidth.value}px`,
        top: props.position === "top" ? offset : "",
        bottom: props.position === "bottom" ? offset : "",
        left: props.teleported ? `${rootLeft.value}px` : "",
        transform: transform.value ? `translateY(${transform.value}px)` : "",
        zIndex: props.zIndex
      };
    });
    const update = () => {
      if (!scrollContainer.value) return;
      scrollTop.value = scrollContainer.value instanceof Window ? document.documentElement.scrollTop : scrollContainer.value.scrollTop || 0;
      const { position, target: target2, offset } = props;
      const rootHeightOffset = offset + rootHeight.value;
      if (position === "top") if (target2) {
        const difference = targetRect.bottom.value - rootHeightOffset;
        fixed.value = offset > rootTop.value && targetRect.bottom.value > 0;
        transform.value = difference < 0 ? difference : 0;
      } else fixed.value = offset > rootTop.value;
      else if (target2) {
        const difference = windowHeight.value - targetRect.top.value - rootHeightOffset;
        fixed.value = windowHeight.value - offset < rootBottom.value && windowHeight.value > targetRect.top.value;
        transform.value = difference < 0 ? -difference : 0;
      } else fixed.value = windowHeight.value - offset < rootBottom.value;
    };
    const updateRootRect = async () => {
      if (!fixed.value) {
        updateRoot();
        return;
      }
      fixed.value = false;
      await vue.nextTick();
      updateRoot();
      fixed.value = true;
    };
    const handleScroll = async () => {
      updateRoot();
      await vue.nextTick();
      emit("scroll", {
        scrollTop: scrollTop.value,
        fixed: fixed.value
      });
    };
    vue.watch(fixed, (val) => emit(event.CHANGE_EVENT, val));
    vue.onMounted(() => {
      if (props.target) {
        target.value = document.querySelector(props.target) ?? void 0;
        if (!target.value) error.throwError(COMPONENT_NAME, `Target does not exist: ${props.target}`);
      } else target.value = document.documentElement;
      scrollContainer.value = scroll.getScrollContainer(root.value, true);
      updateRoot();
    });
    vue.onActivated(() => {
      vue.nextTick(updateRootRect);
    });
    vue.onDeactivated(() => {
      fixed.value = false;
    });
    index$1.useEventListener(scrollContainer, "scroll", handleScroll);
    vue.watchEffect(update);
    __expose({
      update,
      updateRoot: updateRootRect
    });
    return (_ctx, _cache) => {
      return vue.openBlock(), vue.createElementBlock("div", {
        ref_key: "root",
        ref: root,
        class: vue.normalizeClass(vue.unref(ns).b()),
        style: vue.normalizeStyle(rootStyle.value)
      }, [vue.createVNode(vue.unref(index$2.ElTeleport), {
        disabled: teleportDisabled.value,
        to: __props.appendTo
      }, {
        default: vue.withCtx(() => [vue.createElementVNode("div", {
          class: vue.normalizeClass({ [vue.unref(ns).m("fixed")]: fixed.value }),
          style: vue.normalizeStyle(affixStyle.value)
        }, [vue.renderSlot(_ctx.$slots, "default")], 6)]),
        _: 3
      }, 8, ["disabled", "to"])], 6);
    };
  }
});
exports.default = affix_vue_vue_type_script_setup_true_lang_default;
