"use strict";
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const axios = require("./axios.js");
const httpEnum = require("../enums/httpEnum.js");
const prefix = "javascript:";
const translateStr = (target) => {
  if (typeof target === "string") {
    if (target.startsWith(prefix)) {
      const funcStr = target.split(prefix)[1];
      let result;
      try {
        result = new Function(`${funcStr}`)();
      } catch (error) {
        console.log(error);
        window["$message"].error("js内容解析有误！");
      }
      return result;
    } else {
      return target;
    }
  }
  for (const key in target) {
    if (Object.prototype.hasOwnProperty.call(target, key)) {
      const subTarget = target[key];
      target[key] = translateStr(subTarget);
    }
  }
  return target;
};
const customizeHttp = (targetParams, globalParams) => {
  if (!targetParams || !globalParams) {
    return;
  }
  const {
    // 全局请求源地址
    requestOriginUrl,
    // 全局请求内容
    requestParams: globalRequestParams
  } = globalParams;
  const {
    // 请求地址
    requestUrl,
    // 普通 / sql
    requestContentType,
    // 获取数据的方式
    requestDataType,
    // 请求方式 get/post/del/put/patch
    requestHttpType,
    // 请求体类型 none / form-data / x-www-form-urlencoded / json /xml
    requestParamsBodyType,
    // SQL 请求对象
    requestSQLContent,
    // 请求内容 params / cookie / header / body: 同 requestParamsBodyType
    requestParams: targetRequestParams
  } = targetParams;
  if (requestDataType === httpEnum.RequestDataTypeEnum.STATIC)
    return;
  if (!requestUrl) {
    return;
  }
  let headers = {
    ...globalRequestParams.Header,
    ...targetRequestParams.Header
  };
  headers = translateStr(headers);
  let data = {};
  let params = { ...targetRequestParams.Params };
  params = translateStr(params);
  let formData = new FormData();
  switch (requestParamsBodyType) {
    case httpEnum.RequestBodyEnum.NONE:
      break;
    case httpEnum.RequestBodyEnum.JSON:
      headers["Content-Type"] = httpEnum.ContentTypeEnum.JSON;
      data = translateStr(targetRequestParams.Body["json"]);
      if (typeof data === "string")
        data = JSON.parse(data);
      break;
    case httpEnum.RequestBodyEnum.XML:
      headers["Content-Type"] = httpEnum.ContentTypeEnum.XML;
      data = translateStr(targetRequestParams.Body["xml"]);
      break;
    case httpEnum.RequestBodyEnum.X_WWW_FORM_URLENCODED: {
      headers["Content-Type"] = httpEnum.ContentTypeEnum.FORM_URLENCODED;
      const bodyFormData = targetRequestParams.Body["x-www-form-urlencoded"];
      for (const i in bodyFormData)
        formData.set(i, translateStr(bodyFormData[i]));
      data = formData;
      break;
    }
    case httpEnum.RequestBodyEnum.FORM_DATA: {
      headers["Content-Type"] = httpEnum.ContentTypeEnum.FORM_DATA;
      const bodyFormUrlencoded = targetRequestParams.Body["form-data"];
      for (const i in bodyFormUrlencoded) {
        formData.set(i, translateStr(bodyFormUrlencoded[i]));
      }
      data = formData;
      break;
    }
  }
  if (requestContentType === httpEnum.RequestContentTypeEnum.SQL) {
    headers["Content-Type"] = httpEnum.ContentTypeEnum.JSON;
    data = requestSQLContent;
  }
  try {
    const url = new Function("return `" + `${requestOriginUrl}${requestUrl}`.trim() + "`")();
    return axios.default({
      url,
      method: requestHttpType,
      data,
      params,
      headers
    });
  } catch (error) {
    console.log(error);
    window["$message"].error("URL地址格式有误！");
  }
};
exports.customizeHttp = customizeHttp;
exports.translateStr = translateStr;
