import { sha256, aes } from "./crypto.mjs";
import "../../../node_modules/element-plus/es/index.mjs";
/* empty css                                                       */
/* empty css                                                             */
import v4 from "../../../node_modules/uuid/dist/esm-browser/v4.mjs";
import { ElLoading } from "../../../node_modules/element-plus/es/components/loading/index.mjs";
const LoadingInstance = {
  target: null,
  // 保存Loading实例
  count: 0
};
function successRollback(resolve, res) {
  closeLoading();
  resolve(res);
}
function errRollback(reject, err) {
  closeLoading();
  reject(err.data || err);
}
function paramHandler(params, showLoading, isUpload = false) {
  params = params || {};
  if (!isUpload && $data_crypto) {
    let paramsObj;
    if (Array.isArray(params)) {
      paramsObj = {
        oriArrayParams: JSON.stringify(params)
      };
    } else {
      paramsObj = Object.assign({}, params);
    }
    let file = paramsObj.file;
    paramsObj.nonce = v4();
    paramsObj.timestamp = Date.now();
    if ($data_verify_sign && JSON.stringify(paramsObj) != "{}") {
      var keysArr = Object.keys(paramsObj).sort();
      params = {};
      let paramStr = "";
      for (var i in keysArr) {
        let key = keysArr[i];
        if (key == "file") {
          continue;
        }
        let value = paramsObj[key];
        if (!value && value !== false && value != 0) {
          value = "";
        }
        params[key] = value;
        if (typeof value == "object") {
          value = JSON.stringify(
            value,
            (key2, value2) => value2 === null ? void 0 : value2
          );
        }
        paramStr += key + "=" + value + "&";
      }
      let signMsg = sha256(paramStr);
      params.sig = signMsg;
    } else {
      params = paramsObj;
    }
    let userInfo = window.$store.getters.user;
    params.uid = userInfo.userId;
    if ($data_crypto) {
      if ("{}" != JSON.stringify(params)) {
        let paramStr = encodeURIComponent(JSON.stringify(params));
        params = { request: aes.en(paramStr) };
      }
    } else {
      if (params["oriArrayParams"]) {
        params = JSON.parse(params["oriArrayParams"]);
      }
    }
    if (file) {
      params.file = file;
    }
  }
  if (showLoading === true) {
    LoadingInstance.count++;
    if (LoadingInstance.count === 1) {
      LoadingInstance.target = ElLoading.service({
        fullscreen: true,
        lock: true,
        text: "拼命加载中",
        // spinner: loadingSvg,
        background: "rgba(0, 0, 0, 0)",
        customClass: "h-loading"
      });
    }
  }
  return params || {};
}
function closeLoading() {
  if (LoadingInstance.count > 0) {
    LoadingInstance.count--;
  }
  if (LoadingInstance.count === 0 && LoadingInstance.target) {
    LoadingInstance.target.close();
    LoadingInstance.target = null;
  }
}
export {
  errRollback,
  paramHandler,
  successRollback
};
