import Api from 'socket-api'

export const api = Api('http://localhost:3000', {
  // timeout: 10 * 1000,
  // keepConnection: 30000,
  socket: {secure: true, rejectUnauthorized: false, verify: false},
})

export const loadList = async ({method, params = {}, filter, select, type}) => {
  let skip = 0
  const getChunk = () => api[method]({...params, query: {filter, select, skip, type}})
  const result = {}
  for (;;) {
    const {data, total} = await getChunk()
    data.reduce((acc, {_id: id, ...rest}) => {
      acc[id] = Object.keys(rest).length ? rest : null
      return acc
    }, result)
    if (skip + data.length >= total) {
      break
    }

    skip += data.length
  }

  return result
}
