"use strict";
const common_vendor = require("./vendor.js");
const common_config = require("./config.js");
function buildUrl(path) {
  if (!path)
    return common_config.API_BASE_URL;
  if (path.startsWith("http"))
    return path;
  return common_config.API_BASE_URL + (path.startsWith("/") ? path : "/" + path);
}
function requestWithFallback(options, candidates, idx, resolve, reject) {
  const base = candidates[idx] || common_config.API_BASE_URL;
  const url = options.url.replace(/^https?:\/\/[^/]+/, base);
  common_vendor.index.request({ ...options, url, success: (res) => {
    const { statusCode, data } = res;
    if (statusCode >= 200 && statusCode < 300)
      return resolve(data);
    const msg = data && (data.message || data.error || data.msg) || "HTTP " + statusCode;
    common_vendor.index.showToast({ title: msg, icon: "none" });
    if (idx + 1 < candidates.length)
      return requestWithFallback(options, candidates, idx + 1, resolve, reject);
    reject(new Error(msg));
  }, fail: (err) => {
    if (idx + 1 < candidates.length)
      return requestWithFallback(options, candidates, idx + 1, resolve, reject);
    reject(err);
  } });
}
function get(path, params = {}) {
  return new Promise((resolve, reject) => {
    const headers = { "X-Shop-Id": common_config.SHOP_ID };
    if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
      headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
    const options = { url: buildUrl(path), method: "GET", data: params, header: headers };
    requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
  });
}
function post(path, body = {}) {
  return new Promise((resolve, reject) => {
    const headers = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
    if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
      headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
    const options = { url: buildUrl(path), method: "POST", data: body, header: headers };
    requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
  });
}
function put(path, body = {}) {
  return new Promise((resolve, reject) => {
    const headers = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
    if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
      headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
    const options = { url: buildUrl(path), method: "PUT", data: body, header: headers };
    requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
  });
}
function del(path, body = {}) {
  return new Promise((resolve, reject) => {
    const headers = { "Content-Type": "application/json", "X-Shop-Id": common_config.SHOP_ID };
    if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
      headers["X-User-Id"] = common_config.DEFAULT_USER_ID;
    const options = { url: buildUrl(path), method: "DELETE", data: body, header: headers };
    requestWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
  });
}
function uploadWithFallback(options, candidates, idx, resolve, reject) {
  const base = candidates[idx] || common_config.API_BASE_URL;
  const url = options.url.replace(/^https?:\/\/[^/]+/, base);
  const uploadOptions = { ...options, url };
  common_vendor.index.uploadFile({
    ...uploadOptions,
    success: (res) => {
      const statusCode = res.statusCode || 0;
      if (statusCode >= 200 && statusCode < 300) {
        try {
          const data = typeof res.data === "string" ? JSON.parse(res.data) : res.data;
          return resolve(data);
        } catch (e) {
          return resolve(res.data);
        }
      }
      if (idx + 1 < candidates.length)
        return uploadWithFallback(options, candidates, idx + 1, resolve, reject);
      reject(new Error("HTTP " + statusCode));
    },
    fail: (err) => {
      if (idx + 1 < candidates.length)
        return uploadWithFallback(options, candidates, idx + 1, resolve, reject);
      reject(err);
    }
  });
}
function upload(path, filePath, formData = {}, name = "file") {
  return new Promise((resolve, reject) => {
    const header = { "X-Shop-Id": common_config.SHOP_ID };
    if (common_config.ENABLE_DEFAULT_USER && common_config.DEFAULT_USER_ID)
      header["X-User-Id"] = common_config.DEFAULT_USER_ID;
    const options = { url: buildUrl(path), filePath, name, formData, header };
    uploadWithFallback(options, common_config.API_BASE_URL_CANDIDATES, 0, resolve, reject);
  });
}
exports.del = del;
exports.get = get;
exports.post = post;
exports.put = put;
exports.upload = upload;
//# sourceMappingURL=../../.sourcemap/mp-weixin/common/http.js.map
