import { defineComponent, shallowRef, reactive, getCurrentInstance, computed, watch, onMounted, onUpdated, onUnmounted, createVNode } from 'vue';
import '../../../utils/index.mjs';
import '../../../vue-components/index.mjs';
import useConfigInject from '../../config-provider/src/hooks/useConfigInject.mjs';
import useStyle from '../style/index.mjs';
import { getTargetRect, getFixedTop, getFixedBottom, removeObserveTarget, addObserveTarget } from './utils.mjs';
import { affixProps } from './props.mjs';
import { AffixStatus } from './interface.mjs';
import { throttleByAnimationFrame } from '../../../utils/throttleByAnimationFrame.mjs';
import { classNames } from '../../../utils/dom/class.mjs';
import { omit } from '../../../utils/lang/omit.mjs';
import ResizeObserver from '../../../vue-components/vc-resize-observer/src/index.mjs';

var AAffix = /* @__PURE__ */ defineComponent({
  compatConfig: {
    MODE: 3
  },
  name: "AAffix",
  inheritAttrs: false,
  props: affixProps(),
  setup(props, {
    slots,
    emit,
    expose,
    attrs
  }) {
    const placeholderNode = shallowRef();
    const fixedNode = shallowRef();
    const state = reactive({
      affixStyle: void 0,
      placeholderStyle: void 0,
      status: AffixStatus.None,
      lastAffix: false,
      prevTarget: null,
      timeout: null
    });
    const currentInstance = getCurrentInstance();
    const offsetTop = computed(() => {
      return props.offsetBottom === void 0 && props.offsetTop === void 0 ? 0 : props.offsetTop;
    });
    const offsetBottom = computed(() => props.offsetBottom);
    const measure = () => {
      const {
        status,
        lastAffix
      } = state;
      const {
        target
      } = props;
      if (status !== AffixStatus.Prepare || !fixedNode.value || !placeholderNode.value || !target)
        return;
      const targetNode = target();
      if (!targetNode)
        return;
      const newState = {
        status: AffixStatus.None
      };
      const placeholderRect = getTargetRect(placeholderNode.value);
      if (placeholderRect.top === 0 && placeholderRect.left === 0 && placeholderRect.width === 0 && placeholderRect.height === 0)
        return;
      const targetRect = getTargetRect(targetNode);
      const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
      const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
      if (placeholderRect.top === 0 && placeholderRect.left === 0 && placeholderRect.width === 0 && placeholderRect.height === 0)
        return;
      if (fixedTop !== void 0) {
        const width = `${placeholderRect.width}px`;
        const height = `${placeholderRect.height}px`;
        newState.affixStyle = {
          position: "fixed",
          top: fixedTop,
          width,
          height
        };
        newState.placeholderStyle = {
          width,
          height
        };
      } else if (fixedBottom !== void 0) {
        const width = `${placeholderRect.width}px`;
        const height = `${placeholderRect.height}px`;
        newState.affixStyle = {
          position: "fixed",
          bottom: fixedBottom,
          width,
          height
        };
        newState.placeholderStyle = {
          width,
          height
        };
      }
      newState.lastAffix = !!newState.affixStyle;
      if (lastAffix !== newState.lastAffix)
        emit("change", newState.lastAffix);
      Object.assign(state, newState);
    };
    const prepareMeasure = () => {
      Object.assign(state, {
        status: AffixStatus.Prepare,
        affixStyle: void 0,
        placeholderStyle: void 0
      });
      if (process.env.NODE_ENV === "test")
        emit("testUpdatePosition");
    };
    const updatePosition = throttleByAnimationFrame(() => {
      prepareMeasure();
    });
    const lazyUpdatePosition = throttleByAnimationFrame(() => {
      const {
        target
      } = props;
      const {
        affixStyle
      } = state;
      if (target && affixStyle) {
        const targetNode = target();
        if (targetNode && placeholderNode.value) {
          const targetRect = getTargetRect(targetNode);
          const placeholderRect = getTargetRect(placeholderNode.value);
          const fixedTop = getFixedTop(placeholderRect, targetRect, offsetTop.value);
          const fixedBottom = getFixedBottom(placeholderRect, targetRect, offsetBottom.value);
          if (fixedTop !== void 0 && affixStyle.top === fixedTop || fixedBottom !== void 0 && affixStyle.bottom === fixedBottom)
            return;
        }
      }
      prepareMeasure();
    });
    expose({
      updatePosition,
      lazyUpdatePosition
    });
    watch(() => props.target, (val) => {
      const newTarget = (val == null ? void 0 : val()) || null;
      if (state.prevTarget !== newTarget) {
        removeObserveTarget(currentInstance);
        if (newTarget) {
          addObserveTarget(newTarget, currentInstance);
          updatePosition();
        }
        state.prevTarget = newTarget;
      }
    });
    watch(() => [props.offsetTop, props.offsetBottom], updatePosition);
    onMounted(() => {
      const {
        target
      } = props;
      if (target) {
        state.timeout = setTimeout(() => {
          addObserveTarget(target(), currentInstance);
          updatePosition();
        });
      }
    });
    onUpdated(() => {
      measure();
    });
    onUnmounted(() => {
      clearTimeout(state.timeout);
      removeObserveTarget(currentInstance);
      updatePosition.cancel();
      lazyUpdatePosition.cancel();
    });
    const {
      prefixCls
    } = useConfigInject("affix", props);
    const [wrapSSR, hashId] = useStyle(prefixCls);
    return () => {
      const {
        affixStyle,
        placeholderStyle,
        status
      } = state;
      const className = classNames({
        [prefixCls.value]: affixStyle,
        [hashId.value]: true
      });
      const restProps = omit(props, ["prefixCls", "offsetTop", "offsetBottom", "target", "onChange", "onTestUpdatePosition"]);
      return wrapSSR(createVNode(ResizeObserver, {
        "onResize": updatePosition
      }, {
        default: () => {
          var _a;
          return [createVNode("div", {
            ...restProps,
            ...attrs,
            "ref": placeholderNode,
            "data-measure-status": status
          }, [affixStyle && createVNode("div", {
            "style": placeholderStyle,
            "aria-hidden": "true"
          }, null), createVNode("div", {
            "class": className,
            "ref": fixedNode,
            "style": affixStyle
          }, [(_a = slots.default) == null ? void 0 : _a.call(slots)])])];
        }
      }));
    };
  }
});

export { AAffix as default };
//# sourceMappingURL=Affix.mjs.map
