using Newtonsoft.Json.Linq;
using UnityEngine;

namespace LLM.Editor.Helper
{
    /// <summary>
    /// Provides extension methods for converting JToken objects to Unity-specific types.
    /// </summary>
    public static class JsonExtensions
    {
        public static Vector3 ToVector3(this JToken token)
        {
            if (token is not JObject obj) return Vector3.zero;
            var x = obj["x"]?.Value<float>() ?? 0f;
            var y = obj["y"]?.Value<float>() ?? 0f;
            var z = obj["z"]?.Value<float>() ?? 0f;
            return new Vector3(x, y, z);
        }

        public static Quaternion ToQuaternion(this JToken token)
        {
            if (token is not JObject obj) return Quaternion.identity;
            var x = obj["x"]?.Value<float>() ?? 0f;
            var y = obj["y"]?.Value<float>() ?? 0f;
            var z = obj["z"]?.Value<float>() ?? 0f;
            var w = obj["w"]?.Value<float>() ?? 1f;
            return new Quaternion(x, y, z, w);
        }
    }
}
