import { jsxs as F, jsx as m } from "react/jsx-runtime";
import { t as v } from "../../classix-DG18itHa.js";
import { useRef as a, useId as k, useState as x, useCallback as E, useEffect as K } from "react";
import { Button as P } from "../button/index.js";
import '../../assets/index42.css';const R = "_popover_ttn4t_1", I = "_left_ttn4t_23", N = "_right_ttn4t_28", b = "_trigger_ttn4t_33", n = {
  popover: R,
  "popover-panel": "_popover-panel_ttn4t_6",
  left: I,
  right: N,
  trigger: b
}, O = ({
  children: s,
  position: g = "left",
  triggerAccessibleLabel: _,
  triggerAriaHasPopup: w,
  triggerButtonContent: y
}) => {
  const c = a(null), p = a(void 0), l = k(), u = a(null), [t, f] = x(!1), i = ({
    initialFocus: e
  } = {}) => {
    p.current = e, f(!0);
  }, o = E(({
    restoreFocus: e = !0
  } = {}) => {
    var r;
    f(!1), e && ((r = u.current) == null || r.focus());
  }, []), h = (e) => {
    t && e.key === "Escape" && o();
  }, D = (e) => {
    if (!t) {
      if (e.key === "ArrowDown") {
        e.preventDefault(), i({
          initialFocus: "first"
        });
        return;
      }
      e.key === "ArrowUp" && (e.preventDefault(), i({
        initialFocus: "last"
      }));
    }
  };
  return K(() => {
    if (!t)
      return;
    const e = (r) => {
      var d;
      (d = c.current) != null && d.contains(r.target) || o({
        restoreFocus: !1
      });
    };
    return document.addEventListener("mousedown", e), () => {
      document.removeEventListener("mousedown", e);
    };
  }, [o, t]), /* @__PURE__ */ F("div", { className: n.popover, onKeyDown: h, ref: c, "data-component": "popover", children: [
    /* @__PURE__ */ m(P, { "aria-controls": t ? l : void 0, "aria-expanded": t, "aria-haspopup": w, "aria-label": _, className: v(n.trigger), onKeyDown: D, onClick: () => t ? o({
      restoreFocus: !1
    }) : i(), ref: u, variant: "minimal", children: y }),
    t && /* @__PURE__ */ m("div", { className: v(n["popover-panel"], n[g]), id: l, children: typeof s == "function" ? s({
      close: o,
      initialFocus: p.current,
      isOpen: t
    }) : s })
  ] });
};
export {
  O as Popover
};
//# sourceMappingURL=index.js.map
