package com.bytedance.webx.pia.page.bridge;

import X.2Pr;
import X.2Qo;
import X.C0847KJq;
import X.C1836Kj5;
import X.C1847KjG;
import X.C1855KjO;
import X.C1865KjY;
import X.C1866KjZ;
import X.C1881Kjo;
import X.C1886Kjt;
import X.C4950LsD;
import X.M12;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import com.bytedance.hybrid.pia.bridge.protocol.Callback;
import com.bytedance.vmsdk.worker.JsWorker;
import org.json.JSONObject;

public final class PiaPostWorkerMessageMethod implements C1865KjY<2Qo> {
    public final String name = "pia.postWorkerMessage";
    public final Class<2Qo> paramsType = 2Qo.class;
    public final C1836Kj5 prefetchRuntime;
    public final C0847KJq privilege = C0847KJq.Protected;
    public final int version;

    static {
        Covode.recordClassIndex(40500);
    }

    public final String getName() {
        return this.name;
    }

    public final Class<2Qo> getParamsType() {
        return this.paramsType;
    }

    public final C0847KJq getPrivilege() {
        return this.privilege;
    }

    public final int getVersion() {
        return this.version;
    }

    public final 2Qo decodeParams(String str) {
        return (2Qo) C1866KjZ.LIZ(this, str);
    }

    public PiaPostWorkerMessageMethod(C1836Kj5 kj5) {
        this.prefetchRuntime = kj5;
    }

    public final void invoke(2Qo r7, C4950LsD<? super Callback.Status, ? super String, 2Pr> lsD) {
        C1847KjG kjG;
        MethodCollector.i(8041);
        M12.LIZ((Object) r7, (Object) lsD);
        if (r7.LIZ == null) {
            lsD.invoke(Callback.Status.InvalidParams, "Parameter 'data' requested!");
            MethodCollector.o(8041);
            return;
        }
        C1836Kj5 kj5 = this.prefetchRuntime;
        if (kj5 != null) {
            kjG = kj5.LIZ;
        } else {
            kjG = null;
        }
        if (kjG != null) {
            int i = C1855KjO.LIZ[kjG.ordinal()];
            if (i == 1) {
                C1836Kj5 kj52 = this.prefetchRuntime;
                String str = r7.LIZ;
                M12.LIZ((Object) str);
                C1881Kjo kjo = kj52.LIZIZ;
                M12.LIZ((Object) str);
                JsWorker jsWorker = kjo.LIZ;
                if (jsWorker == null || !jsWorker.LIZIZ) {
                    C1886Kjt.LIZ(C1886Kjt.LIZ, "Attempt to post a message to dead worker, message = ".concat(String.valueOf(str)), (String) null, 6);
                } else {
                    JsWorker jsWorker2 = kjo.LIZ;
                    if (jsWorker2.LIZIZ) {
                        JsWorker.nativePostMessage(jsWorker2.LIZ, str);
                    }
                    C1886Kjt.LIZ.LIZ("Post message to worker, message = ".concat(String.valueOf(str)), "PiaCore");
                }
                lsD.invoke(Callback.Status.Success, null);
                MethodCollector.o(8041);
                return;
            } else if (i == 2) {
                lsD.invoke(Callback.Status.Failed, new JSONObject().put("state", "fetching").toString());
                MethodCollector.o(8041);
                return;
            } else if (i == 3) {
                lsD.invoke(Callback.Status.Failed, new JSONObject().put("state", "terminate").toString());
                MethodCollector.o(8041);
                return;
            }
        }
        lsD.invoke(Callback.Status.Failed, new JSONObject().put("state", "unusable").toString());
        MethodCollector.o(8041);
    }
}
