"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __propIsEnum = Object.prototype.propertyIsEnumerable;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __spreadValues = (a, b) => {
  for (var prop in b || (b = {}))
    if (__hasOwnProp.call(b, prop))
      __defNormalProp(a, prop, b[prop]);
  if (__getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(b)) {
      if (__propIsEnum.call(b, prop))
        __defNormalProp(a, prop, b[prop]);
    }
  return a;
};
var __objRest = (source, exclude) => {
  var target = {};
  for (var prop in source)
    if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
      target[prop] = source[prop];
  if (source != null && __getOwnPropSymbols)
    for (var prop of __getOwnPropSymbols(source)) {
      if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
        target[prop] = source[prop];
    }
  return target;
};
var common_vendor = require("../common/vendor.js");
var config_index = require("../config/index.js");
const request = (_a) => {
  var _b = _a, {
    baseUrl = config_index.base_url,
    url,
    loading = true,
    method = "GET",
    data = {}
  } = _b, options = __objRest(_b, [
    "baseUrl",
    "url",
    "loading",
    "method",
    "data"
  ]);
  const { token } = common_vendor.index.getStorageSync("userData");
  return new Promise((resolve, reject) => {
    if (url) {
      loading && common_vendor.index.showLoading();
      common_vendor.index.request(__spreadValues({
        url: `${baseUrl}${url}`,
        method,
        header: {
          Authorization: token || "",
          "ngrok-skip-browser-warning": "69420"
        },
        data,
        success: (res) => {
          if (res.statusCode === 200) {
            resolve(res.data);
          } else if (res.data.error === "Unauthorized") {
            common_vendor.index.showToast({
              title: "\u767B\u5F55\u5DF2\u8FC7\u671F\uFF0C\u8BF7\u91CD\u65B0\u767B\u5F55",
              icon: "none"
            });
            common_vendor.index.redirectTo({
              url: "/pages/login/index"
            });
          } else if (res.statusCode === 502) {
            common_vendor.index.showToast({
              title: "\u670D\u52A1\u6682\u4E0D\u53EF\u7528",
              icon: "error"
            });
            reject("\u670D\u52A1\u6682\u4E0D\u53EF\u7528");
          }
        },
        fail: (err) => {
          reject(err);
        },
        complete: () => {
          common_vendor.index.hideLoading();
        }
      }, options));
    } else {
      reject(new Error("url is must required"));
    }
  });
};
const requestFormData = (_c) => {
  var _d = _c, {
    baseUrl = config_index.base_url,
    url,
    loading = true,
    method = "POST",
    file
  } = _d, options = __objRest(_d, [
    "baseUrl",
    "url",
    "loading",
    "method",
    "file"
  ]);
  const { token } = common_vendor.index.getStorageSync("userData");
  return new Promise((resolve, reject) => {
    if (url) {
      loading && common_vendor.index.showLoading();
      common_vendor.index.uploadFile({
        url: `${baseUrl}${url}`,
        method,
        header: {
          Authorization: token || "",
          "Content-Type": "multipart/form-data",
          "ngrok-skip-browser-warning": "69420"
        },
        filePath: file.path || "no_path",
        name: "file",
        formData: { "file": file },
        success: (res) => {
          resolve(JSON.parse(res.data));
        },
        fail: (err) => {
          reject(err);
        },
        complete: () => {
          loading && common_vendor.index.hideLoading();
        }
      });
    } else {
      reject(new Error("url is must required"));
    }
  });
};
exports.request = request;
exports.requestFormData = requestFormData;
