import Form from '../components/Form/Form'
import createServices from './services'
import customWidgets from '../widgets/widgets'
import customFields from '../fields/fields'
import withServices from '../components/withServices'


const createForm = (options = {}) => {
  const { services: passedServices } = options

  const services = createServices(passedServices)

  const widgetsWithServices = Object.keys(customWidgets).reduce((prev, cur) => ({
    ...prev,
    [cur]: withServices(customWidgets[cur], services)
  }), {})

  const fieldsWithServices = Object.keys(customFields).reduce((prev, cur) => ({
    ...prev,
    [cur]: withServices(customFields[cur], services)
  }), {})


  return ({ widgets, fields, ...props }) => (
    <Form
      {...props}
      widgets={{
        ...widgetsWithServices,
        ...widgets
      }}
      fields={{
        ...fieldsWithServices,
        ...fields
      }}
    />
  )
}

export default createForm
