def transform_to_factory_kwargs(data, original_kwargs=None, prefix=""):
    """
    Transforms factory data into the correct kwargs to pass to the factory

    Args:
        data (dict): dictionary of data from the factory_data file
        original_kwargs (dict): kwargs passed into the factory function from code
        prefix (str): argument prefix string

    Returns:
        dict: the generate kwargs to call the factory with
    """
    if original_kwargs is None:
        original_kwargs = {}

    kwargs = {
        key: value for key, value in original_kwargs.items() if key not in data.keys()
    }

    for key, value in data.items():
        prefixed_key = "{}__{}".format(prefix, key) if prefix else key

        if original_kwargs is not None and prefixed_key in original_kwargs:
            kwargs[prefixed_key] = original_kwargs[prefixed_key]
        elif isinstance(value, dict):
            kwargs.update(transform_to_factory_kwargs(value, prefix=prefixed_key))
        elif isinstance(value, list):
            kwargs[prefixed_key] = [
                transform_to_factory_kwargs(item, prefix=prefixed_key) for item in value
            ]
        else:
            kwargs[prefixed_key] = value

    return kwargs