import copy


def _make_reference_filters(filters, ref_dimension, offset_func):
    """
    Copies and replaces the reference dimension's definition in all of the filters applied to a dataset query.

    This is used to shift the dimension filters to fit the reference window.

    :param filters:
    :param ref_dimension:
    :param offset_func:
    :return:
    """
    reference_filters = []
    for ref_filter in filters:
        if ref_filter.field is ref_dimension:
            # NOTE: Important to apply the offset function to the start and stop properties because the date math can
            # become expensive over many rows
            ref_filter = copy.copy(ref_filter)
            ref_filter.start = offset_func(ref_filter.start)
            ref_filter.stop = offset_func(ref_filter.stop)

        reference_filters.append(ref_filter)

    return reference_filters