/**
 * @flow
 */
import { RandomNumBoth } from '../Tool';
import localData from './localData';


// 获取用户保单列表
function get001001(param) {
  const { policy } = localData;
  const result = [];
  console.log('policy:', policy);
  console.log('data:', param);
  policy.forEach((item) => {
    if (item.uid === param.uid) {
      result.push(item);
    }
  });
  return result;
}

//获取保险产品
function get001002() {
  const { product } = localData;
  return product;
}

function get001003(param) {
  const { mult, prem } = param;
  console.log('get001003:', mult, prem, param)
  const reg = /^[1-9]\d*$|^0$/
  let premTotal = Number(prem);
  if (reg.test(mult)) {
    premTotal = Number(prem) * mult;
  }
  return premTotal;
}


const mockMap = {
  '001001': get001001,
  '001002': get001002,
  '001003': get001003,
};


export default function getMockData(url, config, callback) {
  console.log('getMockData:', url, config);
  const time = RandomNumBoth(0, 2);
  // 随机暂停  0.5 - 2.5秒
  console.log('模拟请求：', time, '秒');
  setTimeout(
    () => {
      const result = mockMap[config.APICode](JSON.parse(config.data || JSON.stringify({})));
      console.log('result:', result);
      callback(result);
    },
    (time + 0.5) * 1000,
  );
}
