import copy


def amp_to_mag_extended(mag_kwargs_list, light_model, data_api):
    """Convert the magnitude entries into amp (counts per second)
    used by lenstronomy to render the image, for extended objects

    Parameters
    ----------
    mag_kwargs_list : list
        list of kwargs dictionaries in which 'amp' keys are replaced by 'magnitude'
    light_model : lenstronomy.LightModel object
        light model describing the surface brightness profile, used for calculating
        the total flux. Note that only some profiles with an analytic integral can be
        used.
    data_api : lenstronomy.DataAPI object
        a wrapper object around lenstronomy.Observation that has the magnitude zeropoint
        information, with which the magnitude-to-amp conversion is done.

    Returns
    -------
    list
        list of kwargs dictionaries with 'magnitude' replaced by 'amp'

    """
    amp_kwargs_list = copy.deepcopy(mag_kwargs_list)
    for i, mag_kwargs in enumerate(mag_kwargs_list):
        amp_kwargs = amp_kwargs_list[i]
        mag = amp_kwargs.pop('magnitude')
        cps_norm = light_model.total_flux(amp_kwargs_list, norm=True, k=i)[0]
        cps = data_api.magnitude2cps(mag)
        amp = cps/ cps_norm
        amp_kwargs['amp'] = amp 
    return amp_kwargs_list