/*
 * Copyright (c) Facebook, Inc. and its 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.flipper.plugins.inspector.descriptors.utils;

import static com.facebook.flipper.plugins.inspector.InspectorValue.Type.Enum;

import androidx.collection.SimpleArrayMap;
import com.facebook.flipper.plugins.inspector.InspectorValue;

public class EnumMapping {
  private final SimpleArrayMap<String, Integer> mMapping = new SimpleArrayMap<>();
  private final String mDefaultKey;

  public EnumMapping(String defaultKey) {
    mDefaultKey = defaultKey;
  }

  public void put(String s, int i) {
    mMapping.put(s, i);
  }

  public InspectorValue get(final int i) {
    return get(i, true);
  }

  public InspectorValue get(final int i, final boolean mutable) {
    for (int ii = 0, count = mMapping.size(); ii < count; ii++) {
      if (mMapping.valueAt(ii) == i) {
        return mutable
            ? InspectorValue.mutable(Enum, mMapping.keyAt(ii))
            : InspectorValue.immutable(Enum, mMapping.keyAt(ii));
      }
    }
    return mutable
        ? InspectorValue.mutable(Enum, mDefaultKey)
        : InspectorValue.immutable(Enum, mDefaultKey);
  }

  public int get(String s) {
    if (mMapping.containsKey(s)) {
      return mMapping.get(s);
    }
    return mMapping.get(mDefaultKey);
  }
}

@@ -328,16 +328,134 @@
 e.Enum;%0A
+import static com.facebook.flipper.plugins.inspector.InspectorValue.Type.Picker;%0A%0Aimport androidx.collection.ArrayMap;
 %0Aimport 
@@ -596,22 +596,16 @@
 e final 
-Simple
 ArrayMap
@@ -637,22 +637,16 @@
 g = new 
-Simple
 ArrayMap
@@ -709,16 +709,22 @@
 Mapping(
+final 
 String d
@@ -789,16 +789,22 @@
 oid put(
+final 
 String s
@@ -804,16 +804,22 @@
 tring s,
+ final
  int i) 
@@ -935,60 +935,123 @@
 lic 
-InspectorValue get(final int i, final boolean mutabl
+static String findKeyForValue(%0A      SimpleArrayMap%3CString, Integer%3E mapping, String mDefaultValue, int currentValu
 e) %7B
@@ -1065,17 +1065,16 @@
 r (int i
-i
  = 0, co
@@ -1080,17 +1080,16 @@
 ount = m
-M
 apping.s
@@ -1096,17 +1096,16 @@
 ize(); i
-i
  %3C count
@@ -1106,17 +1106,16 @@
  count; 
-i
 i++) %7B%0A 
@@ -1120,25 +1120,24 @@
 %0A      if (m
-M
 apping.value
@@ -1144,15 +1144,30 @@
 At(i
-i) == i
+).equals(currentValue)
 ) %7B%0A
@@ -1186,157 +1186,576 @@
 rn m
-utable
+apping.keyAt(i);
 %0A      
+%7D%0A
     
-  ? InspectorValue.mutable(Enum, mMapping.keyAt(ii))%0A            : InspectorValue.immutable(Enum, mMapping.keyAt(ii));%0A    
+%7D%0A    return mDefaultValue;%0A  %7D%0A%0A  public InspectorValue get(final int i, final boolean mutable) %7B%0A    String value = findKeyForValue(mMapping, mDefaultKey, i);%0A    return mutable ? InspectorValue.mutable(Enum, value) : InspectorValue.immutable(Enum, value);%0A  %7D%0A%0A  public int get(final String s) %7B%0A    if (mMapping.containsKey(s)) %7B%0A      return mMapping.get(s);%0A    %7D%0A    return mMapping.get(mDefaultKey);%0A
   %7D%0A
+%0A
   
-  %7D
+public InspectorValue toPicker() %7B%0A    return toPicker(true);%0A  %7D%0A%0A  public InspectorValue toPicker(final boolean mutable) %7B
 %0A   
@@ -1795,36 +1795,83 @@
 orValue.mutable(
-Enum
+Picker, new InspectorValue.Picker(mMapping.keySet()
 , mDefaultKey)%0A 
@@ -1868,16 +1868,17 @@
 aultKey)
+)
 %0A       
@@ -1943,67 +1943,190 @@
 lic 
-int get(String s) %7B%0A    if (mMapping.containsKey(s)
+InspectorValue toPicker(final int currentValue) %7B%0A    return toPicker(currentValue, true);%0A  %7D%0A%0A  public InspectorValue toPicker(final int currentValue, final boolean mutable
 ) %7B%0A    
   re
@@ -2125,75 +2125,236 @@
     
-  
-return mMapping.get(s);%0A    %7D%0A    return mMapping.get(mDefaultKey
+String value = findKeyForValue(mMapping, mDefaultKey, currentValue);%0A    return mutable%0A        ? InspectorValue.mutable(Picker, new InspectorValue.Picker(mMapping.keySet(), value))%0A        : InspectorValue.immutable(Enum, value
 );%0A 

Add picker support for EnumMapping