"use strict";
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
const transformData = require("./transformData.js");
const isCancel = require("../cancel/isCancel.js");
const index = require("../defaults/index.js");
const CanceledError = require("../cancel/CanceledError.js");
const AxiosHeaders = require("./AxiosHeaders.js");
const adapters = require("../adapters/adapters.js");
function throwIfCancellationRequested(config) {
  if (config.cancelToken) {
    config.cancelToken.throwIfRequested();
  }
  if (config.signal && config.signal.aborted) {
    throw new CanceledError.default(null, config);
  }
}
function dispatchRequest(config) {
  throwIfCancellationRequested(config);
  config.headers = AxiosHeaders.default.from(config.headers);
  config.data = transformData.default.call(config, config.transformRequest);
  if (["post", "put", "patch"].indexOf(config.method) !== -1) {
    config.headers.setContentType("application/x-www-form-urlencoded", false);
  }
  const adapter = adapters.default.getAdapter(config.adapter || index.default.adapter, config);
  return adapter(config).then(
    function onAdapterResolution(response) {
      throwIfCancellationRequested(config);
      config.response = response;
      try {
        response.data = transformData.default.call(config, config.transformResponse, response);
      } finally {
        delete config.response;
      }
      response.headers = AxiosHeaders.default.from(response.headers);
      return response;
    },
    function onAdapterRejection(reason) {
      if (!isCancel.default(reason)) {
        throwIfCancellationRequested(config);
        if (reason && reason.response) {
          config.response = reason.response;
          try {
            reason.response.data = transformData.default.call(
              config,
              config.transformResponse,
              reason.response
            );
          } finally {
            delete config.response;
          }
          reason.response.headers = AxiosHeaders.default.from(reason.response.headers);
        }
      }
      return Promise.reject(reason);
    }
  );
}
exports.default = dispatchRequest;
