"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const index = require("../../../../@popperjs/core/dist/index.js");
const fromPairs = require("../../../../lodash-es/fromPairs.js");
const usePopper = (referenceElementRef, popperElementRef, opts = {}) => {
  const stateUpdater = {
    name: "updateState",
    enabled: true,
    phase: "write",
    fn: ({ state }) => {
      const derivedState = deriveState(state);
      Object.assign(states.value, derivedState);
    },
    requires: ["computeStyles"]
  };
  const options = vue.computed(() => {
    const { onFirstUpdate, placement, strategy, modifiers } = vue.unref(opts);
    return {
      onFirstUpdate,
      placement: placement || "bottom",
      strategy: strategy || "absolute",
      modifiers: [
        ...modifiers || [],
        stateUpdater,
        {
          name: "applyStyles",
          enabled: false
        }
      ]
    };
  });
  const instanceRef = vue.shallowRef();
  const states = vue.ref({
    styles: {
      popper: {
        position: vue.unref(options).strategy,
        left: "0",
        top: "0"
      },
      arrow: { position: "absolute" }
    },
    attributes: {}
  });
  const destroy = () => {
    if (!instanceRef.value) return;
    instanceRef.value.destroy();
    instanceRef.value = void 0;
  };
  vue.watch(options, (newOptions) => {
    const instance = vue.unref(instanceRef);
    if (instance) instance.setOptions(newOptions);
  }, { deep: true });
  vue.watch([referenceElementRef, popperElementRef], ([referenceElement, popperElement]) => {
    destroy();
    if (!referenceElement || !popperElement) return;
    instanceRef.value = index.createPopper(referenceElement, popperElement, vue.unref(options));
  });
  vue.onBeforeUnmount(() => {
    destroy();
  });
  return {
    state: vue.computed(() => {
      var _a;
      return { ...((_a = vue.unref(instanceRef)) == null ? void 0 : _a.state) || {} };
    }),
    styles: vue.computed(() => vue.unref(states).styles),
    attributes: vue.computed(() => vue.unref(states).attributes),
    update: () => {
      var _a;
      return (_a = vue.unref(instanceRef)) == null ? void 0 : _a.update();
    },
    forceUpdate: () => {
      var _a;
      return (_a = vue.unref(instanceRef)) == null ? void 0 : _a.forceUpdate();
    },
    instanceRef: vue.computed(() => vue.unref(instanceRef))
  };
};
function deriveState(state) {
  const elements = Object.keys(state.elements);
  return {
    styles: fromPairs.default(elements.map((element) => [element, state.styles[element] || {}])),
    attributes: fromPairs.default(elements.map((element) => [element, state.attributes[element]]))
  };
}
exports.usePopper = usePopper;
