Metasurfaces are the bidimensional analog of metamaterials. There are made of resonant elements deposited on a thin film. They have been shown to allow for the control of polarization of light, in particular through topological effects and to make possible the transmission of a light beam under generalized refraction laws. In the present work, metasurfaces whose period is made of several resonant elements with both electric and magnetic dipoles are considered. A general theory of diffraction is developed and the possibility of optimization towards designing a predefined wavefront are investigated. To do so, we use multiple scattering theory as well as a singular perturbation approach that allows us to obtain a simple setting of the scattering problem in terms of a generalized impedance operator. This formulation is then used within an optimization algorithm in order to investigate the range of parameters over which a fine control of the transmitted beam can be obtained.