import { ref as l, watchSyncEffect as c, toValue as f, computed as s, onUnmounted as v } from "vue";
import p from "./useScriptTag.js";
const m = "https://cdn.plaid.com/link/v2/stable/link-initialize.js";
function P(u) {
  const { isLoaded: i, error: r } = p(m), a = l(null), t = l(!1);
  c(() => {
    const e = f(u);
    if (i.value && !(!e.token && !e.receivedRedirectUri)) {
      if (!window.Plaid)
        throw new Error("Error loading Plaid SDK");
      n(), a.value = window.Plaid.create({
        ...e,
        onLoad: () => {
          var o;
          t.value = !0, (o = e.onLoad) == null || o.call(e);
        }
      });
    }
  });
  const d = s(
    () => !r.value && a.value !== null && (i.value || t.value)
  );
  function n() {
    a.value && (a.value.exit({ force: !0 }), a.value.destroy(), t.value = !1);
  }
  return v(n), {
    ready: d,
    error: r,
    open: () => {
      var e;
      return (e = a.value) == null ? void 0 : e.open();
    },
    exit: (e) => {
      var o;
      return (o = a.value) == null ? void 0 : o.exit(e);
    }
  };
}
export {
  P as default
};
//# sourceMappingURL=usePlaidLink.js.map
