import amosRequest, { singleFetch } from 'amos-processor/lib/fetch/amosRequest';
import { utils, Store } from 'amos-tool';
import { message } from 'amos-framework';
import formatUrl from 'amos-processor/lib/utils/urlFormat';
import SysConsts from 'amos-processor/lib/config/consts';
import payload from './payload';

const lsTool = Store.lsTool;

const defaultPageable = {
  page: 0,
  size: 10
};

const defaultHeaders = {
  appKey: 'YH-PATROL',
  product: 'YH-PATROL_WEB',
};

const compleHeaders = () => {
  return {
    ...defaultHeaders,
    token: getToken()
  };
};

/**
 * 获取 token
 */
const getToken = () => {
  return lsTool.read(SysConsts.token);
}

/**
 * 删除全局变量requests中的地址
 * @param {*} url 
 * @param {*} method 
 */
const delRequest = (url, method) => {
  if (window.requests && window.requests.length > 0) {
    window.requests = window.requests.filter(e => e.url !== url && e.method !== method);
  }
};

/**
 * 构建分页
 */
const buildPageable = (url, pageable = defaultPageable) => {
  let { page, size } = pageable;
  return `${url}?page=${page}&size=${size}`;
};

/**
 * 转换指定值为 JSON
 * @param {array} dataList
 * @param {string} key
 */
const convertDatalist = (dataList = [], key = 'content') => {
  const newDL = dataList.map(d => {
    if (!utils.isNil(d[key]) && utils.isString(d[key])) {
      d[key] = JSON.parse(d[key]);
    }
    return d;
  });
  return newDL;
};

export function commonGet(url) {
  window.setLoading && window.setLoading(true);
  return amosRequest(url, { 
    timeout: 10000000 , 
    method: 'GET',
    headers: compleHeaders(),
    reject(error){
      window.setLoading && window.setLoading(false);
      delRequest(url, 'GET');
      //message.danger(error.message || '请求失败，服务器错误！');
    }
  }).then(data => payload(data));
}

export function commonPost(url, data, params) {
  window.setLoading && window.setLoading(true);
  return amosRequest(url, {
    timeout: 10000000,
    method: 'POST',
    headers: compleHeaders(),
    body: data,
    reject(error){
      delRequest(url, 'POST');
      window.setLoading && window.setLoading(false);
      //message.danger(error.message || '请求失败，服务器错误！');
    }
  }).then(data => payload(data));
}

export function commonPut(url, data, params) {
  window.setLoading && window.setLoading(true);
  return amosRequest(url, {
    timeout: 10000000,
    method: 'PUT',
    headers: compleHeaders(),
    body: data,
    reject(error){
      delRequest(url, 'PUT');
      window.setLoading && window.setLoading(false);
      //message.danger(error.message || '请求失败，服务器错误！');
    }
  }).then(data => payload(data));
}

export function commonDelete(url, data, params) {
  window.setLoading && window.setLoading(true);
  return amosRequest(url, {
    timeout: 10000000 ,
    method: 'DELETE',
    headers: compleHeaders(),
    body: data,
    reject(error){
      window.setLoading && window.setLoading(false);
      //message.danger(error.message || '请求失败，服务器错误！');
    }
  }).then(data => payload(data));
}

export const getWithCookie = (url) => {
  return commonGet(url, {
    hasCookie: true
  });
};

export { amosRequest, singleFetch, convertDatalist, buildPageable, formatUrl };
