import * as n from "react";
import * as F from "react-dom";
import O from "../../../react-fast-compare/index.es.js";
import { fromEntries as v, useIsomorphicLayoutEffect as b } from "./utils.es.js";
import { createPopper as P } from "../../../@popperjs/core/lib/popper.es.js";
var U = [], h = function(o, p, r) {
  r === void 0 && (r = {});
  var i = n.useRef(null), e = {
    onFirstUpdate: r.onFirstUpdate,
    placement: r.placement || "bottom",
    strategy: r.strategy || "absolute",
    modifiers: r.modifiers || U
  }, l = n.useState({
    styles: {
      popper: {
        position: e.strategy,
        left: "0",
        top: "0"
      },
      arrow: {
        position: "absolute"
      }
    },
    attributes: {}
  }), m = l[0], S = l[1], d = n.useMemo(function() {
    return {
      name: "updateState",
      enabled: !0,
      phase: "write",
      fn: function(u) {
        var f = u.state, y = Object.keys(f.elements);
        F.flushSync(function() {
          S({
            styles: v(y.map(function(s) {
              return [s, f.styles[s] || {}];
            })),
            attributes: v(y.map(function(s) {
              return [s, f.attributes[s]];
            }))
          });
        });
      },
      requires: ["computeStyles"]
    };
  }, []), c = n.useMemo(function() {
    var a = {
      onFirstUpdate: e.onFirstUpdate,
      placement: e.placement,
      strategy: e.strategy,
      modifiers: [].concat(e.modifiers, [d, {
        name: "applyStyles",
        enabled: !1
      }])
    };
    return O(i.current, a) ? i.current || a : (i.current = a, a);
  }, [e.onFirstUpdate, e.placement, e.strategy, e.modifiers, d]), t = n.useRef();
  return b(function() {
    t.current && t.current.setOptions(c);
  }, [c]), b(function() {
    if (!(o == null || p == null)) {
      var a = r.createPopper || P, u = a(o, p, c);
      return t.current = u, function() {
        u.destroy(), t.current = null;
      };
    }
  }, [o, p, r.createPopper]), {
    state: t.current ? t.current.state : null,
    styles: m.styles,
    attributes: m.attributes,
    update: t.current ? t.current.update : null,
    forceUpdate: t.current ? t.current.forceUpdate : null
  };
};
export {
  h as usePopper
};
//# sourceMappingURL=usePopper.es.js.map
