package ed;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import io.flutter.plugin.common.FlutterException;
import java.nio.ByteBuffer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/* JADX INFO: loaded from: classes.dex */
public final class n implements v {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public static final n f25841a = new n();

    @Override // ed.v
    public final ByteBuffer a(q qVar) {
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("method", qVar.f25842a);
            jSONObject.put("args", o.wrap(qVar.f25843b));
            return m.f25840a.encodeMessage(jSONObject);
        } catch (JSONException e10) {
            throw new IllegalArgumentException("Invalid JSON", e10);
        }
    }

    @Override // ed.v
    public final q b(ByteBuffer byteBuffer) {
        try {
            Object objDecodeMessage = m.f25840a.decodeMessage(byteBuffer);
            if (objDecodeMessage instanceof JSONObject) {
                JSONObject jSONObject = (JSONObject) objDecodeMessage;
                Object obj = jSONObject.get("method");
                Object objOpt = jSONObject.opt("args");
                if (objOpt == JSONObject.NULL) {
                    objOpt = null;
                }
                if (obj instanceof String) {
                    return new q((String) obj, objOpt);
                }
            }
            throw new IllegalArgumentException("Invalid method call: " + objDecodeMessage);
        } catch (JSONException e10) {
            throw new IllegalArgumentException("Invalid JSON", e10);
        }
    }

    @Override // ed.v
    public final Object c(ByteBuffer byteBuffer) {
        try {
            Object objDecodeMessage = m.f25840a.decodeMessage(byteBuffer);
            if (objDecodeMessage instanceof JSONArray) {
                JSONArray jSONArray = (JSONArray) objDecodeMessage;
                Object obj = null;
                if (jSONArray.length() == 1) {
                    Object objOpt = jSONArray.opt(0);
                    if (objOpt == JSONObject.NULL) {
                        return null;
                    }
                    return objOpt;
                }
                if (jSONArray.length() == 3) {
                    Object obj2 = jSONArray.get(0);
                    Object objOpt2 = jSONArray.opt(1);
                    Object obj3 = JSONObject.NULL;
                    if (objOpt2 == obj3) {
                        objOpt2 = null;
                    }
                    Object objOpt3 = jSONArray.opt(2);
                    if (objOpt3 != obj3) {
                        obj = objOpt3;
                    }
                    if ((obj2 instanceof String) && (objOpt2 == null || (objOpt2 instanceof String))) {
                        throw new FlutterException((String) obj2, (String) objOpt2, obj);
                    }
                }
            }
            throw new IllegalArgumentException("Invalid envelope: " + objDecodeMessage);
        } catch (JSONException e10) {
            throw new IllegalArgumentException("Invalid JSON", e10);
        }
    }

    @Override // ed.v
    @NonNull
    public ByteBuffer encodeErrorEnvelope(@NonNull String str, @Nullable String str2, @Nullable Object obj) {
        return m.f25840a.encodeMessage(new JSONArray().put(str).put(o.wrap(str2)).put(o.wrap(obj)));
    }

    @Override // ed.v
    @NonNull
    public ByteBuffer encodeErrorEnvelopeWithStacktrace(@NonNull String str, @Nullable String str2, @Nullable Object obj, @Nullable String str3) {
        return m.f25840a.encodeMessage(new JSONArray().put(str).put(o.wrap(str2)).put(o.wrap(obj)).put(o.wrap(str3)));
    }

    @Override // ed.v
    @NonNull
    public ByteBuffer encodeSuccessEnvelope(@Nullable Object obj) {
        return m.f25840a.encodeMessage(new JSONArray().put(o.wrap(obj)));
    }
}
