"use strict";
const common_vendor = require("../../../../common/vendor.js");
const uni_modules_uviewPro_libs_function_deepMerge = require("../function/deepMerge.js");
const IGNORE_REQUEST_KEYS = ["baseUrl", "meta"];
class Request {
  constructor() {
    this.config = {
      baseUrl: "",
      // 请求的根域名
      header: {},
      // 默认的请求头
      method: "POST",
      // 请求方式
      dataType: "json",
      // 设置为json，返回后uni.request会对数据进行一次JSON.parse
      responseType: "text",
      // 此参数无需处理，因为5+和支付宝小程序不支持，默认为text即可
      timeout: 6e4,
      meta: {
        originalData: true,
        // 是否在拦截器中返回服务端的原始数据，见文档说明
        toast: false,
        // 是否在请求出错时，弹出toast
        loading: false
        // 是否显示加载中
      }
    };
    this.interceptor = {
      request: null,
      response: null
    };
  }
  /**
   * 将全局配置合并到本次请求的 options 中
   * - 忽略 IGNORE_REQUEST_KEYS 中的字段（如 meta）
   * - 对 header 使用深合并（全局 header 为默认，options.header 优先）
   * - 对对象类型的字段尝试深合并，基础类型以 options 值优先
   * - 处理 baseUrl：若存在全局 baseUrl 且 options.url 非完整 url（非 http 开头），则合并成完整 URL
   */
  mergeGlobalConfigToOptions(options) {
    const mergedOptions = { ...options };
    for (const key of Object.keys(this.config)) {
      if (IGNORE_REQUEST_KEYS.includes(key)) {
        continue;
      }
      const cfgVal = this.config[key];
      const optVal = options[key];
      if (cfgVal === void 0)
        continue;
      if (key === "header") {
        mergedOptions.header = uni_modules_uviewPro_libs_function_deepMerge.deepMerge(cfgVal || {}, optVal || {});
        continue;
      }
      if (typeof cfgVal === "string" || typeof cfgVal === "number" || typeof cfgVal === "boolean") {
        mergedOptions[key] = optVal !== void 0 ? optVal : cfgVal;
        continue;
      }
      if (typeof cfgVal === "object" && !Array.isArray(cfgVal)) {
        mergedOptions[key] = uni_modules_uviewPro_libs_function_deepMerge.deepMerge(cfgVal || {}, optVal || {});
        continue;
      }
      if (optVal === void 0) {
        mergedOptions[key] = cfgVal;
      }
    }
    const baseUrl = this.config.baseUrl;
    if (baseUrl && mergedOptions.url && typeof mergedOptions.url === "string" && mergedOptions.url.indexOf("http") !== 0) {
      mergedOptions.url = baseUrl + (mergedOptions.url.indexOf("/") === 0 ? mergedOptions.url : `/${mergedOptions.url}`);
    }
    if (!mergedOptions.url) {
      mergedOptions.url = "";
    }
    return mergedOptions;
  }
  /**
   * 设置全局默认配置
   * @param customConfig 自定义配置
   */
  setConfig(customConfig) {
    this.config = uni_modules_uviewPro_libs_function_deepMerge.deepMerge(this.config, customConfig);
  }
  /**
   * 主要请求部分
   * @param options 请求参数
   */
  request(options) {
    const mergedMeta = {
      ...this.config.meta,
      ...options.meta || {}
    };
    options.meta = mergedMeta;
    options.url = options.url || "";
    options.params = options.params || {};
    options = this.mergeGlobalConfigToOptions(options);
    if (this.interceptor.request && typeof this.interceptor.request === "function") {
      const interceptorRequest = this.interceptor.request(options);
      if (!interceptorRequest) {
        return new Promise(() => {
        });
      }
      this.options = interceptorRequest;
    }
    return new Promise((resolve, reject) => {
      options.complete = (response) => {
        const meta = options.meta || this.config.meta || {};
        const originalData = meta.originalData ?? false;
        response.config = options;
        if (originalData) {
          if (this.interceptor.response && typeof this.interceptor.response === "function") {
            const resInterceptors = this.interceptor.response(response);
            if (resInterceptors !== false) {
              resolve(resInterceptors);
            } else {
              reject(response);
            }
          } else {
            resolve(response);
          }
        } else {
          if (response.statusCode === 200) {
            if (this.interceptor.response && typeof this.interceptor.response === "function") {
              const resInterceptors = this.interceptor.response(response.data);
              if (resInterceptors !== false) {
                resolve(resInterceptors);
              } else {
                reject(response.data);
              }
            } else {
              resolve(response.data);
            }
          } else {
            reject(response);
          }
        }
      };
      common_vendor.index.request(options);
    });
  }
  get(url, data = {}, options = {}) {
    return this.request({
      method: "GET",
      url,
      data,
      header: options.header || {},
      meta: options.meta
    });
  }
  post(url, data = {}, options = {}) {
    return this.request({
      url,
      method: "POST",
      data,
      header: options.header || {},
      meta: options.meta
    });
  }
  put(url, data = {}, options = {}) {
    return this.request({
      url,
      method: "PUT",
      data,
      header: options.header || {},
      meta: options.meta
    });
  }
  delete(url, data = {}, options = {}) {
    return this.request({
      url,
      method: "DELETE",
      data,
      header: options.header || {},
      meta: options.meta
    });
  }
}
const httpInstance = new Request();
exports.httpInstance = httpInstance;
//# sourceMappingURL=../../../../../.sourcemap/mp-weixin/uni_modules/uview-pro/libs/request/index.js.map
