using UnityEngine;

namespace MuzikaGromche;

// A global MonoBehaviour instance to run coroutines from non-MonoBehaviour or static context.
internal static class GlobalBehaviour
{
    sealed class AdhocBehaviour : MonoBehaviour;

    static AdhocBehaviour? instance = null;

    public static MonoBehaviour Instance
    {
        get
        {
            if (instance == null)
            {
                var go = new GameObject("MuzikaGromche_GlobalBehaviour", [
                    typeof(AdhocBehaviour),
                ])
                {
                    hideFlags = HideFlags.HideAndDontSave
                };
                Object.DontDestroyOnLoad(go);
                instance = go.GetComponent<AdhocBehaviour>();
            }
            return instance;
        }
    }
}
