using System;
using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif

/// <summary>
/// Small helper to safely enable Input System artifacts without letting InputSystem exceptions crash initialization.
/// Use InputSafety.SafeEnable(action) instead of action.Enable() when running on devices that may have many bindings.
/// </summary>
public static class InputSafety
{
#if ENABLE_INPUT_SYSTEM
    public static void SafeEnable(this InputAction action)
    {
        if (action == null) return;
        try
        {
            if (!action.enabled) action.Enable();
        }
        catch (Exception ex)
        {
            Debug.LogError($"[InputSafety] Failed to enable InputAction '{action.name}': {ex}");
        }
    }

    public static void SafeEnable(this InputActionReference actionRef)
    {
        if (actionRef == null || actionRef.action == null) return;
        SafeEnable(actionRef.action);
    }

    public static void SafeEnable(this InputActionMap map)
    {
        if (map == null) return;
        try
        {
            map.Enable();
        }
        catch (Exception ex)
        {
            Debug.LogError($"[InputSafety] Failed to enable InputActionMap '{map.name}': {ex}");
        }
    }

    public static void SafeEnable(this InputActionAsset asset)
    {
        if (asset == null) return;
        try
        {
            asset.Enable();
        }
        catch (Exception ex)
        {
            Debug.LogError($"[InputSafety] Failed to enable InputActionAsset '{asset.name}': {ex}");
        }
    }
#else
    // If Input System isn't enabled, provide no-op overloads so call sites don't need conditional compilation.
    public static void SafeEnable(this object _) { }
#endif
}
