from aiservice.models.functions_to_optimize import FunctionToOptimize
from libcst import Module

from testgen.postprocessing.range_modifier import modify_large_loops
from testgen.postprocessing.removeassert_transformer import remove_asserts_from_test
from testgen.postprocessing.topdef_terminator import delete_top_def_nodes


def postprocessing_testgen_pipeline(
    module: Module, helper_function_names: list[str], function_to_optimize: FunctionToOptimize, module_path: str
) -> Module:
    """Applies postprocessing functions in a pipeline, a list of (function, kwargs)"""
    pipeline = [
        (delete_top_def_nodes, {"deletable_list": helper_function_names}),
        (
            remove_asserts_from_test,
            {
                "function_to_optimize": function_to_optimize,
                "helper_function_names": helper_function_names,
                "module_path": module_path,
            },
        ),
        (modify_large_loops, {}),
    ]

    for func, kwargs in pipeline:
        module = func(module, **kwargs)
    return module
