import { computed, unref, shallowRef, ref, watch, onBeforeUnmount } from "vue";
import { createPopper as vn } from "../../../../@popperjs/core/dist/index.mjs";
import u from "../../../../lodash-es/fromPairs.mjs";
const P = (o, i, u2 = {}) => {
  const p = {
    name: "updateState",
    enabled: true,
    phase: "write",
    fn: ({ state: t }) => {
      const a = g(t);
      Object.assign(l.value, a);
    },
    requires: ["computeStyles"]
  }, s = computed(() => {
    const { onFirstUpdate: t, placement: a, strategy: m, modifiers: y } = unref(u2);
    return {
      onFirstUpdate: t,
      placement: a || "bottom",
      strategy: m || "absolute",
      modifiers: [
        ...y || [],
        p,
        { name: "applyStyles", enabled: false }
      ]
    };
  }), r = shallowRef(), l = ref({
    styles: {
      popper: {
        position: unref(s).strategy,
        left: "0",
        top: "0"
      },
      arrow: {
        position: "absolute"
      }
    },
    attributes: {}
  }), c = () => {
    r.value && (r.value.destroy(), r.value = void 0);
  };
  return watch(s, (t) => {
    const a = unref(r);
    a && a.setOptions(t);
  }, {
    deep: true
  }), watch([o, i], ([t, a]) => {
    c(), !(!t || !a) && (r.value = vn(t, a, unref(s)));
  }), onBeforeUnmount(() => {
    c();
  }), {
    state: computed(() => {
      var t;
      return { ...((t = unref(r)) == null ? void 0 : t.state) || {} };
    }),
    styles: computed(() => unref(l).styles),
    attributes: computed(() => unref(l).attributes),
    update: () => {
      var t;
      return (t = unref(r)) == null ? void 0 : t.update();
    },
    forceUpdate: () => {
      var t;
      return (t = unref(r)) == null ? void 0 : t.forceUpdate();
    },
    instanceRef: computed(() => unref(r))
  };
};
function g(o) {
  const i = Object.keys(o.elements), u$1 = u(i.map((s) => [s, o.styles[s] || {}])), p = u(i.map((s) => [s, o.attributes[s]]));
  return {
    styles: u$1,
    attributes: p
  };
}
export {
  P as usePopper
};
