import { ref, watchEffect, effectScope, nextTick, onScopeDispose } from "vue";
import { staticPositionStrategy } from "./static.mjs";
import { connectedPositionStrategy } from "./connected.mjs";
import { pointerPositionStrategy } from "./pointer.mjs";
import { propsFactory, IN_BROWSER } from "../../utils/dist/index.mjs";
const POSITION_STRATEGIES = {
  static: staticPositionStrategy,
  connected: connectedPositionStrategy,
  pointer: pointerPositionStrategy
};
const makePositionStrategyProps = propsFactory({
  positionStrategy: {
    type: [String, Function],
    default: "static",
    validator: (val) => typeof val === "function" || val in POSITION_STRATEGIES
  },
  anchor: {
    type: String,
    default: "bottom"
  },
  origin: {
    type: String,
    default: "auto"
  },
  offset: [Number, String]
}, "position-strategy");
function usePositionStrategy(props, data) {
  const contentStyles = ref({});
  const anchorClasses = ref([]);
  const updatePosition = ref();
  let scope;
  watchEffect(async () => {
    scope == null ? void 0 : scope.stop();
    updatePosition.value = void 0;
    if (!(IN_BROWSER && data.isActive.value && props.positionStrategy))
      return;
    scope = effectScope();
    await nextTick();
    scope.run(() => {
      var _a;
      const strategy = typeof props.positionStrategy === "function" ? props.positionStrategy : POSITION_STRATEGIES[props.positionStrategy];
      updatePosition.value = (_a = strategy(
        data,
        props,
        contentStyles,
        anchorClasses
      )) == null ? void 0 : _a.updatePosition;
    });
  });
  IN_BROWSER && window.addEventListener("resize", onResize, { passive: true });
  onScopeDispose(() => {
    IN_BROWSER && window.removeEventListener("resize", onResize);
    updatePosition.value = void 0;
    scope == null ? void 0 : scope.stop();
  });
  function onResize(e) {
    var _a;
    (_a = updatePosition.value) == null ? void 0 : _a.call(updatePosition, e);
  }
  return {
    contentStyles,
    anchorClasses,
    updatePosition
  };
}
export {
  POSITION_STRATEGIES,
  makePositionStrategyProps,
  usePositionStrategy
};
//# sourceMappingURL=index.mjs.map
