{"size": 3776, "ext": "js", "max_stars_repo_path": "app/utils/nav-builder.js", "max_stars_repo_name": "alphagov-mirror/pay-selfservice", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/utils/nav-builder.js", "max_issues_repo_name": "alphagov-mirror/pay-selfservice", "max_issues_repo_issues_event_min_datetime": "2021-04-22T12:19:42.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-28T03:15:54.000Z", "max_forks_repo_path": "app/utils/nav-builder.js", "max_forks_repo_name": "uk-gov-mirror/alphagov.pay-selfservice", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 74, "max_forks_count": null, "avg_line_length": 34.3272727273, "max_line_length": 145, "alphanum_fraction": 0.7113347458}
'use strict'

const _ = require('lodash')
const paths = require('./../paths')
const formatAccountPathsFor = require('./format-account-paths-for')
const pathLookup = require('./path-lookup')
const formatPSPname = require('./format-PSP-name')

const mainSettingsPaths = [
  paths.account.settings,
  paths.account.digitalWallet,
  paths.account.toggle3ds,
  paths.account.toggleBillingAddress,
  paths.account.emailNotifications,
  paths.account.toggleMotoMaskCardNumberAndSecurityCode
]

const yourPspPaths = [
  paths.account.yourPsp,
  paths.account.credentials,
  paths.account.notificationCredentials
]

const serviceNavigationItems = (currentPath, permissions, type, account = {}) => {
  const navigationItems = []
  navigationItems.push({
    id: 'navigation-menu-home',
    name: 'Dashboard',
    url: formatAccountPathsFor(paths.account.dashboard.index, account.external_id),
    current: pathLookup(currentPath, paths.account.dashboard.index),
    permissions: true
  })
  if (type === 'card') {
    navigationItems.push({
      id: 'navigation-menu-transactions',
      name: 'Transactions',
      url: formatAccountPathsFor(paths.account.transactions.index, account.external_id),
      current: pathLookup(currentPath, paths.account.transactions.index),
      permissions: permissions.transactions_read
    })
    navigationItems.push({
      id: 'navigation-menu-payment-links',
      name: 'Payment links',
      url: formatAccountPathsFor(paths.account.paymentLinks.start, account.external_id),
      current: pathLookup(currentPath, paths.account.paymentLinks.start),
      permissions: permissions.tokens_create
    })
  }
  navigationItems.push({
    id: 'navigation-menu-settings',
    name: 'Settings',
    url: formatAccountPathsFor(paths.account.settings.index, account.external_id),
    current: currentPath !== '/' ? pathLookup(currentPath, [
      ...mainSettingsPaths,
      ...yourPspPaths,
      paths.account.apiKeys,
      paths.account.paymentTypes
    ]) : false,
    permissions: _.some([
      permissions.tokens_read,
      permissions.gateway_credentials_read,
      permissions.payment_types_read,
      permissions.toggle_3ds_read,
      permissions.email_notification_template_read
    ], Boolean)
  })

  return navigationItems
}

const adminNavigationItems = (currentPath, permissions, type, paymentProvider, account = {}) => {
  const apiKeysPath = formatAccountPathsFor(paths.account.apiKeys.index, account.external_id)

  return [
    {
      id: 'navigation-menu-settings-home',
      name: 'Settings',
      url: formatAccountPathsFor(paths.account.settings.index, account.external_id),
      current: pathLookup(currentPath, mainSettingsPaths),
      permissions: type === 'card'
    },
    {
      id: 'navigation-menu-api-keys',
      name: 'API keys',
      url: apiKeysPath,
      current: pathLookup(currentPath, paths.account.apiKeys.index),
      permissions: permissions.tokens_update
    },
    {
      id: 'navigation-menu-your-psp',
      name: `Your PSP - ${formatPSPname(paymentProvider)}`,
      url: formatAccountPathsFor(paths.account.yourPsp.index, account.external_id),
      current: pathLookup(currentPath, yourPspPaths),
      permissions: permissions.gateway_credentials_update && type === 'card' && (paymentProvider !== 'stripe') && (paymentProvider !== 'sandbox')
    },
    {
      id: 'navigation-menu-payment-types',
      name: 'Card types',
      url: formatAccountPathsFor(paths.account.paymentTypes.index, account.external_id),
      current: pathLookup(currentPath, paths.account.paymentTypes.index),
      permissions: permissions.payment_types_read && type === 'card'
    }
  ]
}

module.exports = {
  serviceNavigationItems: serviceNavigationItems,
  adminNavigationItems: adminNavigationItems
}
