const { get } = require('lodash')

const metadataRepo = require('../../../../lib/metadata')
const { updateInvestment } = require('../../repos')
const { valueLabels } = require('../../labels')
const { transformObjectToOption } = require('../../../transformers')

function populateForm (req, res, next) {
  res.locals.form = get(res, 'locals.form', {})
  res.locals.form.labels = valueLabels.edit
  res.locals.form.state = Object.assign({}, res.locals.investmentData, {
    average_salary: get(res.locals.investmentData, 'average_salary.id'),
  })
  res.locals.form.options = {
    averageSalaryRange: metadataRepo.salaryRangeOptions.map(transformObjectToOption),
    fdiValue: metadataRepo.fdiValueOptions.map(transformObjectToOption),
  }

  next()
}

function handleFormPost (req, res, next) {
  res.locals.projectId = req.params.investmentId

  const formattedBody = Object.assign({}, req.body, {
    average_salary: {
      id: req.body.average_salary,
    },
    total_investment: req.body.client_cannot_provide_total_investment === 'true' ? null : req.body.total_investment,
    foreign_equity_investment: req.body.client_cannot_provide_foreign_investment === 'true' ? null : req.body.foreign_equity_investment,
  })

  updateInvestment(req.session.token, res.locals.projectId, formattedBody)
    .then(() => next())
    .catch((err) => {
      if (err.statusCode === 400) {
        res.locals.form = Object.assign(
          {},
          get(res, 'locals.form', {}),
          {
            state: req.body,
            errors: {
              messages: err.error,
            },
          },
        )

        if (!req.body.client_cannot_provide_total_investment) {
          delete res.locals.form.errors.messages.total_investment
          res.locals.form.errors.messages.client_cannot_provide_total_investment = ['This field is required']
        }
        if (!req.body.client_cannot_provide_foreign_investment) {
          delete res.locals.form.errors.messages.foreign_equity_investment
          res.locals.form.errors.messages.client_cannot_provide_foreign_investment = ['This field is required']
        }

        next()
      } else {
        next(err)
      }
    })
}

module.exports = {
  populateForm,
  handleFormPost,
}

@@ -1,19 +1,34 @@
 const %7B 
+assign, get, mer
 ge
-t
  %7D = req
@@ -47,65 +47,8 @@
 ')%0A%0A
-const metadataRepo = require('../../../../lib/metadata')%0A
 cons
@@ -151,39 +151,26 @@
 const %7B 
-transformObjectTo
+get
 Option
+s
  %7D = req
@@ -188,24 +188,32 @@
 /../
-transformers')%0A%0A
+../lib/options')%0A%0Aasync 
 func
@@ -255,80 +255,221 @@
 %7B%0A  
-res.locals.form = get(res, 'locals.form', %7B%7D)%0A  res.locals.form.
+const token = req.session.token%0A  const investmentData = get(res, 'locals.investmentData', %7B%7D)%0A  const createdOn = investmentData.created_on%0A%0A  const form = get(res, 'locals.form')%0A  const defaults = %7B%0A    
 labels
- =
+:
  val
@@ -485,64 +485,32 @@
 edit
+,
 %0A  
-res.locals.form.state = Object.assign(%7B%7D, res.locals.
+  state: assign(%7B%7D, 
 inve
@@ -519,32 +519,34 @@
 mentData, %7B%0A    
+  
 average_salary: 
@@ -549,27 +549,16 @@
 ry: get(
-res.locals.
 investme
@@ -591,43 +591,33 @@
 '),%0A
+  
   %7D)
+,
 %0A  
-res.locals.form.
+  
 options
- =
+:
  %7B%0A
+  
     
@@ -640,70 +640,66 @@
 ge: 
-metadataRepo.salaryRan
+await 
 ge
+t
 Options
-.map(transformObjectToOption
+(token, 'salary-range', %7B createdOn %7D
 ),%0A
+  
     
@@ -712,70 +712,145 @@
 ue: 
-metadataRepo.fdiValueOptions.map(transformObjectToOption),%0A  %7D
+await getOptions(token, 'fdi-type', %7B createdOn %7D),%0A    %7D,%0A  %7D%0A%0A  const combined = merge(%7B%7D, defaults, form)%0A  res.locals.form = combined
 %0A%0A  
@@ -976,23 +976,16 @@
 dBody = 
-Object.
 assign(%7B
@@ -1510,15 +1510,8 @@
 m = 
-Object.
 assi

Update investment value form to use active options