import * as R from 'ramda'
import * as Yup from 'yup'

import Autocomplete from 'src/components/inputs/formik/Autocomplete.js'
import { getView } from 'src/pages/Triggers/helper'

const advancedRequirementOptions = [
  { display: 'SMS verification', code: 'sms' },
  { display: 'ID card image', code: 'idCardPhoto' },
  { display: 'ID data', code: 'idCardData' },
  { display: 'Customer camera', code: 'facephoto' },
  { display: 'US SSN', code: 'usSsn' }
]

const alreadyExists = (values, requirement) => {
  return R.isEmpty(R.filter(value => value.requirement === requirement)(values))
}

const displayRequirement = code => {
  return R.prop(
    'display',
    R.find(R.propEq('code', code))(advancedRequirementOptions)
  )
}

const defaultSchema = Yup.object().shape({
  expirationTime: Yup.string()
    .label('Expiration time')
    .required(),
  automation: Yup.string()
    .label('Automation')
    .matches(/(Manual|Automatic)/)
    .required()
})

const getOverridesSchema = values => {
  return Yup.object().shape({
    id: Yup.string()
      .label('Requirement')
      .required()
      .test({
        test() {
          const { requirement } = this.parent
          if (!alreadyExists(values, requirement)) {
            return this.createError({
              message: `Requirement ${displayRequirement(
                requirement
              )} already overriden`
            })
          }
          return true
        }
      }),
    expirationTime: Yup.string()
      .label('Expiration time')
      .required(),
    automation: Yup.string()
      .label('Automation')
      .matches(/(Manual|Automatic)/)
      .required()
  })
}

const getDefaultSettings = () => {
  return [
    {
      name: 'expirationTime',
      header: 'Expiration Time',
      width: 196,
      size: 'sm',
      editable: false
    },
    {
      name: 'automation',
      header: 'Automation',
      width: 196,
      size: 'sm',
      input: Autocomplete,
      inputProps: {
        options: [
          { code: 'Automatic', display: 'Automatic' },
          { code: 'Manual', display: 'Manual' }
        ],
        labelProp: 'display',
        valueProp: 'code'
      }
    }
  ]
}

const getOverrides = () => {
  return [
    {
      name: 'requirement',
      header: 'Requirement',
      width: 196,
      size: 'sm',
      view: getView(advancedRequirementOptions, 'display'),
      input: Autocomplete,
      inputProps: {
        options: advancedRequirementOptions,
        labelProp: 'display',
        valueProp: 'code'
      }
    },
    {
      name: 'expirationTime',
      header: 'Expiration Time',
      width: 196,
      size: 'sm',
      editable: false
    },
    {
      name: 'automation',
      header: 'Automation',
      width: 196,
      size: 'sm',
      input: Autocomplete,
      inputProps: {
        options: [
          { code: 'Automatic', display: 'Automatic' },
          { code: 'Manual', display: 'Manual' }
        ],
        labelProp: 'display',
        valueProp: 'code'
      }
    }
  ]
}

const defaults = [
  {
    expirationTime: 'Forever',
    automation: 'Automatic'
  }
]

const overridesDefaults = {
  requirement: '',
  expirationTime: 'Forever',
  automation: 'Automatic'
}

export {
  defaultSchema,
  getOverridesSchema,
  defaults,
  overridesDefaults,
  getDefaultSettings,
  getOverrides
}

@@ -456,141 +456,8 @@
 %0A%5D%0A%0A
-const alreadyExists = (values, requirement) =%3E %7B%0A  return R.isEmpty(R.filter(value =%3E value.requirement === requirement)(values))%0A%7D%0A%0A
 cons
@@ -1050,29 +1050,37 @@
 if (
-!alreadyExists(values
+R.find(R.propEq('requirement'
 , re
@@ -1090,16 +1090,25 @@
 rement))
+(values))
  %7B%0A     

replace unneeded function with expression