import moment from 'moment'
import textTable from 'text-table'

const find = (apiWrapper) => (search, options) => {
  const {
    startdate,
    enddate,
    businessunitids
  } = options
  return new Promise((resolve, reject) => {
    apiWrapper.getActivities({
      startdate,
      enddate,
      businessunitids
    })
    .then((response) => {
      const searchParam = search.toLowerCase()
      const activities = response.activities.activity
      const foundActivities = activities.filter((activity) => {
        const name = activity.product.name.toLowerCase()
        const bookableSlots = parseInt(activity.bookableslots, 10)

        return name.indexOf(searchParam) !== -1 && bookableSlots > 0
      })
      .map((activity) => {
        const startTime = activity.start.timepoint.datetime
        const dayOfWeek = moment(startTime).format('dddd')
        const time = moment(startTime).format('HH:mm')
        const formattedStartTime = `${dayOfWeek} ${time}`

        return [
          activity.id,
          formattedStartTime,
          activity.product.name,
          activity.bookableslots
        ]
      })

      if (foundActivities.length === 0) {
        resolve('No activities found')
      }

      const table = textTable(foundActivities)
      const spacedTable = `\`\`\`${table.toString()}\`\`\``
      resolve(spacedTable)
    })
  })
}

export default find

@@ -915,66 +915,8 @@
 mm')
-%0A        const formattedStartTime = %60$%7BdayOfWeek%7D $%7Btime%7D%60
 %0A%0A  
@@ -967,23 +967,30 @@
     
-formattedStartT
+dayOfWeek,%0A          t
 ime,

Put time and day in separate columns