const y = {
  endpoint: "",
  path: "/notify",
  token: "",
  timeout: 5e3
};
function d(n) {
  return String(n || "").replace(/\/+$/, "");
}
function m(n) {
  const t = String(n || "/notify");
  return t.startsWith("/") ? t : `/${t}`;
}
function p(n = {}) {
  var t;
  return {
    ...y,
    ...((t = globalThis.$config) == null ? void 0 : t.notify) || {},
    ...n || {}
  };
}
function l(n = {}) {
  const t = async (s, r = {}) => {
    var f;
    const o = p({
      ...n,
      ...r.config || {}
    });
    if (o.enabled === !1)
      return {
        ok: !0,
        skipped: !0,
        message: "notify disabled"
      };
    if (!o.endpoint)
      throw new Error("missing notify.endpoint");
    const c = new AbortController(), g = Number(o.timeout || 5e3), u = setTimeout(() => c.abort(), g);
    try {
      const i = await fetch(`${d(o.endpoint)}${m(o.path)}`, {
        method: "POST",
        headers: {
          "Content-Type": "application/json",
          ...o.token ? { "x-mvframe-notify-token": o.token } : {}
        },
        body: JSON.stringify({
          message: s,
          atMobiles: r.atMobiles,
          atUserIds: r.atUserIds,
          atAll: r.atAll
        }),
        signal: c.signal
      }), e = (i.headers.get("content-type") || "").includes("application/json") ? await i.json() : { message: await i.text() };
      if (!i.ok || (e == null ? void 0 : e.ok) === !1) {
        const a = new Error((e == null ? void 0 : e.message) || ((f = e == null ? void 0 : e.push) == null ? void 0 : f.message) || "notify failed");
        throw a.response = i, a.payload = e, a;
      }
      return e;
    } finally {
      clearTimeout(u);
    }
  };
  return {
    send: t,
    ding: t,
    dingtalk: t
  };
}
async function b(n, t) {
  return l().send(n, t);
}
const T = (n, t = {}) => {
  const s = l(t);
  n.config.globalProperties.$notify = s, globalThis.$notify = s;
};
export {
  l as createNotifyClient,
  T as default,
  p as getNotifyConfig,
  b as notify
};
