import { effectScope, nextTick, onScopeDispose, ref, watchEffect } from "vue";
import { IN_BROWSER, propsFactory } from "@veno-ui/utils";
import { staticPositionStrategy } from "./static";
import { connectedPositionStrategy } from "./connected";
import { pointerPositionStrategy } from "./pointer";
const POSITION_STRATEGIES = {
  static: staticPositionStrategy,
  // specific viewport position, usually centered
  connected: connectedPositionStrategy,
  // connected to a certain element
  pointer: pointerPositionStrategy
  // pointer position
};
const makePositionStrategyProps = propsFactory({
  /**
   * @zh 定位策略
   */
  positionStrategy: {
    type: [String, Function],
    default: "static",
    validator: (val) => typeof val === "function" || val in POSITION_STRATEGIES
  },
  /**
   * @zh 锚点位置
   */
  anchor: {
    type: String,
    default: "bottom"
  },
  /**
   * @zh 原始点策略
   */
  origin: {
    type: String,
    default: "auto"
  },
  /**
   * @zh 偏移值
   */
  offset: [Number, String]
}, "position-strategy");
function usePositionStrategy(props, data) {
  const contentStyles = ref({});
  const anchorClasses = ref([]);
  const updatePosition = ref();
  let scope;
  watchEffect(async () => {
    scope?.stop();
    updatePosition.value = void 0;
    if (!(IN_BROWSER && data.isActive.value && props.positionStrategy))
      return;
    scope = effectScope();
    await nextTick();
    scope.run(() => {
      const strategy = typeof props.positionStrategy === "function" ? props.positionStrategy : POSITION_STRATEGIES[props.positionStrategy];
      updatePosition.value = strategy(
        data,
        props,
        contentStyles,
        anchorClasses
      )?.updatePosition;
    });
  });
  IN_BROWSER && window.addEventListener("resize", onResize, { passive: true });
  onScopeDispose(() => {
    IN_BROWSER && window.removeEventListener("resize", onResize);
    updatePosition.value = void 0;
    scope?.stop();
  });
  function onResize(e) {
    updatePosition.value?.(e);
  }
  return {
    contentStyles,
    anchorClasses,
    updatePosition
  };
}
export {
  POSITION_STRATEGIES,
  makePositionStrategyProps,
  usePositionStrategy
};
//# sourceMappingURL=index.mjs.map
