const { assign, find, get, isEmpty } = require('lodash')

const config = require('../../../../config')
const {
  transformCompanyToView,
  transformCompaniesHouseToView,
} = require('../transformers')

const { buildUkOtherCompanyOptions, buildForeignOtherCompanyOptions } = require('../options')
const UK_BRANCH_OF_FOREIGN_COMPANY_ID = 'b0730fc6-fcce-4071-bdab-ba8de4f4fc98'

async function getBusinessTypeOption (token, businessTypeId) {
  const ukOtherCompanyOptions = await buildUkOtherCompanyOptions(token)
  const foreignOtherCompanyOptions = await buildForeignOtherCompanyOptions(token)
  return (
    find(ukOtherCompanyOptions, { value: businessTypeId }) ||
    find(foreignOtherCompanyOptions, { value: businessTypeId })
  )
}

async function getBusinessTypeLabel (token, companiesHouseCategory, businessTypeId) {
  if (companiesHouseCategory) {
    return companiesHouseCategory
  }
  const businessTypeOption = await getBusinessTypeOption(token, businessTypeId)
  if (businessTypeOption) {
    return businessTypeOption.label
  }
}

function isForeignCompany (req, res) {
  if (res.locals.company) {
    return !get(res.locals, 'company.uk_based')
  }

  return req.query.country === 'non-uk'
}

function isOnOneList (req, res) {
  return !isEmpty(get(res.locals, 'company.one_list_group_tier'))
}

function getHeading (company, isForeign) {
  const action = company ? 'Edit' : 'Add'
  const type = isForeign ? 'foreign' : 'UK'

  return `${action} ${type} company`
}

async function renderForm (req, res, next) {
  try {
    if (res.locals.companiesHouseRecord) {
      res.locals = assign({}, res.locals, {
        isCompaniesHouse: true,
        chDetails: transformCompaniesHouseToView(res.locals.companiesHouseRecord),
      })
    }

    const businessType = get(res.locals, 'formData.business_type')
    const businessTypeLabel = await getBusinessTypeLabel(
      req.session.token, res.locals.companiesHouseCategory, businessType
    )
    const showTradingAddress = !isEmpty(get(res.locals, 'formData.trading_address_1'))
    const heading = `${res.locals.company ? 'Edit' : 'Add'} business details`

    if (res.locals.company) {
      res.breadcrumb(res.locals.company.name, `/companies/${res.locals.company.id}`)
      res.breadcrumb('Business details', `/companies/${res.locals.company.id}/business-details`)
    }

    const isForeign = isForeignCompany(req, res)

    res
      .breadcrumb(heading)
      .render('companies/views/_deprecated/edit', {
        isForeign,
        isOnOneList: isOnOneList(req, res),
        companyDetails: res.locals.company ? transformCompanyToView(res.locals.company) : {},
        heading,
        businessTypeLabel,
        showTradingAddress,
        showCompanyNumber: businessType === UK_BRANCH_OF_FOREIGN_COMPANY_ID,
        oneListEmail: config.oneList.email,
      })
  } catch (error) {
    next(error)
  }
}

module.exports = {
  renderForm,
}

@@ -4,16 +4,8 @@
 st %7B
- assign,
  fin
@@ -1196,281 +1196,8 @@
 %0A%7D%0A%0A
-function isOnOneList (req, res) %7B%0A  return !isEmpty(get(res.locals, 'company.one_list_group_tier'))%0A%7D%0A%0Afunction getHeading (company, isForeign) %7B%0A  const action = company ? 'Edit' : 'Add'%0A  const type = isForeign ? 'foreign' : 'UK'%0A%0A  return %60$%7Baction%7D $%7Btype%7D company%60%0A%7D%0A%0A
 asyn
@@ -1308,76 +1308,9 @@
 cals
- = assign(%7B%7D, res.locals, %7B%0A        isCompaniesHouse: true,%0A        
+.
 chDe
@@ -1314,17 +1314,18 @@
 hDetails
-:
+ =
  transfo
@@ -1379,26 +1379,16 @@
 eRecord)
-,%0A      %7D)
 %0A    %7D%0A%0A
@@ -1678,16 +1678,65 @@
 ss_1'))%0A
+    const isForeign = isForeignCompany(req, res)%0A
     cons
@@ -2029,58 +2029,8 @@
  %7D%0A%0A
-    const isForeign = isForeignCompany(req, res)%0A%0A
     
@@ -2143,41 +2143,148 @@
     
-isOnOneList: isOnOneList(req, res
+heading,%0A        businessTypeLabel,%0A        showTradingAddress,%0A        isOnOneList: !isEmpty(get(res.locals.company, 'one_list_group_tier')
 ),%0A 
@@ -2380,80 +2380,8 @@
 %7B%7D,%0A
-        heading,%0A        businessTypeLabel,%0A        showTradingAddress,%0A
     

Refactor add/edit company render form