package io.scanbot.sdk.reactnative;

import androidx.annotation.NonNull;

import com.facebook.react.bridge.Promise;

import java.util.Map;

import io.scanbot.sdk.sdk_native_wrapper.base.SBNWProxyDelegate;

public class ResultProxyDelegate implements SBNWProxyDelegate {
    private Promise promise;
    private ResultProxyCallback callback;

    public ResultProxyDelegate(@NonNull Promise promise) {
        this.promise = promise;
    }

    public ResultProxyDelegate(@NonNull Promise promise, @NonNull ResultProxyCallback callback) {
        this.promise = promise;
        this.callback = callback;
    }

    @Override
    public void didRejectPromise(@NonNull Map<String, ?> map) {
        promise.reject(new Exception((String) map.get("message")));
    }

    @Override
    public void didResolvePromise(@NonNull Map<String, ?> map) {
        if (this.callback != null) {
            this.callback.onSuccess();
        }

        promise.resolve(JSONUtils.toWritableMap(map));
    }
}
