package com.lynx.jsbridge;

import X.C4363Qm0;
import X.C4398QmZ;
import X.C4499QoC;
import X.C4514QoR;
import X.ITb;
import X.LhC;
import X.Q39;
import com.bytedance.covode.number.Covode;
import com.lynx.react.bridge.Callback;
import com.lynx.react.bridge.JavaOnlyMap;
import com.lynx.react.bridge.ReadableArray;
import com.lynx.react.bridge.ReadableMap;
import com.lynx.tasm.LynxGetUIResult;
import com.lynx.tasm.behavior.ui.LynxBaseUI;
import com.lynx.tasm.behavior.utils.LynxUIMethodsExecutor;

public class LynxUIMethodModule extends LynxContextModule {
    static {
        Covode.recordClassIndex(46295);
    }

    public LynxUIMethodModule(C4398QmZ qmZ) {
        super(qmZ);
    }

    public static Callback wrapCallback(final Callback callback) {
        return new Callback() {
            static {
                Covode.recordClassIndex(46299);
            }

            public final void invoke(Object... objArr) {
                if (callback != null) {
                    JavaOnlyMap javaOnlyMap = new JavaOnlyMap();
                    javaOnlyMap.putInt("code", objArr[0].intValue());
                    if (objArr.length > 1) {
                        javaOnlyMap.put("data", objArr[1]);
                    }
                    callback.invoke(new Object[]{javaOnlyMap});
                }
            }
        };
    }

    @ITb
    public void invokeUIMethod(String str, ReadableArray readableArray, String str2, ReadableMap readableMap, Callback callback) {
        final ReadableArray readableArray2 = readableArray;
        final String str3 = str;
        final String str4 = str2;
        final Callback callback2 = callback;
        final ReadableMap readableMap2 = readableMap;
        LhC.LIZ(new C4499QoC(this.mLynxContext) {
            static {
                Covode.recordClassIndex(46298);
            }

            public final void LIZ() {
                int i;
                boolean z;
                if (!str3.isEmpty()) {
                    i = Integer.parseInt(str3);
                } else {
                    i = -1;
                }
                C4398QmZ qmZ = LynxUIMethodModule.this.mLynxContext;
                ReadableArray readableArray = readableArray2;
                String str = str4;
                ReadableMap readableMap = readableMap2;
                Callback wrapCallback = LynxUIMethodModule.wrapCallback(callback2);
                C4363Qm0 qm0 = (C4363Qm0) qmZ.LJIIIZ.get();
                if (qm0 != null) {
                    LynxBaseUI LIZIZ2 = qm0.LIZIZ(i);
                    String str2 = "component not found";
                    if (LIZIZ2 != null) {
                        int i2 = 0;
                        while (i2 < readableArray.size()) {
                            String string = readableArray.getString(i2);
                            if (readableMap == null || readableMap.size() <= 0 || !readableMap.hasKey("_isCallByRefId") || !readableMap.getBoolean("_isCallByRefId")) {
                                z = false;
                            } else {
                                z = true;
                            }
                            if (string.startsWith("#") || z) {
                                String substring = string.substring(1);
                                if (z) {
                                    LIZIZ2 = qm0.LIZIZ(string, LIZIZ2);
                                } else {
                                    LIZIZ2 = qm0.LIZ(substring, LIZIZ2);
                                }
                                if (LIZIZ2 == null) {
                                    str2 = "not found ".concat(String.valueOf(string));
                                } else {
                                    if (LIZIZ2.getIdSelector() != null) {
                                        LIZIZ2.getIdSelector().equals(substring);
                                    }
                                    i2++;
                                }
                            } else if (wrapCallback != null) {
                                wrapCallback.invoke(new Object[]{5, string + " not support，only support id selector currently"});
                                return;
                            } else {
                                return;
                            }
                        }
                        LynxUIMethodsExecutor.LIZ(LIZIZ2, str, readableMap, wrapCallback);
                        return;
                    }
                    if (wrapCallback != null) {
                        wrapCallback.invoke(new Object[]{2, str2});
                    }
                }
            }
        });
    }

    @ITb
    public void invokeUIMethodForSelectorQuery(String str, String str2, String str3, ReadableMap readableMap, Callback callback) {
        final Callback wrapCallback = wrapCallback(callback);
        final String str4 = str2;
        final String str5 = str;
        final ReadableMap readableMap2 = readableMap;
        final String str6 = str3;
        this.mLynxContext.LIZ((Runnable) new C4499QoC(this.mLynxContext) {
            static {
                Covode.recordClassIndex(46296);
            }

            public final void LIZ() {
                boolean z;
                final LynxGetUIResult lynxUIFromTasm;
                ReadableMap readableMap = readableMap2;
                if (readableMap == null || readableMap.size() <= 0 || !readableMap2.hasKey("_isCallByRefId") || !readableMap2.getBoolean("_isCallByRefId")) {
                    z = false;
                } else {
                    z = true;
                }
                C4398QmZ qmZ = LynxUIMethodModule.this.mLynxContext;
                String str = str5;
                String str2 = str4;
                C4514QoR qoR = (C4514QoR) qmZ.LJIIL.get();
                if (qoR == null) {
                    lynxUIFromTasm = null;
                } else {
                    lynxUIFromTasm = qoR.getLynxUIFromTasm(str, str2, z, true);
                }
                if (lynxUIFromTasm == null) {
                    wrapCallback.invoke(new Object[]{1, Q39.LIZJ});
                } else if (lynxUIFromTasm.LIZ == 0) {
                    LhC.LIZIZ(new C4499QoC(LynxUIMethodModule.this.mLynxContext) {
                        static {
                            Covode.recordClassIndex(46297);
                        }

                        public final void LIZ() {
                            C4398QmZ qmZ = LynxUIMethodModule.this.mLynxContext;
                            int i = lynxUIFromTasm.LIZIZ.getInt(0);
                            String str = str6;
                            ReadableMap readableMap = readableMap2;
                            Callback callback = wrapCallback;
                            C4363Qm0 qm0 = (C4363Qm0) qmZ.LJIIIZ.get();
                            if (qm0 != null) {
                                LynxBaseUI LIZ2 = qm0.LIZ(i);
                                if (LIZ2 != null) {
                                    LynxUIMethodsExecutor.LIZ(LIZ2, str, readableMap, callback);
                                } else if (callback != null) {
                                    callback.invoke(new Object[]{6, "node does not have a LynxUI"});
                                }
                            }
                        }
                    });
                } else {
                    wrapCallback.invoke(new Object[]{Integer.valueOf(lynxUIFromTasm.LIZ), lynxUIFromTasm.LIZJ});
                }
            }
        });
    }
}
