import { isClient as d } from "@vueuse/core";
import { computed as c, isRef as v, unref as u, ref as f, watch as p } from "vue";
const a = /* @__PURE__ */ new WeakMap(), o = /* @__PURE__ */ new Set();
d && document.addEventListener("fullscreenchange", () => {
  if (o.forEach((e) => e.value = !1), document.fullscreenElement) {
    const e = a.get(
      document.fullscreenElement
    );
    e && (e.value = !0);
  }
});
const E = (e = f(null)) => {
  const i = c({
    get: () => u(e),
    set: (t) => {
      v(e) && (e.value = t);
    }
  }), l = f(!1);
  o.add(l), p(
    () => e.value,
    (t, n) => {
      t && a.set(t, l), n && a.delete(n);
    },
    {
      immediate: !0,
      flush: "post"
    }
  );
  async function r(t = !1) {
    await s();
    const n = u(e);
    return n && (t || !document.fullscreenElement) ? (await n.requestFullscreen(), l.value = !0, !0) : !1;
  }
  async function s(t = !1) {
    const n = u(e);
    return t || n && document.fullscreenElement === n ? (await document.exitFullscreen(), l.value = !1, !0) : !1;
  }
  async function m(t = !1) {
    return await (u(l) ? s(t) : r(t));
  }
  return {
    target: i,
    full: c(() => u(l)),
    enter: r,
    exit: s,
    toggle: m
  };
};
export {
  E as useFullScreen
};
