import { Subscribable as i } from "./subscribable.js";
var t = class extends i {
  #e;
  #s;
  #i;
  constructor() {
    super(), this.#i = (s) => {
      if (typeof window < "u" && window.addEventListener) {
        const e = () => s();
        return window.addEventListener("visibilitychange", e, !1), () => {
          window.removeEventListener("visibilitychange", e);
        };
      }
    };
  }
  onSubscribe() {
    this.#s || this.setEventListener(this.#i);
  }
  onUnsubscribe() {
    this.hasListeners() || (this.#s?.(), this.#s = void 0);
  }
  setEventListener(s) {
    this.#i = s, this.#s?.(), this.#s = s((e) => {
      typeof e == "boolean" ? this.setFocused(e) : this.onFocus();
    });
  }
  setFocused(s) {
    this.#e !== s && (this.#e = s, this.onFocus());
  }
  onFocus() {
    const s = this.isFocused();
    this.listeners.forEach((e) => {
      e(s);
    });
  }
  isFocused() {
    return typeof this.#e == "boolean" ? this.#e : globalThis.document?.visibilityState !== "hidden";
  }
}, o = new t();
export {
  t as FocusManager,
  o as focusManager
};
