import c from "../utils.mjs";
import I from "../helpers/buildURL.mjs";
import P from "./InterceptorManager.mjs";
import R from "./dispatchRequest.mjs";
import f from "./mergeConfig.mjs";
import x from "./buildFullPath.mjs";
import E from "../helpers/validator.mjs";
import z from "./AxiosHeaders.mjs";
const i = E.validators;
class m {
  constructor(t) {
    this.defaults = t, this.interceptors = {
      request: new P(),
      response: new P()
    };
  }
  /**
   * Dispatch a request
   *
   * @param {String|Object} configOrUrl The config specific for this request (merged with this.defaults)
   * @param {?Object} config
   *
   * @returns {Promise} The Promise to be fulfilled
   */
  request(t, e) {
    typeof t == "string" ? (e = e || {}, e.url = t) : e = t || {}, e = f(this.defaults, e);
    const { transitional: l, paramsSerializer: p, headers: a } = e;
    l !== void 0 && E.assertOptions(l, {
      silentJSONParsing: i.transitional(i.boolean),
      forcedJSONParsing: i.transitional(i.boolean),
      clarifyTimeoutError: i.transitional(i.boolean)
    }, !1), p != null && (c.isFunction(p) ? e.paramsSerializer = {
      serialize: p
    } : E.assertOptions(p, {
      encode: i.function,
      serialize: i.function
    }, !0)), e.method = (e.method || this.defaults.method || "get").toLowerCase();
    let y = a && c.merge(
      a.common,
      a[e.method]
    );
    a && c.forEach(
      ["delete", "get", "head", "post", "put", "patch", "common"],
      (r) => {
        delete a[r];
      }
    ), e.headers = z.concat(y, a);
    const u = [];
    let q = !0;
    this.interceptors.request.forEach(function(o) {
      typeof o.runWhen == "function" && o.runWhen(e) === !1 || (q = q && o.synchronous, u.unshift(o.fulfilled, o.rejected));
    });
    const d = [];
    this.interceptors.response.forEach(function(o) {
      d.push(o.fulfilled, o.rejected);
    });
    let n, s = 0, h;
    if (!q) {
      const r = [R.bind(this), void 0];
      for (r.unshift.apply(r, u), r.push.apply(r, d), h = r.length, n = Promise.resolve(e); s < h; )
        n = n.then(r[s++], r[s++]);
      return n;
    }
    h = u.length;
    let b = e;
    for (s = 0; s < h; ) {
      const r = u[s++], o = u[s++];
      try {
        b = r(b);
      } catch (C) {
        o.call(this, C);
        break;
      }
    }
    try {
      n = R.call(this, b);
    } catch (r) {
      return Promise.reject(r);
    }
    for (s = 0, h = d.length; s < h; )
      n = n.then(d[s++], d[s++]);
    return n;
  }
  getUri(t) {
    t = f(this.defaults, t);
    const e = x(t.baseURL, t.url);
    return I(e, t.params, t.paramsSerializer);
  }
}
c.forEach(["delete", "get", "head", "options"], function(t) {
  m.prototype[t] = function(e, l) {
    return this.request(f(l || {}, {
      method: t,
      url: e,
      data: (l || {}).data
    }));
  };
});
c.forEach(["post", "put", "patch"], function(t) {
  function e(l) {
    return function(a, y, u) {
      return this.request(f(u || {}, {
        method: t,
        headers: l ? {
          "Content-Type": "multipart/form-data"
        } : {},
        url: a,
        data: y
      }));
    };
  }
  m.prototype[t] = e(), m.prototype[t + "Form"] = e(!0);
});
const L = m;
export {
  L as default
};
