import { ref as o, onMounted as p, onUnmounted as L } from "vue";
let u = null;
function S(r) {
  u = r;
}
function h(r, f = { manual: !1 }) {
  const l = o(null), t = o(!1), n = o(null);
  let a = 0;
  const d = () => t.value = !0, c = (e) => n.value = e.error;
  function s() {
    if (u) {
      t.value = !1, n.value = null;
      const v = ++a;
      u(r).then(
        () => {
          v === a && (t.value = !0);
        },
        (m) => {
          v === a && (n.value = m);
        }
      );
      return;
    }
    let e = document.querySelector(`script[src="${r}"]`);
    e ? t.value = !0 : (t.value = !1, n.value = null, e = document.createElement("script"), e.type = "text/javascript", e.async = !0, e.src = r, e.addEventListener("load", d), e.addEventListener("error", c), document.head.appendChild(e)), l.value = e;
  }
  function i() {
    if (!document)
      return;
    if (u) {
      a++, l.value = null, t.value = !1;
      return;
    }
    const e = document.querySelector(`script[src="${r}"]`);
    e && (e.removeEventListener("load", d), e.removeEventListener("error", c), document.head.removeChild(e), l.value = null, t.value = !1);
  }
  return f.manual || (p(s), L(i)), { scriptTag: l, load: s, unload: i, isLoaded: t, error: n };
}
export {
  h as default,
  S as setScriptLoader
};
//# sourceMappingURL=useScriptTag.js.map
