import { a as l } from "./tabbable.CcMz3mIL.js";
function* h(t = document.activeElement) {
  t != null && (yield t, "shadowRoot" in t && t.shadowRoot && t.shadowRoot.mode !== "closed" && (yield* h(t.shadowRoot.activeElement)));
}
function d() {
  return [...h()].pop();
}
let n = [];
function f() {
  return [...n];
}
class v {
  constructor(e) {
    this.tabDirection = "forward", this.handleFocusIn = () => {
      this.isActive() && this.checkFocus();
    }, this.handleKeyDown = (o) => {
      if (o.key !== "Tab" || this.isExternalActivated || !this.isActive())
        return;
      const c = d();
      if (this.previousFocus = c, this.previousFocus && this.possiblyHasTabbableChildren(this.previousFocus))
        return;
      o.shiftKey ? this.tabDirection = "backward" : this.tabDirection = "forward";
      const s = l(this.element);
      let i = s.findIndex(
        (r) => r === c
      );
      this.previousFocus = this.currentFocus;
      const a = this.tabDirection === "forward" ? 1 : -1;
      for (; ; ) {
        i + a >= s.length ? i = 0 : i + a < 0 ? i = s.length - 1 : i += a, this.previousFocus = this.currentFocus;
        const r = (
          /** @type {HTMLElement} */
          s[i]
        );
        if (this.tabDirection === "backward" && this.previousFocus && this.possiblyHasTabbableChildren(this.previousFocus) || r && this.possiblyHasTabbableChildren(r))
          return;
        o.preventDefault(), this.currentFocus = r, this.currentFocus?.focus({ preventScroll: !1 });
        const u = [...h()];
        if (u.includes(this.currentFocus) || !u.includes(this.previousFocus))
          break;
      }
      setTimeout(() => this.checkFocus());
    }, this.handleKeyUp = () => {
      this.tabDirection = "forward";
    }, this.element = e, this.elementsWithTabbableControls = ["iframe"];
  }
  /** Activates focus trapping. */
  activate() {
    n.push(this.element), document.addEventListener("focusin", this.handleFocusIn), document.addEventListener("keydown", this.handleKeyDown), document.addEventListener("keyup", this.handleKeyUp);
  }
  /** Deactivates focus trapping. */
  deactivate() {
    n = n.filter((e) => e !== this.element), this.currentFocus = null, document.removeEventListener("focusin", this.handleFocusIn), document.removeEventListener("keydown", this.handleKeyDown), document.removeEventListener("keyup", this.handleKeyUp);
  }
  /** Determines if this modal element is currently active or not. */
  isActive() {
    return n[n.length - 1] === this.element;
  }
  /** Activates external modal behavior and temporarily disables focus trapping. */
  activateExternal() {
    this.isExternalActivated = !0;
  }
  /** Deactivates external modal behavior and re-enables focus trapping. */
  deactivateExternal() {
    this.isExternalActivated = !1;
  }
  checkFocus() {
    if (this.isActive() && !this.isExternalActivated) {
      const e = l(this.element);
      if (!this.element.matches(":focus-within")) {
        const o = e[0], c = e[e.length - 1], s = this.tabDirection === "forward" ? o : c;
        typeof s?.focus == "function" && (this.currentFocus = s, s.focus({ preventScroll: !1 }));
      }
    }
  }
  possiblyHasTabbableChildren(e) {
    return this.elementsWithTabbableControls.includes(
      e.tagName.toLowerCase()
    ) || e.hasAttribute("controls");
  }
}
export {
  v as M,
  h as a,
  f as b,
  d as g
};
