import mock from 'mockjs-async';

import user from './user.js';

function regMock(list){
  if(list){
    let keys = Object.keys(list)
    keys.map(key=>{
      let pattern;
      if(typeof key === 'string'){
        pattern = new RegExp(key)
      }else{
        pattern=key;
      }
      let config = list[key];
      if(typeof config == 'function'){
        console.log(`[mock]注册 ()${pattern}`)
        mock.mock(pattern,(options)=>{
          return new Promise(reslove=>{
            if(config){
              config(options).then(ret=>{
                console.log(`[mock]response ()${pattern}`,options,ret)
                reslove(ret);
              });
            }else{
              reslove();
            }
          })
          i
        });
      }else{
        let methods = Object.keys(config);
        methods.forEach(method=>{
          console.log(`[mock]注册 (${method})${pattern}`)
          mock.mock(pattern,method,(options)=>{
            return new Promise(reslove=>{
              if(config[method]){
                config[method](options).then(ret=>{
                  console.log(`[mock]response (${method})${pattern}`,options,ret)
                  reslove(ret);
                });
              }else{
                reslove();
              }
            })
            i
          });
        })
        
      }
    })
  }
}
/* eslint-disable */
if(MOCK){
  console.log(`=========MOCK OPEN=======`)
  regMock(user);
}else{
  console.log(`=========MOCK CLOSE=======`)
}
/* eslint-enable */


export default {};