// api基础域名
const BaseAPIURl = require('./baseUrl.js').basePath.BASE_API_URL;
const $api = {
  user: {
    login: { // 模拟用户登陆
      method: 'POST',
      url: `${BaseAPIURl}wx/registered`
    },
    info: {
      method: 'GET',
      url: `${BaseAPIURl}wx/info/:appid`
    },
    getCode: {
      method: 'GET',
      url: `${BaseAPIURl}wx/getOpenid`
    },
    update: {
      method: 'PUT',
      url: `${BaseAPIURl}wx/user`
    },
    appointment: {
      method: 'GET',
      url: `${BaseAPIURl}wx/appointment/:openid`
    },    
    delAppointment: {
      method: 'DELETE',
      url: `${BaseAPIURl}wx/appointment/of/:id`
    },
    building: {
      method: 'GET',
      url: `${BaseAPIURl}wx/collection/:openid`
    },
    delBuilding: {
      method: 'DELETE',
      url: `${BaseAPIURl}wx/collection/:openid`
    }
  },
  dict: {
    list: {
      methods: 'GET',
      url: `${BaseAPIURl}/dict`
    }
  },
  banner: {
    list: {
      method: 'GET',
      url: `${BaseAPIURl}wx/banner`
    }
  },
  building: {
    list: {
      method: 'GET',
      url: `${BaseAPIURl}wx/building/list`
    },
    detail: {
      method: 'GET',
      url: `${BaseAPIURl}/wx/buildingSelectId/:id`
    }
  },
  dynamic: { // 项目
    list: { // 项目列表
      method: 'GET',
      url: `${BaseAPIURl}wx/buildingDynamiceList`
    },
    detail: { // 项目详情
      method: 'GET',
      url: `${BaseAPIURl}wx/buildingDynamiceInfo/:id`
    }
  },
  activity: {
    list: { // 活动列表
      method: 'GET',
      url: `${BaseAPIURl}wx/activity`
    },
    detail: { // 活动详情
      method: 'GET',
      url: `${BaseAPIURl}wx/activity/:id`
    }
  },
  appointment: {
    getByUser: {
      method: 'GET',
      url: `${BaseAPIURl}/wx/appointment/:openid`
    },
    save: {
      method: 'POST',
      url: `${BaseAPIURl}/wx/appointment/:openid`
    }
  },
  collection: {
    getByUser: {
      method: 'GET',
      url: `${BaseAPIURl}/wx/collection/:openid`
    },
    save: {
      method: 'POST',
      url: `${BaseAPIURl}/wx/collection/:openid`
    },
    deleteById: {
      method: 'DELETE',
      url: `${BaseAPIURl}/wx/collection/:openid`
    }
  },
  comment: {
    list: { // 评论列表
      methods: 'GET',
      url: `${BaseAPIURl}/wx/comment/list`
    }
  }
}

const hasKey = (o, k) => Object.keys(o).indexOf(k) > -1

// 获取 api 支持 a.b.c 字符串格式
function get(k = '', urlParams = {}) {
  let found = true
  const api = k.split('.').reduce((acc, key) => {
    if (!found) return acc;

    if (acc === 0) {
      if (hasKey($api, key)) {
        return $api[key]
      } else {
        found = false
        return {}
      }
    } else {
      if (hasKey(acc, key)) {
        return acc[key]
      } else {
        found = false
        return acc
      }
    }
  }, 0)

  return {
    ...api,
    url: api.url.split('/').map((item) => {
      if (item.indexOf(':') > -1) {
        const key = item.replace(':', '')
        return item !== 'https:' && hasKey(urlParams, key) ? urlParams[key] : item
      }

      return item
    }).join('/')
  }
}

module.exports = {
  $api: $api,
  get: get,
}