/* eslint-disable  */
import axios from 'axios';
import assign from 'lodash/assign';
import toLower from 'lodash/toLower';
// import isArray from 'lodash/isArray';
// import isArrayLikeObject from 'lodash/isArrayLikeObject';
import isFunction from 'lodash/isFunction';
import extendConfig from './defaultConfig';
const globalConfigMatchers = [];
/* no-unused-vars */
const getMatchedConfig = requestConfig => {
  if (!globalConfigMatchers.length) {
    return null;
  }
  for (let i = 0, len = globalConfigMatchers.length; i < len; i += 1) {
    const [matcher, extendConfig] = globalConfigMatchers[i];
    if (matcher(requestConfig)) {
      return extendConfig;
    }

    return null;
  }
};
export const request = (config, ctx) => {
  const {
    url,
    method = 'GET',
    params,
    convertParams,
    converter,
    successMessage,
    ...restAxiosConfig
  } = config;

  let requestOptions = {
    ...restAxiosConfig,
    url,
    method,
  };

  const matchedConfig = getMatchedConfig(requestOptions);

  const curConf = assign({}, globalConfig, matchedConfig);

  const {
    bizRedirectHandler,
    bizDevErrorAdapter,
    bizErrorMessageAdapter,
    bizSuccessMessageAdapter,
    bizNotifyHandler,
    bizDataAdapter,
    bizParamsAdapter,
    bizRequestAdapter,
  } = curConf;

  let finalParams = params || {};

  if (bizParamsAdapter) {
    finalParams = bizParamsAdapter(finalParams);
  }

  const lMethod = toLower(method);

  if (lMethod === 'get' || lMethod === 'delete') {
    requestOptions.params = finalParams;
  } else {
    requestOptions.data = finalParams;
  }

  if (bizRequestAdapter) {
    requestOptions = bizRequestAdapter(requestOptions);
  }

  return axios(requestOptions)
    .then(raw => raw && raw.data)
    .then(
      res =>
        new Promise((resolve, reject) => {
          // 1. 重定向
          if (bizRedirectHandler) {
            bizRedirectHandler(res);
          }

          // 2. 开发级别信息转换
          const errorDesc = bizDevErrorAdapter ? bizDevErrorAdapter(res) : null;

          // 3. 用户级错误信息转换
          const errorMsg = bizErrorMessageAdapter
            ? bizErrorMessageAdapter(res)
            : null;

          // 4. 成功信息转换
          const successMsg = bizSuccessMessageAdapter
            ? bizSuccessMessageAdapter(res, successMessage)
            : null;

          // 5. 消息处理
          if (bizNotifyHandler) {
            bizNotifyHandler({
              errorDesc,
              errorMessage: errorMsg,
              successMessage: successMsg,
            });
          }

          // 生产环境只有 errorMsg 起作用
          if (errorDesc || errorMsg) {
            return reject(errorDesc || errorMsg);
          }

          // 6. 数据转换
          const data = bizDataAdapter ? bizDataAdapter(res) : res;

          resolve(data);
        }),
    );
};

request.defaults = axios.defaults;

request.use = (matcher, extendConfig) => {
  if (isFunction(matcher)) {
    globalConfigMatchers.push([matcher, extendConfig]);
  } else {
    globalConfig = assign({}, globalConfig, matcher);
  }
};
