"""Module for managing addons."""

import addon_utils


def disable_old_versions():
    """Ensure that old versions of the addon are disabled.

    Needed to avoid namespace clashes when users dont follow instructions.

    """
    failsafe = 0
    addon_names = ["rizomuv_bridge_link", "rizomuv_bridge_core", "rizomuv_bridge"]
    for name in addon_names:
        is_enabled, is_loaded = addon_utils.check(name)
        try:
            if is_enabled:
                addon_utils.disable(module_name=name, default_set=True, handle_error=True)
        except Exception:
            if failsafe > 2:
                return
            failsafe += 1
            disable_old_versions()
