package com.rntensorflow.imagerecognition;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

import java.util.HashMap;
import java.util.Map;

public class RNImageRecognizerModule extends ReactContextBaseJavaModule {

    private Map<String, ImageRecognizer> imageRecognizers = new HashMap<>();
    private ReactApplicationContext reactContext;

    public RNImageRecognizerModule(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "RNImageRecognition";
    }

    @Override
    public void onCatalystInstanceDestroy() {
        for (String id : imageRecognizers.keySet()) {
            this.imageRecognizers.remove(id);
        }
    }

    @ReactMethod
    public void initImageRecognizer(String id, ReadableMap data, Promise promise) {
        try {
            String model = data.getString("model");
            String label = data.getString("label");
            Integer imageMean = data.hasKey("imageMean") ? data.getInt("imageMean") : null;
            Double imageStd = data.hasKey("imageStd") ? data.getDouble("imageStd") : null;

            ImageRecognizer imageRecognizer = ImageRecognizer.init(reactContext, model, label, imageMean, imageStd);
            imageRecognizers.put(id, imageRecognizer);
            promise.resolve(true);
        } catch (Exception e) {
            promise.reject(e);
        }
    }

    @ReactMethod
    public void recognize(String id, ReadableMap data, Promise promise) {
        try {
            String image = data.getString("image");
            String inputName = data.getString("inputName");
            Integer inputSize = data.hasKey("inputSize") ? data.getInt("inputSize") : null;
            String outputName = data.getString("outputName");
            Integer maxResults = data.hasKey("maxResults") ? data.getInt("maxResults") : null;
            Double threshold = data.hasKey("threshold") ? data.getDouble("threshold") : null;

            ImageRecognizer imageRecognizer = imageRecognizers.get(id);
            imageRecognizer.recognizeImage(image, inputName, inputSize, outputName, maxResults, threshold);
            promise.resolve(true);
        } catch (Exception e) {
            promise.reject(e);
        }
    }

    @ReactMethod
    public void close(String id, Promise promise) {
        try {
            this.imageRecognizers.remove(id);
            promise.resolve(true);
        } catch (Exception e) {
            promise.reject(e);
        }
    }
}

@@ -74,226 +74,9 @@
 dge.
-Promise;%0Aimport com.facebook.react.bridge.ReactApplicationContext;%0Aimport com.facebook.react.bridge.ReactContextBaseJavaModule;%0Aimport com.facebook.react.bridge.ReactMethod;%0Aimport com.facebook.react.bridge.ReadableMap
+*
 ;%0A%0Ai
@@ -935,16 +935,17 @@
 ng label
+s
  = data.
@@ -960,16 +960,17 @@
 g(%22label
+s
 %22);%0A    
@@ -1242,16 +1242,17 @@
 l, label
+s
 , imageM
@@ -1616,32 +1616,59 @@
 ring inputName =
+ data.hasKey(%22inputName%22) ?
  data.getString(
@@ -1671,32 +1671,39 @@
 ing(%22inputName%22)
+ : null
 ;%0A            In
@@ -1807,24 +1807,52 @@
 outputName =
+ data.hasKey(%22outputName%22) ?
  data.getStr
@@ -1868,16 +1868,23 @@
 utName%22)
+ : null
 ;%0A      
@@ -2142,32 +2142,55 @@
 id);%0A           
+ WritableArray result =
  imageRecognizer
@@ -2290,36 +2290,38 @@
 promise.resolve(
-true
+result
 );%0A        %7D cat

Return result properly