import h from "../utils.mjs";
import k from "../core/settle.mjs";
import L from "../helpers/cookies.mjs";
import N from "../helpers/buildURL.mjs";
import c from "../core/buildFullPath.mjs";
import v from "../helpers/isURLSameOrigin.mjs";
import A from "../defaults/transitional.mjs";
import l from "../core/AxiosError.mjs";
import D from "../cancel/CanceledError.mjs";
import q from "../helpers/parseProtocol.mjs";
import R from "../platform/index.mjs";
import C from "../core/AxiosHeaders.mjs";
import O from "../helpers/speedometer.mjs";
function H(e, y) {
  let f = 0;
  const u = O(50, 250);
  return (i) => {
    const s = i.loaded, o = i.lengthComputable ? i.total : void 0, a = s - f, n = u(a), E = s <= o;
    f = s;
    const m = {
      loaded: s,
      total: o,
      progress: o ? s / o : void 0,
      bytes: a,
      rate: n || void 0,
      estimated: n && o && E ? (o - s) / n : void 0,
      event: i
    };
    m[y ? "download" : "upload"] = !0, e(m);
  };
}
const S = typeof XMLHttpRequest < "u", G = S && function(e) {
  return new Promise(function(f, u) {
    let i = e.data;
    const s = C.from(e.headers).normalize();
    let { responseType: o, withXSRFToken: a } = e, n;
    function E() {
      e.cancelToken && e.cancelToken.unsubscribe(n), e.signal && e.signal.removeEventListener("abort", n);
    }
    let m;
    if (h.isFormData(i)) {
      if (R.hasStandardBrowserEnv || R.hasStandardBrowserWebWorkerEnv)
        s.setContentType(!1);
      else if ((m = s.getContentType()) !== !1) {
        const [r, ...d] = m ? m.split(";").map((p) => p.trim()).filter(Boolean) : [];
        s.setContentType([r || "multipart/form-data", ...d].join("; "));
      }
    }
    let t = new XMLHttpRequest();
    if (e.auth) {
      const r = e.auth.username || "", d = e.auth.password ? unescape(encodeURIComponent(e.auth.password)) : "";
      s.set("Authorization", "Basic " + btoa(r + ":" + d));
    }
    const T = c(e.baseURL, e.url);
    t.open(e.method.toUpperCase(), N(T, e.params, e.paramsSerializer), !0), t.timeout = e.timeout;
    function x() {
      if (!t)
        return;
      const r = C.from(
        "getAllResponseHeaders" in t && t.getAllResponseHeaders()
      ), p = {
        data: !o || o === "text" || o === "json" ? t.responseText : t.response,
        status: t.status,
        statusText: t.statusText,
        headers: r,
        config: e,
        request: t
      };
      k(function(b) {
        f(b), E();
      }, function(b) {
        u(b), E();
      }, p), t = null;
    }
    if ("onloadend" in t ? t.onloadend = x : t.onreadystatechange = function() {
      !t || t.readyState !== 4 || t.status === 0 && !(t.responseURL && t.responseURL.indexOf("file:") === 0) || setTimeout(x);
    }, t.onabort = function() {
      t && (u(new l("Request aborted", l.ECONNABORTED, e, t)), t = null);
    }, t.onerror = function() {
      u(new l("Network Error", l.ERR_NETWORK, e, t)), t = null;
    }, t.ontimeout = function() {
      let d = e.timeout ? "timeout of " + e.timeout + "ms exceeded" : "timeout exceeded";
      const p = e.transitional || A;
      e.timeoutErrorMessage && (d = e.timeoutErrorMessage), u(new l(
        d,
        p.clarifyTimeoutError ? l.ETIMEDOUT : l.ECONNABORTED,
        e,
        t
      )), t = null;
    }, R.hasStandardBrowserEnv && (a && h.isFunction(a) && (a = a(e)), a || a !== !1 && v(T))) {
      const r = e.xsrfHeaderName && e.xsrfCookieName && L.read(e.xsrfCookieName);
      r && s.set(e.xsrfHeaderName, r);
    }
    i === void 0 && s.setContentType(null), "setRequestHeader" in t && h.forEach(s.toJSON(), function(d, p) {
      t.setRequestHeader(p, d);
    }), h.isUndefined(e.withCredentials) || (t.withCredentials = !!e.withCredentials), o && o !== "json" && (t.responseType = e.responseType), typeof e.onDownloadProgress == "function" && t.addEventListener("progress", H(e.onDownloadProgress, !0)), typeof e.onUploadProgress == "function" && t.upload && t.upload.addEventListener("progress", H(e.onUploadProgress)), (e.cancelToken || e.signal) && (n = (r) => {
      t && (u(!r || r.type ? new D(null, e, t) : r), t.abort(), t = null);
    }, e.cancelToken && e.cancelToken.subscribe(n), e.signal && (e.signal.aborted ? n() : e.signal.addEventListener("abort", n)));
    const w = q(T);
    if (w && R.protocols.indexOf(w) === -1) {
      u(new l("Unsupported protocol " + w + ":", l.ERR_BAD_REQUEST, e));
      return;
    }
    t.send(i || null);
  });
};
export {
  G as default
};
