// Common modules
import '../../../common'

const browser = require('../../../browser')
const endpoints = require('../../../api')
const getApiData = require('../../../get-api-data')
const querystring = require('../../../get-url-parameter')
const templating = require('../../../template-render')

const onRenderCallback = () => {
  browser.loaded()
}

const formatAddress = (addressObj) => {
  const addressParts = ['street1', 'street2', 'street3', 'city']
  const formattedAddress = addressParts
    .map((p) => addressObj[p].trim())
    .filter((p) => p.length > 0)
    .join(', ')
  return `${formattedAddress}. ${addressObj.postcode}`
}

const formatSupportOffered = (supportOffered) => {
  if (supportOffered.length === 0) return 'None'
  return supportOffered.join(', ')
}

const formatFeatures = (features) => {
  const readable = ['No', 'Yes', 'Don\'t Know/Ask']
  const boolDiscFields = ['acceptsHousingBenefit', 'acceptsNoHousingBenefitWithServiceProviderSupport', 'acceptsPets', 'acceptsCouples', 'hasDisabledAccess', 'isSuitableForWomen', 'isSuitableForYoungPeople', 'hasSingleRooms', 'hasSharedRooms', 'hasShowerBathroomFacilities', 'hasAccessToKitchen', 'hasFlexibleMealTimes', 'hasLounge', 'providesCleanBedding', 'allowsVisitors', 'allowsChildren', 'hasOnSiteManager', 'referenceReferralIsRequired', 'foodIsIncluded']
  boolDiscFields
    .forEach((f) => {
      features[`${f}Formatted`] = readable[features[f]]
    })
  return features
}

browser.loading()

getApiData
  .data(`${endpoints.accommodation}/${querystring.parameter('id')}`)
  .then((result) => {
  console.log(result.data.features)
    result.data.address.formattedAddress = formatAddress(result.data.address)
    result.data.generalInfo.formattedSupportOffered = formatSupportOffered(result.data.generalInfo.supportOffered)
    result.data.features = formatFeatures(result.data.features)

    templating.renderTemplate('js-template', result.data, 'js-template-placeholder', onRenderCallback)
  }, (e) => {
    browser.redirect('/500')
  })

@@ -489,16 +489,50 @@
 ssParts%0A
+    .filter((p) =%3E addressObj%5Bp%5D)%0A
     .map
@@ -1462,16 +1462,77 @@
 %0A    %7D)%0A
+  if (features.price === 0) %7B%0A    features.price = 'TBC'%0A  %7D%0A
   return
@@ -1661,24 +1661,26 @@
 esult) =%3E %7B%0A
+  
   console.lo
@@ -1692,25 +1692,16 @@
 ult.data
-.features
 )%0A    re

fix for null address fields