/*
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

package com.facebook.react.animated;

import androidx.annotation.Nullable;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.events.RCTEventEmitter;
import java.util.List;

/** Handles updating a {@link ValueAnimatedNode} when an event gets dispatched. */
/* package */ class EventAnimationDriver implements RCTEventEmitter {
  private List<String> mEventPath;
  /* package */ ValueAnimatedNode mValueNode;

  public EventAnimationDriver(List<String> eventPath, ValueAnimatedNode valueNode) {
    mEventPath = eventPath;
    mValueNode = valueNode;
  }

  @Override
  public void receiveEvent(int targetTag, String eventName, @Nullable WritableMap event) {
    if (event == null) {
      throw new IllegalArgumentException("Native animated events must have event data.");
    }

    // Get the new value for the node by looking into the event map using the provided event path.
    ReadableMap curMap = event;
    for (int i = 0; i < mEventPath.size() - 1; i++) {
      curMap = curMap.getMap(mEventPath.get(i));
    }

    mValueNode.mValue = curMap.getDouble(mEventPath.get(mEventPath.size() - 1));
  }

  @Override
  public void receiveTouches(
      String eventName, WritableArray touches, WritableArray changedIndices) {
    throw new RuntimeException("receiveTouches is not support by native animated events");
  }
}

@@ -296,27 +296,186 @@
 dge.Readable
-Map
+Array;%0Aimport com.facebook.react.bridge.ReadableMap;%0Aimport com.facebook.react.bridge.ReadableType;%0Aimport com.facebook.react.bridge.UnexpectedNativeTypeException
 ;%0Aimport com
@@ -1362,24 +1362,25 @@
 dableMap cur
+r
 Map = event;
@@ -1380,16 +1380,52 @@
  event;%0A
+    ReadableArray currArray = null;%0A
     for 
@@ -1480,30 +1480,560 @@
     
+if (
 cur
+r
 Map 
-= curMap.getMap
+!= null) %7B%0A        String key = mEventPath.get(i);%0A        ReadableType keyType = currMap.getType(key);%0A        if (keyType == ReadableType.Map) %7B%0A          currMap = currMap.getMap(key);%0A          currArray = null;%0A        %7D else if (keyType == ReadableType.Array) %7B%0A          currArray = currMap.getArray(key);%0A          currMap = null;%0A        %7D else %7B%0A          throw new UnexpectedNativeTypeException(%0A              %22Unexpected type %22 + keyType + %22 for key '%22 + key + %22'%22);%0A        %7D%0A      %7D else %7B%0A        int index = Integer.parseInt
 (mEv
@@ -2057,91 +2057,739 @@
     
-%7D%0A%0A
     
-mValueNode.mValue = curMap.getDouble(mEventPath.get(mEventPath.size() - 1));
+ReadableType keyType = currArray.getType(index);%0A        if (keyType == ReadableType.Map) %7B%0A          currMap = currArray.getMap(index);%0A          currArray = null;%0A        %7D else if (keyType == ReadableType.Array) %7B%0A          currArray = currArray.getArray(index);%0A          currMap = null;%0A        %7D else %7B%0A          throw new UnexpectedNativeTypeException(%0A              %22Unexpected type %22 + keyType + %22 for index '%22 + index + %22'%22);%0A        %7D%0A      %7D%0A    %7D%0A%0A    String lastKey = mEventPath.get(mEventPath.size() - 1);%0A    if (currMap != null) %7B%0A      mValueNode.mValue = currMap.getDouble(lastKey);%0A    %7D else %7B%0A      int lastIndex = Integer.parseInt(lastKey);%0A      mValueNode.mValue = currArray.getDouble(lastIndex);%0A    %7D
 %0A  %7D

Support Animated.event extracting values from array