import p from "axios";
import q from "qs";
import { C as r } from "./base-PQGakkrl.js";
class b {
  requestPendingList;
  globalOptions;
  constructor() {
    this.requestPendingList = /* @__PURE__ */ new Map(), this.globalOptions = {}, this._setInterceptors();
  }
  _initRequest(e, t, s) {
    const a = this, i = this._getExtInfo(s.ext);
    return new Promise((n) => {
      const l = /* @__PURE__ */ new Date();
      let h = {
        url: e,
        timeout: 1e3 * 15,
        responseType: "json",
        retry: 3,
        retryDelay: 1e3
        //_requestKeyMerge: ["method", "url", "_ran"],
      };
      h = r.getJsonMerge(h, s.options, t), p(h).then((o) => {
        if (s.ext._operateType === "fileDownBinary")
          s.ext.uploadDownBinaryCallback(o, i, n);
        else {
          const c = i.resultCallback ? i.resultCallback(o) : a._initResult(o, i.resultCode);
          a._initRequestResolve(n, c, i, l);
        }
      }).catch((o) => {
        if (i.errorCallback && o.response) {
          const c = i.errorCallback(o);
          c && (c.then && r.isFunction(c.then) ? c.then((u) => {
            a._initRequestError(o, n, u, i, l);
          }) : a._initRequestError(o, n, c, i, l));
        } else {
          let c = "", u = "";
          o.response ? c = o.response.status.toString() : o.code === "ECONNABORTED" ? u = "Axios:TimeOut  Repeat" : r.isEmpty(o.message) || (u = `Axios:${r.isEmpty(o.code) ? "" : o.code + "-"}${o.message}`);
          const m = a._initResultErr(c, u);
          a._initRequestError(o, n, m, i, l);
        }
      });
    });
  }
  _initRequestResolve(e, t, s, a) {
    s.isTimeOut ? this._timeOut(a, () => {
      e(t);
    }) : e(t);
  }
  _initRequestError(e, t, s, a, i) {
    p.isCancel(e) || this._initRequestResolve(t, s, a, i);
  }
  initGet(e) {
    const t = {
      method: "get",
      headers: this._getHeader(e.ext),
      params: e.param
    };
    return this._initRequest(e.url, t, e);
  }
  initPost(e) {
    const t = {
      method: "post",
      headers: this._getHeader(e.ext),
      data: JSON.stringify(e.param)
    };
    return this._initRequest(e.url, t, e);
  }
  initPut(e) {
    const t = e.url, s = {
      method: "put",
      headers: this._getHeader(e.ext),
      data: JSON.stringify(e.param)
    };
    return this._initRequest(t, s, e);
  }
  initDelete(e) {
    const t = e.url, s = {
      method: "delete",
      headers: this._getHeader(e.ext),
      data: JSON.stringify(e.param)
    };
    return this._initRequest(t, s, e);
  }
  initFile(e) {
    const t = {
      method: "post"
    }, s = this._getHeader(e.ext);
    t.headers = s;
    const a = new FormData();
    return Object.keys(e.param).forEach((i) => {
      const n = e.param[i];
      r.isArray(n) ? n.forEach((l) => {
        a.append(i, l);
      }) : a.append(i, e.param[i]);
    }), t.data = a, this._initRequest(e.url, t, e);
  }
  initFileDownLoad(e) {
    const t = this, s = {
      method: "post",
      responseType: "blob"
    }, a = r.isEmpty(e.options.method) ? "post" : e.options.method;
    s.method = a;
    const i = this._getHeader(e.ext);
    return i["Content-Type"] = "application/json;", s.headers = i, r.isEmpty(e.param) || (a === "get" ? s.params = e.param : s.data = JSON.stringify(e.param)), e.ext._operateType = "fileDownBinary", e.ext.uploadDownBinaryCallback = (n, l, h) => {
      const o = new FileReader();
      o.onload = function(c) {
        try {
          const u = JSON.parse(c.currentTarget.result), m = l.resultCallback ? l.resultCallback({ data: u }) : t._initResult({ data: u }, l.resultCode);
          m.success || h(m);
        } catch {
          const m = n.headers["content-disposition"];
          let d = "";
          r.isEmpty(m) ? d = e.param && e.param.fileName ? e.param.fileName : (/* @__PURE__ */ new Date()).getTime().toString() : d = window.decodeURIComponent(m.split("=")[1]), d = d.replace(/\"/g, "").replace(/'/g, "");
          let g = "";
          const R = d.lastIndexOf(".");
          R > -1 && (g = d.substring(R + 1));
          let y;
          switch (g) {
            case "xls":
            case "xlsx":
              y = { type: "application/vnd.ms-excel" };
              break;
            case "csv":
              y = { type: "text/csv" };
              break;
            case "doc":
            case "docx":
              y = { type: "application/msword" };
              break;
            case "pdf":
              y = { type: "application/pdf" };
              break;
          }
          const _ = new Blob([n.data], y);
          if (window.navigator && window.navigator.msSaveOrOpenBlob)
            window.navigator.msSaveOrOpenBlob(_, d);
          else {
            const f = document.createElement("a");
            f.href = (window.URL || window.webkitURL).createObjectURL(_), f.download = d, f.style.display = "none", document.body.appendChild(f), f.click(), window.URL.revokeObjectURL(f.href);
          }
          h({ success: !0 });
        }
      }, o.readAsText(n.data);
    }, this._initRequest(e.url, s, e);
  }
  _getHeader(e) {
    const t = {
      "Content-Type": "application/json;charset=UTF-8",
      "X-Requested-With": "XMLHttpRequest"
    };
    if (e && e.headerCallback) {
      const s = e.headerCallback();
      if (!r.isEmpty(s))
        for (const [a, i] of Object.entries(s))
          t[a] = i;
    }
    return t;
  }
  _getExtInfo(e) {
    const t = r.getJsonClone(e);
    return t.isTimeOut = r.isBoolean(t.isTimeOut) ? t.isTimeOut : !0, t;
  }
  _timeOut(e, t) {
    const i = (/* @__PURE__ */ new Date()).getTime() - e.getTime() - 200, n = i > 0 ? 0 : Math.abs(i);
    n <= 0 ? t() : setTimeout(() => {
      t();
    }, n);
  }
  _initResult(e, t) {
    const s = { code: null, success: !1, msg: "", data: null };
    if (e.data) {
      s.code = e.data.code.toString();
      const a = t.success || "200";
      s.code === a ? (s.success = !0, s.data = e.data.data) : (s.success = !1, s.msg = e.data.message, s.data = e.data.data);
    }
    return s;
  }
  _initResultErr(e, t) {
    const s = { code: null, success: !1, msg: "Axios:Unknown error .", data: null }, a = r.isEmpty(t) ? `Axios:Unknown error ${e}` : t;
    return s.msg = a, s;
  }
  _setInterceptors() {
    p.interceptors.request.use((e) => (this.globalOptions.interceptorsRequestCallback && this.globalOptions.interceptorsRequestCallback(e), e && (!r.isEmpty(e.__retryCount) && e.__retryCount > 0 || (this._removeRequestPending(e), this._addRequestPending(e))), e), (e) => Promise.reject(e)), p.interceptors.response.use((e) => (this.globalOptions.interceptorsResponseCallback && this.globalOptions.interceptorsResponseCallback(e), this._removeRequestPending(e.config), e), (e) => {
      if (e.code === "ECONNABORTED" && e.message.indexOf("timeout") > -1) {
        const t = e.config;
        return !t || !t.retry || (t.__retryCount = t.__retryCount || 0, t.__retryCount >= t.retry) ? Promise.reject(e) : (t.__retryCount += 1, new Promise((a) => {
          setTimeout(function() {
            a();
          }, t.retryDelay || 1);
        }).then(function() {
          return p(t);
        }));
      } else
        return Promise.reject(e);
    });
  }
  _getRequestKeyMerge(e) {
    const t = e._requestKeyMerge, s = r.isBoolean(e.isRequestCancelRepeat) ? e.isRequestCancelRepeat : !0, a = [];
    return !r.isEmpty(e) && s && (r.isUndefined(t) ? (a.push(e.method), a.push(e.url), r.isEmpty(e._ran) || a.push(e._ran)) : ["method", "url", "params", "data", "_ran"].forEach((i) => {
      if (r.isExistValue(t, i) && !r.isEmpty(e[i])) {
        let n = e[i];
        i === "params" || i === "data" ? (r.isString(n) && (n = JSON.parse(n)), a.push(q.stringify(n))) : (i === "_ran" && r.isEmpty(n) && (n = (/* @__PURE__ */ new Date()).getTime()), a.push(n));
      }
    })), a.join("&");
  }
  _addRequestPending(e) {
    const t = this._getRequestKeyMerge(e);
    r.isEmpty(t) || (e.cancelToken = e.cancelToken || new p.CancelToken((s) => {
      this.requestPendingList.has(t) || this.requestPendingList.set(t, s);
    }));
  }
  _removeRequestPending(e) {
    const t = this._getRequestKeyMerge(e);
    !r.isEmpty(t) && this.requestPendingList.has(t) && (this.requestPendingList.get(t)(`AxiosCancelRepeatRequest: ${t}`), this.requestPendingList.delete(t));
  }
  cancelAll() {
    const e = [];
    for (const t of this.requestPendingList)
      e.push(t[0]);
    e.forEach((t) => {
      this.requestPendingList.get(t)(`AxiosCancelRepeatRequest: ${t}`), this.requestPendingList.delete(t);
    });
  }
  setResendAgain(e, t = {}) {
    const s = e.config, a = r.isUndefined(t.retryDelay) ? 500 : t.retryDelay;
    return !s || !s.retry ? Promise.reject(e) : new Promise((n) => {
      setTimeout(function() {
        n();
      }, a || 1);
    }).then(function() {
      return s.cancelToken = null, p(s);
    });
  }
  setGlobalOptions(e) {
    this.globalOptions = r.getJsonMerge(this.globalOptions, e || {});
  }
}
const E = new b();
export {
  E as a
};
//# sourceMappingURL=axios-DqkFEq4Y.js.map
