import { usePopper } from "../../../../hooks/use-popper/index.mjs";
import { POPPER_INJECTION_KEY } from "../constants.mjs";
import { buildPopperOptions, unwrapMeasurableEl } from "../utils.mjs";
import { useResizeObserver } from "../../../../../node_modules/@vueuse/core/index.mjs";
import { inject, ref, computed, unref, watch, onMounted, onBeforeUnmount } from "vue";
import isUndefined from "../../../../../../lodash-es/isUndefined.mjs";
const DEFAULT_ARROW_OFFSET = 0;
const usePopperContent = (props) => {
  const { popperInstanceRef, contentRef, triggerRef, role } = inject(POPPER_INJECTION_KEY, void 0);
  const arrowRef = ref();
  const arrowOffset = computed(() => props.arrowOffset);
  const eventListenerModifier = computed(() => {
    return {
      name: "eventListeners",
      enabled: !!props.visible
    };
  });
  const arrowModifier = computed(() => {
    const arrowEl = unref(arrowRef);
    const offset = unref(arrowOffset) ?? DEFAULT_ARROW_OFFSET;
    return {
      name: "arrow",
      enabled: !isUndefined(arrowEl),
      options: {
        element: arrowEl,
        padding: offset
      }
    };
  });
  const options = computed(() => {
    return {
      onFirstUpdate: () => {
        update();
      },
      ...buildPopperOptions(props, [unref(arrowModifier), unref(eventListenerModifier)])
    };
  });
  const computedReference = computed(() => unwrapMeasurableEl(props.referenceEl) || unref(triggerRef));
  const { attributes, state, styles, update, forceUpdate, instanceRef } = usePopper(computedReference, contentRef, options);
  watch(instanceRef, (instance) => popperInstanceRef.value = instance, { flush: "sync" });
  onMounted(() => {
    watch(() => {
      var _a, _b;
      return (_b = (_a = unref(computedReference)) == null ? void 0 : _a.getBoundingClientRect) == null ? void 0 : _b.call(_a);
    }, () => {
      update();
    });
  });
  let stopResizeObserver;
  watch(() => props.visible, (visible) => {
    stopResizeObserver == null ? void 0 : stopResizeObserver();
    stopResizeObserver = void 0;
    if (visible) stopResizeObserver = useResizeObserver(contentRef, update).stop;
  });
  onBeforeUnmount(() => {
    popperInstanceRef.value = void 0;
    stopResizeObserver == null ? void 0 : stopResizeObserver();
    stopResizeObserver = void 0;
  });
  return {
    attributes,
    arrowRef,
    contentRef,
    instanceRef,
    state,
    styles,
    role,
    forceUpdate,
    update
  };
};
export {
  usePopperContent
};
