import { defineComponent, reactive, ref, computed, createVNode } from "vue";
import { INDEX_BAR_KEY } from "../index-bar/IndexBar.mjs";
import { getRootScrollTop, getScrollTop } from "../utils/dom.mjs";
import { useParent, useRect } from "../../../@vant/use/dist/index.esm.mjs";
import { useExpose } from "../composables/use-expose.mjs";
import { BORDER_BOTTOM } from "../utils/constant.mjs";
import { numericProp } from "../utils/props.mjs";
import { createNamespace } from "../utils/create.mjs";
import { extend } from "../utils/basic.mjs";
import { getZIndexStyle } from "../utils/format.mjs";
const [name, bem] = createNamespace("index-anchor");
const indexAnchorProps = {
  index: numericProp
};
var stdin_default = defineComponent({
  name,
  props: indexAnchorProps,
  setup(props, {
    slots
  }) {
    const state = reactive({
      top: 0,
      left: null,
      rect: {
        top: 0,
        height: 0
      },
      width: null,
      active: false
    });
    const root = ref();
    const {
      parent
    } = useParent(INDEX_BAR_KEY);
    if (!parent) {
      return;
    }
    const isSticky = () => state.active && parent.props.sticky;
    const anchorStyle = computed(() => {
      const {
        zIndex,
        highlightColor
      } = parent.props;
      if (isSticky()) {
        return extend(getZIndexStyle(zIndex), {
          left: state.left ? `${state.left}px` : void 0,
          width: state.width ? `${state.width}px` : void 0,
          transform: state.top ? `translate3d(0, ${state.top}px, 0)` : void 0,
          color: highlightColor
        });
      }
    });
    const getRect = (scrollParent, scrollParentRect) => {
      const rootRect = useRect(root);
      state.rect.height = rootRect.height;
      if (scrollParent === window || scrollParent === document.body) {
        state.rect.top = rootRect.top + getRootScrollTop();
      } else {
        state.rect.top = rootRect.top + getScrollTop(scrollParent) - scrollParentRect.top;
      }
      return state.rect;
    };
    useExpose({
      state,
      getRect
    });
    return () => {
      const sticky = isSticky();
      return createVNode("div", {
        "ref": root,
        "style": {
          height: sticky ? `${state.rect.height}px` : void 0
        }
      }, [createVNode("div", {
        "style": anchorStyle.value,
        "class": [bem({
          sticky
        }), {
          [BORDER_BOTTOM]: sticky
        }]
      }, [slots.default ? slots.default() : props.index])]);
    };
  }
});
export {
  stdin_default as default,
  indexAnchorProps
};
