class FormEditorHelper {

    public FieldDefinition switchToField(FieldDefinition originalField,
                                         String newBinding) {

        if (newBinding != null && !"".equals(newBinding)) {
            Optional<FieldDefinition> availableFieldOptional = availableFields.values()
                    .stream()
                    .filter(availableField -> availableField.getBinding().equals(newBinding)).findFirst();

            if (availableFieldOptional.isPresent()) {
                FieldDefinition availableField = availableFieldOptional.get();

                FieldDefinition resultField = fieldManager.getFieldFromProvider(originalField.getFieldType().getTypeName(),
                                                                                availableField.getFieldTypeInfo());

                if (resultField == null) {
                    // this happens when trying to bind to a property which is of an unsupported type for the current FieldFefintion
                    resultField = fieldManager.getFieldFromProvider(availableField.getFieldType().getTypeName(),
                                                                    availableField.getFieldTypeInfo());
                }

                resultField.copyFrom(originalField);

                resultField.setId(availableField.getId());
                resultField.setName(availableField.getName());
                resultField.setStandaloneClassName(availableField.getStandaloneClassName());
                resultField.setBinding(newBinding);

                return resultField;
            }
        }

        // If we arrive here is because we have a dynamic binding or we are unbinding a field
        FieldDefinition resultField = fieldManager.getFieldFromProvider(originalField.getFieldType().getTypeName(), originalField.getFieldTypeInfo());

        if (newBinding == null || newBinding.equals("")) {
            // unbinding a field
            resultField.setName(generateUnboundFieldName(resultField));
            resultField.setBinding("");
        }

        resultField.copyFrom(originalField);
        resultField.setBinding(newBinding);

        if (resultField.getName() == null) {
            // edge case we only get here with dynamic bindings
            resultField.setName(newBinding);
        }

        return resultField;
    }

    @Inject public  FormEditorHelper(FieldManager fieldManager,
                            ManagedInstance<EditorFieldLayoutComponent> editorFieldLayoutComponents,
                            SyncBeanManager beanManager);

}