"""Locate the live Mixlar desktop-app module when a plugin runs inside it.

The app defines ``MixlarPlugin``, ``PluginRegistry`` and
``_plugin_package_digest``. Depending on how it's launched it appears in
``sys.modules`` under different names — ``mixlar_mini`` when imported as a
module (tests, the signing tool), but ``__main__`` when run or PyInstaller-frozen
as the entry point. So we check both, then fall back to scanning for the module
that carries the app's fingerprint. Returns the module, or ``None`` off-app.

This is why ``from mixlar import MixlarPlugin`` becomes the app's own class in
every launch mode: run-from-source, ``python -m``, and the bundled ``.exe``.
"""
from __future__ import annotations

import sys

_FINGERPRINT = ("MixlarPlugin", "PluginRegistry", "_plugin_package_digest")


def find_app():
    """Return the live app module, or ``None`` when not running inside it."""
    for name in ("mixlar_mini", "__main__"):
        m = sys.modules.get(name)
        if m is not None and all(hasattr(m, a) for a in _FINGERPRINT):
            return m
    for m in list(sys.modules.values()):
        if m is not None and all(hasattr(m, a) for a in _FINGERPRINT):
            return m
    return None


__all__ = ["find_app"]
