var u = Object.defineProperty, f = Object.defineProperties;
var d = Object.getOwnPropertyDescriptors;
var i = Object.getOwnPropertySymbols;
var h = Object.prototype.hasOwnProperty, A = Object.prototype.propertyIsEnumerable;
var a = (t, r, o) => r in t ? u(t, r, { enumerable: !0, configurable: !0, writable: !0, value: o }) : t[r] = o, s = (t, r) => {
  for (var o in r || (r = {}))
    h.call(r, o) && a(t, o, r[o]);
  if (i)
    for (var o of i(r))
      A.call(r, o) && a(t, o, r[o]);
  return t;
}, p = (t, r) => f(t, d(r));
import { jsx as E } from "react/jsx-runtime";
import v from "axios";
import { notification as c } from "antd";
import { authProvider as e } from "../AuthProvider.mjs";
import { AuthApi as w, DataApi as l } from "@glodon-aiot/apis";
import { isExpired as x } from "../tool.mjs";
import { DataApiErrors as I } from "./errors.mjs";
import { render as P } from "react-dom";
import $ from "../components/NoPermission/index.mjs";
const g = process.env.NODE_ENV === "development", D = "/api", S = `${window.location.host}`;
function H(t) {
  var o, n;
  const r = (o = e.user) == null ? void 0 : o.token;
  return r && (x(r) && e.signin(), t.headers = (n = t.headers) != null ? n : {}, t.headers = p(s({}, t.headers), {
    // DO: 8. 去掉这个判断
    Authorization: `Bearer ${r}`
  })), t;
}
function m(t, r = !0) {
  const o = v.create({
    baseURL: `//${S}${g ? "/server" : ""}${D + t}`
  });
  return r && o.interceptors.request.use(H), o;
}
const N = m("/auth/v1"), W = new w(N, {
  onHttpStatusError(t, r) {
    if (t === 401) {
      e.signout();
      return;
    }
    throw {
      status: t,
      message: r
    };
  },
  onAppStatusError(t, r) {
    throw {
      status: t,
      message: r
    };
  }
}), k = m("/aiot/data/v1"), _ = new l(k, {
  onHttpStatusError(t, r) {
    if (t === 401) {
      e.signout();
      return;
    }
    c.error({
      message: t === 403 ? "暂无权限" : r
    });
  },
  onAppStatusError(t, r) {
    switch (t) {
      case I.ProductNoPermission:
        P(/* @__PURE__ */ E($, {
          title: r
        }), document.getElementById("root"));
        return;
      default:
        c.error({
          message: r
        });
    }
  }
});
export {
  W as authApi,
  _ as dataApi,
  S as deviceHost,
  g as isDev
};
