"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const index = require("../platform/index.js");
const utils = require("../utils.js");
const isURLSameOrigin = require("./isURLSameOrigin.js");
const cookies = require("./cookies.js");
const buildFullPath = require("../core/buildFullPath.js");
const mergeConfig = require("../core/mergeConfig.js");
const AxiosHeaders = require("../core/AxiosHeaders.js");
const buildURL = require("./buildURL.js");
const FORM_DATA_CONTENT_HEADERS = ["content-type", "content-length"];
function setFormDataHeaders(headers, formHeaders, policy) {
  if (policy !== "content-only") {
    headers.set(formHeaders);
    return;
  }
  Object.entries(formHeaders).forEach(([key, val]) => {
    if (FORM_DATA_CONTENT_HEADERS.includes(key.toLowerCase())) {
      headers.set(key, val);
    }
  });
}
const encodeUTF8 = (str) => encodeURIComponent(str).replace(
  /%([0-9A-F]{2})/gi,
  (_, hex) => String.fromCharCode(parseInt(hex, 16))
);
const resolveConfig = (config) => {
  const newConfig = mergeConfig.default({}, config);
  const own = (key) => utils.default.hasOwnProp(newConfig, key) ? newConfig[key] : void 0;
  const data = own("data");
  let withXSRFToken = own("withXSRFToken");
  const xsrfHeaderName = own("xsrfHeaderName");
  const xsrfCookieName = own("xsrfCookieName");
  let headers = own("headers");
  const auth = own("auth");
  const baseURL = own("baseURL");
  const allowAbsoluteUrls = own("allowAbsoluteUrls");
  const url = own("url");
  newConfig.headers = headers = AxiosHeaders.default.from(headers);
  newConfig.url = buildURL.default(
    buildFullPath.default(baseURL, url, allowAbsoluteUrls),
    config.params,
    config.paramsSerializer
  );
  if (auth) {
    headers.set(
      "Authorization",
      "Basic " + btoa((auth.username || "") + ":" + (auth.password ? encodeUTF8(auth.password) : ""))
    );
  }
  if (utils.default.isFormData(data)) {
    if (index.default.hasStandardBrowserEnv || index.default.hasStandardBrowserWebWorkerEnv) {
      headers.setContentType(void 0);
    } else if (utils.default.isFunction(data.getHeaders)) {
      setFormDataHeaders(headers, data.getHeaders(), own("formDataHeaderPolicy"));
    }
  }
  if (index.default.hasStandardBrowserEnv) {
    if (utils.default.isFunction(withXSRFToken)) {
      withXSRFToken = withXSRFToken(newConfig);
    }
    const shouldSendXSRF = withXSRFToken === true || withXSRFToken == null && isURLSameOrigin.default(newConfig.url);
    if (shouldSendXSRF) {
      const xsrfValue = xsrfHeaderName && xsrfCookieName && cookies.default.read(xsrfCookieName);
      if (xsrfValue) {
        headers.set(xsrfHeaderName, xsrfValue);
      }
    }
  }
  return newConfig;
};
exports.default = resolveConfig;
