"""Process-local RF-DETR 1.11 config compatibility; v3 running code untouched."""
from contextlib import contextmanager


def scheduler_compatibility(kwargs):
    result = dict(kwargs)
    if 'lr_drop' in result:
        nested = dict(result.get('lr_scheduler_kwargs', {}))
        if 'lr_drop' in nested:
            raise ValueError('Ambiguous duplicate lr_drop')
        nested['lr_drop'] = result.pop('lr_drop')
        result['lr_scheduler_kwargs'] = nested
    return result


@contextmanager
def config_compatibility():
    from rfdetr import RFDETRSegSmall
    original = RFDETRSegSmall.get_train_config
    def compatible(self, **kwargs):
        return original(self, **scheduler_compatibility(kwargs))
    RFDETRSegSmall.get_train_config = compatible
    try:
        yield
    finally:
        RFDETRSegSmall.get_train_config = original
