package com.strmr.ps.data.supabase;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;
import androidx.media3.container.NalUnitUtil;
import androidx.media3.exoplayer.upstream.CmcdData;
import androidx.media3.extractor.text.ttml.TtmlNode;
import androidx.security.crypto.EncryptedSharedPreferences;
import androidx.security.crypto.MasterKey;
import com.strmr.ps.data.supabase.DeviceManager;
import dagger.hilt.android.qualifiers.ApplicationContext;
import defpackage.ae7;
import defpackage.aea;
import defpackage.bk1;
import defpackage.cc9;
import defpackage.ck1;
import defpackage.cr4;
import defpackage.dy4;
import defpackage.e00;
import defpackage.e6a;
import defpackage.ey4;
import defpackage.gj1;
import defpackage.hba;
import defpackage.i49;
import defpackage.iba;
import defpackage.ie7;
import defpackage.ig6;
import defpackage.ik2;
import defpackage.iw7;
import defpackage.j65;
import defpackage.je7;
import defpackage.jn4;
import defpackage.k42;
import defpackage.k92;
import defpackage.ke7;
import defpackage.kn4;
import defpackage.kp6;
import defpackage.kw7;
import defpackage.l38;
import defpackage.m02;
import defpackage.m12;
import defpackage.n89;
import defpackage.ng1;
import defpackage.o29;
import defpackage.pba;
import defpackage.q07;
import defpackage.q9a;
import defpackage.qf8;
import defpackage.qq3;
import defpackage.rf8;
import defpackage.rx4;
import defpackage.s38;
import defpackage.sc2;
import defpackage.v65;
import defpackage.vd9;
import defpackage.ve7;
import defpackage.vw2;
import defpackage.vx;
import defpackage.vx8;
import defpackage.we7;
import defpackage.wj0;
import defpackage.wo5;
import defpackage.wy4;
import defpackage.y41;
import defpackage.yj7;
import defpackage.yy4;
import defpackage.zd9;
import java.io.IOException;
import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.inject.Inject;
import javax.inject.Singleton;
import kotlin.Metadata;
import kotlin.coroutines.jvm.internal.Boxing;
import kotlin.coroutines.jvm.internal.ContinuationImpl;
import kotlin.reflect.KTypeProjection;

/* JADX INFO: loaded from: classes3.dex */
@Singleton
@Metadata(d1 = {"\u0000\u0016\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0018\u0002\n\u0000\n\u0002\u0018\u0002\n\u0002\b\u0005\b\u0007\u0018\u00002\u00020\u0001:\u0001\bB\u001b\b\u0007\u0012\u0006\u0010\u0003\u001a\u00020\u0002\u0012\b\b\u0001\u0010\u0005\u001a\u00020\u0004¢\u0006\u0004\b\u0006\u0010\u0007¨\u0006\t"}, d2 = {"Lcom/strmr/ps/data/supabase/DeviceManager;", "", "Lcc9;", "supabase", "Landroid/content/Context;", "context", "<init>", "(Lcc9;Landroid/content/Context;)V", CmcdData.OBJECT_TYPE_AUDIO_ONLY, "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
@vx8
public final class DeviceManager {
    public final cc9 a;
    public final Context b;
    public int c;
    public boolean d;
    public final ig6 e;
    public final o29 f;
    public final ig6 g;
    public final o29 h;
    public final ng1 i;
    public final j65 j;
    public final j65 k;
    public final String l;

    @Metadata(d1 = {"\u0000\u0018\n\u0002\u0018\u0002\n\u0002\u0010\u0000\n\u0002\u0010\u000e\n\u0002\b\u0002\n\u0002\u0010\b\n\u0002\b\b\b\u0086\u0003\u0018\u00002\u00020\u0001R\u0014\u0010\u0003\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u0003\u0010\u0004R\u0014\u0010\u0006\u001a\u00020\u00058\u0006X\u0086T¢\u0006\u0006\n\u0004\b\u0006\u0010\u0007R\u0014\u0010\b\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\b\u0010\u0004R\u0014\u0010\t\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\t\u0010\u0004R\u0014\u0010\n\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\n\u0010\u0004R\u0014\u0010\u000b\u001a\u00020\u00028\u0002X\u0082T¢\u0006\u0006\n\u0004\b\u000b\u0010\u0004R\u0014\u0010\f\u001a\u00020\u00058\u0002X\u0082T¢\u0006\u0006\n\u0004\b\f\u0010\u0007¨\u0006\r"}, d2 = {"Lcom/strmr/ps/data/supabase/DeviceManager$a;", "", "", "TAG", "Ljava/lang/String;", "", "MAX_DEVICES", "I", "PREF_NAME", "PREF_DEVICE_REGISTERED", "PREF_USER_ID", "TABLE_NAME", "MAX_REFRESH_FAILURES", "app_sideloadRelease"}, k = 1, mv = {2, 0, 0}, xi = NalUnitUtil.H265_NAL_UNIT_TYPE_UNSPECIFIED)
    public static final class a {
    }

    @Inject
    public DeviceManager(@kp6 cc9 cc9Var, @ApplicationContext @kp6 Context context) {
        cr4.g(cc9Var, "supabase");
        cr4.g(context, "context");
        this.a = cc9Var;
        this.b = context;
        ig6 ig6VarA = i49.a(Boolean.FALSE);
        this.e = ig6VarA;
        this.f = ig6VarA;
        ig6 ig6VarA2 = i49.a(ik2.a);
        this.g = ig6VarA2;
        this.h = ig6VarA2;
        gj1.b bVarB = zd9.b();
        m12 m12Var = sc2.a;
        ng1 ng1VarA = bk1.a(gj1.b.a.c((rx4) bVarB, m02.c));
        this.i = ng1VarA;
        final int i = 0;
        this.j = v65.a(new qq3(this) { // from class: g92
            public final /* synthetic */ DeviceManager b;

            {
                this.b = this;
            }

            @Override // defpackage.qq3
            public final Object invoke() throws GeneralSecurityException, IOException {
                switch (i) {
                    case 0:
                        DeviceManager deviceManager = this.b;
                        MasterKey masterKeyBuild = new MasterKey.Builder(deviceManager.b).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
                        cr4.f(masterKeyBuild, "build(...)");
                        SharedPreferences sharedPreferencesCreate = EncryptedSharedPreferences.create(deviceManager.b, "device_manager_prefs", masterKeyBuild, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
                        cr4.f(sharedPreferencesCreate, "create(...)");
                        return sharedPreferencesCreate;
                    default:
                        DeviceManager deviceManager2 = this.b;
                        String string = Settings.Secure.getString(deviceManager2.b.getContentResolver(), "android_id");
                        if (string == null) {
                            string = "unknown";
                        }
                        byte[] bytes = vw2.l(string, "_", deviceManager2.b.getPackageName()).getBytes(bz0.a);
                        cr4.f(bytes, "getBytes(...)");
                        String string2 = UUID.nameUUIDFromBytes(bytes).toString();
                        cr4.f(string2, "toString(...)");
                        return string2;
                }
            }
        });
        final int i2 = 1;
        this.k = v65.a(new qq3(this) { // from class: g92
            public final /* synthetic */ DeviceManager b;

            {
                this.b = this;
            }

            @Override // defpackage.qq3
            public final Object invoke() throws GeneralSecurityException, IOException {
                switch (i2) {
                    case 0:
                        DeviceManager deviceManager = this.b;
                        MasterKey masterKeyBuild = new MasterKey.Builder(deviceManager.b).setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build();
                        cr4.f(masterKeyBuild, "build(...)");
                        SharedPreferences sharedPreferencesCreate = EncryptedSharedPreferences.create(deviceManager.b, "device_manager_prefs", masterKeyBuild, EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
                        cr4.f(sharedPreferencesCreate, "create(...)");
                        return sharedPreferencesCreate;
                    default:
                        DeviceManager deviceManager2 = this.b;
                        String string = Settings.Secure.getString(deviceManager2.b.getContentResolver(), "android_id");
                        if (string == null) {
                            string = "unknown";
                        }
                        byte[] bytes = vw2.l(string, "_", deviceManager2.b.getPackageName()).getBytes(bz0.a);
                        cr4.f(bytes, "getBytes(...)");
                        String string2 = UUID.nameUUIDFromBytes(bytes).toString();
                        cr4.f(string2, "toString(...)");
                        return string2;
                }
            }
        });
        this.l = vw2.l(Build.MANUFACTURER, " ", Build.MODEL);
        ig6VarA.setValue(Boolean.valueOf(g().getBoolean("device_registered", false)));
        wj0.c(ng1VarA, null, null, new r(this, null), 3);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(String str, ContinuationImpl continuationImpl) {
        e eVar;
        if (continuationImpl instanceof e) {
            eVar = (e) continuationImpl;
            int i = eVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                eVar.c = i - Integer.MIN_VALUE;
            } else {
                eVar = new e(this, continuationImpl);
            }
        }
        Object objA = eVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = eVar.c;
        try {
            if (i2 == 0) {
                s38.b(objA);
                ke7 ke7VarA = je7.a(this.a, "user_devices");
                ie7 ie7Var = ke7VarA.a;
                yj7 yj7Var = ie7Var.d.b;
                rf8 rf8Var = new rf8(yj7Var);
                LinkedHashMap linkedHashMap = rf8Var.b;
                ae7 ae7Var = new ae7(yj7Var, linkedHashMap);
                ae7Var.a(f.a, str);
                ae7Var.a(g.a, (String) this.k.getValue());
                ae7Var.a(h.a, Boxing.boxBoolean(true));
                linkedHashMap.put("select", y41.am("*"));
                qf8 qf8Var = new qf8(aea.a(linkedHashMap), ke7VarA.c, rf8Var.c.m());
                l38 l38Var = l38.a;
                String str2 = ke7VarA.b;
                eVar.c = 1;
                objA = l38Var.a(ie7Var, str2, qf8Var, eVar);
                if (objA == ck1Var) {
                    return ck1Var;
                }
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(objA);
            }
            we7 we7Var = (we7) objA;
            return (iba) y41.ab((List) we7Var.c.getL().b(iw7.b(List.class, KTypeProjection.INSTANCE.invariant(iw7.a(iba.class))), we7Var.a));
        } catch (Exception e) {
            Log.e("DeviceManager", "Error checking existing device", e);
            return null;
        }
    }

    public final void b() {
        this.e.setValue(Boolean.FALSE);
        g().edit().remove("device_registered").remove("user_id").apply();
        wj0.c(this.i, null, null, new i(this, null), 3);
    }

    /* JADX WARN: Code restructure failed: missing block: B:41:0x00c3, code lost:
    
        if (r11.d(r12, r0) == r1) goto L42;
     */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object c(String str, ContinuationImpl continuationImpl) {
        j jVar;
        Object next;
        DeviceManager deviceManager;
        if (continuationImpl instanceof j) {
            jVar = (j) continuationImpl;
            int i = jVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                jVar.d = i - Integer.MIN_VALUE;
            } else {
                jVar = new j(this, continuationImpl);
            }
        }
        Object obj = jVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = jVar.d;
        boolean z = true;
        try {
        } catch (Exception e) {
            Log.e("DeviceManager", "Failed to delete device", e);
            z = false;
        }
        if (i2 == 0) {
            s38.b(obj);
            Iterator it = ((Iterable) this.g.getValue()).iterator();
            while (true) {
                if (!it.hasNext()) {
                    next = null;
                    break;
                }
                next = it.next();
                if (((hba) next).a.equals(str)) {
                    break;
                }
            }
            hba hbaVar = (hba) next;
            if (hbaVar != null && hbaVar.e) {
                b();
            }
            ke7 ke7VarA = je7.a(this.a, "user_devices");
            ie7 ie7Var = ke7VarA.a;
            yj7 yj7Var = ie7Var.d.b;
            ve7 ve7Var = new ve7(yj7Var);
            LinkedHashMap linkedHashMap = ve7Var.b;
            new ae7(yj7Var, linkedHashMap).b(TtmlNode.ATTR_ID, str);
            k42 k42Var = new k42(aea.a(linkedHashMap), ke7VarA.c, ve7Var.c.m());
            l38 l38Var = l38.a;
            String str2 = ke7VarA.b;
            jVar.a = this;
            jVar.d = 1;
            if (l38Var.a(ie7Var, str2, k42Var, jVar) == ck1Var) {
                return ck1Var;
            }
            deviceManager = this;
        } else {
            if (i2 != 1) {
                if (i2 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
                return Boxing.boxBoolean(z);
            }
            deviceManager = jVar.a;
            s38.b(obj);
        }
        String string = deviceManager.g().getString("user_id", null);
        if (string != null) {
            jVar.a = null;
            jVar.d = 2;
        }
        return Boxing.boxBoolean(z);
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Serializable d(String str, ContinuationImpl continuationImpl) {
        k kVar;
        Object obj;
        DeviceManager deviceManager;
        if (continuationImpl instanceof k) {
            kVar = (k) continuationImpl;
            int i = kVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                kVar.d = i - Integer.MIN_VALUE;
            } else {
                kVar = new k(this, continuationImpl);
            }
        }
        Object objA = kVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = kVar.d;
        try {
            if (i2 == 0) {
                s38.b(objA);
                ke7 ke7VarA = je7.a(this.a, "user_devices");
                ie7 ie7Var = ke7VarA.a;
                yj7 yj7Var = ie7Var.d.b;
                rf8 rf8Var = new rf8(yj7Var);
                LinkedHashMap linkedHashMap = rf8Var.b;
                ae7 ae7Var = new ae7(yj7Var, linkedHashMap);
                ae7Var.a(l.a, str);
                ae7Var.a(m.a, Boxing.boxBoolean(true));
                linkedHashMap.put("select", y41.am("*"));
                qf8 qf8Var = new qf8(aea.a(linkedHashMap), ke7VarA.c, rf8Var.c.m());
                l38 l38Var = l38.a;
                String str2 = ke7VarA.b;
                kVar.a = this;
                kVar.d = 1;
                objA = l38Var.a(ie7Var, str2, qf8Var, kVar);
                if (objA == ck1Var) {
                    return ck1Var;
                }
                deviceManager = this;
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                deviceManager = kVar.a;
                s38.b(objA);
            }
            we7 we7Var = (we7) objA;
            List<iba> list = (List) we7Var.c.getL().b(iw7.b(List.class, KTypeProjection.INSTANCE.invariant(iw7.a(iba.class))), we7Var.a);
            ArrayList arrayList = new ArrayList(y41.p(list, 10));
            for (iba ibaVar : list) {
                String str3 = ibaVar.a;
                if (str3 == null) {
                    str3 = "";
                }
                String str4 = ibaVar.d;
                String str5 = ibaVar.c;
                String str6 = ibaVar.f;
                deviceManager.getClass();
                long epochMilli = 0;
                if (str6 != null && !n89.ab(str6)) {
                    try {
                        epochMilli = Instant.parse(str6).toEpochMilli();
                    } catch (Exception unused) {
                    }
                }
                arrayList.add(new hba(str3, str4, str5, epochMilli, cr4.b(ibaVar.c, (String) deviceManager.k.getValue())));
            }
            deviceManager.g.setValue(arrayList);
            obj = arrayList;
        } catch (Exception e) {
            Log.e("DeviceManager", "Failed to fetch user devices", e);
            obj = ik2.a;
        }
        return (Serializable) obj;
    }

    public final String e() {
        pba pbaVarO = e00.a(this.a).o();
        if (pbaVarO != null) {
            return pbaVarO.a;
        }
        return null;
    }

    public final String f() {
        return g().getString("user_id", null);
    }

    public final SharedPreferences g() {
        return (SharedPreferences) this.j.getValue();
    }

    public final boolean h() {
        return ((Boolean) this.e.getValue()).booleanValue();
    }

    /* JADX WARN: Code restructure failed: missing block: B:99:0x0252, code lost:
    
        if (r0 == r3) goto L100;
     */
    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:105:0x026b  */
    /* JADX WARN: Removed duplicated region for block: B:32:0x006d A[PHI: r0 r5 r14
      0x006d: PHI (r0v30 java.lang.Object) = (r0v28 java.lang.Object), (r0v1 java.lang.Object) binds: [B:56:0x00e0, B:31:0x006c] A[DONT_GENERATE, DONT_INLINE]
      0x006d: PHI (r5v13 ??) = (r5v25 ??), (r5v26 ??) binds: [B:56:0x00e0, B:31:0x006c] A[DONT_GENERATE, DONT_INLINE]
      0x006d: PHI (r14v16 ??) = (r14v27 ??), (r14v17 ??) binds: [B:56:0x00e0, B:31:0x006c] A[DONT_GENERATE, DONT_INLINE]] */
    /* JADX WARN: Removed duplicated region for block: B:51:0x00cc  */
    /* JADX WARN: Removed duplicated region for block: B:71:0x0110  */
    /* JADX WARN: Removed duplicated region for block: B:72:0x0112 A[Catch: Exception -> 0x0101, PHI: r0 r4 r14
      0x0112: PHI (r0v37 java.lang.Object) = (r0v33 java.lang.Object), (r0v1 java.lang.Object) binds: [B:70:0x010e, B:22:0x0052] A[DONT_GENERATE, DONT_INLINE]
      0x0112: PHI (r4v16 ??) = (r4v25 ??), (r4v18 ??) binds: [B:70:0x010e, B:22:0x0052] A[DONT_GENERATE, DONT_INLINE]
      0x0112: PHI (r14v20 ??) = (r14v25 ??), (r14v21 ??) binds: [B:70:0x010e, B:22:0x0052] A[DONT_GENERATE, DONT_INLINE], TryCatch #7 {Exception -> 0x0101, blocks: (B:86:0x0223, B:72:0x0112, B:74:0x011a, B:76:0x013d, B:77:0x01af, B:79:0x01b5, B:80:0x01c9, B:82:0x01da, B:83:0x01f4, B:65:0x00fb, B:58:0x00e4, B:60:0x00e8, B:62:0x00ec, B:69:0x0104), top: B:119:0x00e4 }] */
    /* JADX WARN: Removed duplicated region for block: B:74:0x011a A[Catch: Exception -> 0x0101, TryCatch #7 {Exception -> 0x0101, blocks: (B:86:0x0223, B:72:0x0112, B:74:0x011a, B:76:0x013d, B:77:0x01af, B:79:0x01b5, B:80:0x01c9, B:82:0x01da, B:83:0x01f4, B:65:0x00fb, B:58:0x00e4, B:60:0x00e8, B:62:0x00ec, B:69:0x0104), top: B:119:0x00e4 }] */
    /* JADX WARN: Removed duplicated region for block: B:76:0x013d A[Catch: Exception -> 0x0101, TryCatch #7 {Exception -> 0x0101, blocks: (B:86:0x0223, B:72:0x0112, B:74:0x011a, B:76:0x013d, B:77:0x01af, B:79:0x01b5, B:80:0x01c9, B:82:0x01da, B:83:0x01f4, B:65:0x00fb, B:58:0x00e4, B:60:0x00e8, B:62:0x00ec, B:69:0x0104), top: B:119:0x00e4 }] */
    /* JADX WARN: Removed duplicated region for block: B:8:0x001a  */
    /* JADX WARN: Removed duplicated region for block: B:96:0x023e  */
    /* JADX WARN: Type inference failed for: r14v0 */
    /* JADX WARN: Type inference failed for: r14v1 */
    /* JADX WARN: Type inference failed for: r14v10 */
    /* JADX WARN: Type inference failed for: r14v11 */
    /* JADX WARN: Type inference failed for: r14v12 */
    /* JADX WARN: Type inference failed for: r14v13, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v14 */
    /* JADX WARN: Type inference failed for: r14v15 */
    /* JADX WARN: Type inference failed for: r14v16, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v17 */
    /* JADX WARN: Type inference failed for: r14v18, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v19 */
    /* JADX WARN: Type inference failed for: r14v2 */
    /* JADX WARN: Type inference failed for: r14v20, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v21 */
    /* JADX WARN: Type inference failed for: r14v22, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v23 */
    /* JADX WARN: Type inference failed for: r14v24 */
    /* JADX WARN: Type inference failed for: r14v25 */
    /* JADX WARN: Type inference failed for: r14v26 */
    /* JADX WARN: Type inference failed for: r14v27 */
    /* JADX WARN: Type inference failed for: r14v28 */
    /* JADX WARN: Type inference failed for: r14v29 */
    /* JADX WARN: Type inference failed for: r14v3, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r14v30 */
    /* JADX WARN: Type inference failed for: r14v31 */
    /* JADX WARN: Type inference failed for: r14v32 */
    /* JADX WARN: Type inference failed for: r14v33 */
    /* JADX WARN: Type inference failed for: r14v4 */
    /* JADX WARN: Type inference failed for: r14v9, types: [java.lang.String] */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v2, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r2v26 */
    /* JADX WARN: Type inference failed for: r2v27 */
    /* JADX WARN: Type inference failed for: r2v4 */
    /* JADX WARN: Type inference failed for: r2v6, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r2v9 */
    /* JADX WARN: Type inference failed for: r4v0, types: [int] */
    /* JADX WARN: Type inference failed for: r4v10 */
    /* JADX WARN: Type inference failed for: r4v12, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r4v14, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r4v16, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r4v18 */
    /* JADX WARN: Type inference failed for: r4v19, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r4v23 */
    /* JADX WARN: Type inference failed for: r4v24 */
    /* JADX WARN: Type inference failed for: r4v25 */
    /* JADX WARN: Type inference failed for: r4v26 */
    /* JADX WARN: Type inference failed for: r4v27 */
    /* JADX WARN: Type inference failed for: r4v28 */
    /* JADX WARN: Type inference failed for: r4v29 */
    /* JADX WARN: Type inference failed for: r4v30 */
    /* JADX WARN: Type inference failed for: r4v4 */
    /* JADX WARN: Type inference failed for: r4v7 */
    /* JADX WARN: Type inference failed for: r5v10, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r5v11 */
    /* JADX WARN: Type inference failed for: r5v12, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r5v13 */
    /* JADX WARN: Type inference failed for: r5v2 */
    /* JADX WARN: Type inference failed for: r5v24 */
    /* JADX WARN: Type inference failed for: r5v25 */
    /* JADX WARN: Type inference failed for: r5v26 */
    /* JADX WARN: Type inference failed for: r5v27 */
    /* JADX WARN: Type inference failed for: r5v28 */
    /* JADX WARN: Type inference failed for: r5v29 */
    /* JADX WARN: Type inference failed for: r5v30 */
    /* JADX WARN: Type inference failed for: r5v31 */
    /* JADX WARN: Type inference failed for: r5v5 */
    /* JADX WARN: Type inference failed for: r5v6, types: [com.strmr.ps.data.supabase.DeviceManager] */
    /* JADX WARN: Type inference failed for: r5v8 */
    /* JADX WARN: Type inference failed for: r5v9 */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object i(String str, String str2, String str3, ContinuationImpl continuationImpl) {
        n nVar;
        ?? r14;
        ?? r142;
        ?? r5;
        ?? r2;
        ?? r143;
        String message;
        Object cVar;
        String message2;
        ?? r4;
        ?? r22;
        String str4;
        pba pbaVar;
        vx vxVarA;
        vx vxVarA2;
        ?? r42;
        ?? r52;
        ?? r144;
        ?? r53;
        ?? r43;
        iba ibaVar;
        String str5;
        ?? r145;
        ?? r44;
        ?? r146;
        ?? r45;
        List list;
        ?? r147;
        ?? r46;
        if (continuationImpl instanceof n) {
            nVar = (n) continuationImpl;
            int i = nVar.e;
            r5 = -2147483648;
            if ((i & Integer.MIN_VALUE) != 0) {
                nVar.e = i - Integer.MIN_VALUE;
            } else {
                nVar = new n(this, continuationImpl);
            }
        }
        n nVar2 = nVar;
        Object objD = nVar2.c;
        Object obj = ck1.a;
        ?? r47 = nVar2.e;
        try {
            try {
            } catch (Exception e) {
                e = e;
                r142 = r47;
            }
        } catch (Exception e2) {
            e = e2;
            r14 = "Device limit exceeded: ";
            r4 = r47;
        }
        switch (r47) {
            case 0:
                s38.b(objD);
                try {
                    pbaVar = new pba(str2, str3, null, null, 3600L, "bearer", null, 396);
                    vxVarA = e00.a(this.a);
                    nVar2.a = this;
                    str4 = str;
                } catch (Exception e3) {
                    e = e3;
                    str4 = str;
                }
                try {
                    nVar2.b = str4;
                    nVar2.e = 1;
                    if (vx.b.a(vxVarA, pbaVar, true, null, nVar2, 4) != obj) {
                        r5 = this;
                        r142 = str4;
                        try {
                            vxVarA2 = e00.a(r5.a);
                            nVar2.a = r5;
                            nVar2.b = r142;
                            nVar2.e = 2;
                            break;
                        } catch (Exception e4) {
                            e = e4;
                            try {
                                Log.w("DeviceManager", "Failed to refresh for device-specific tokens, using original", e);
                                r5 = r5;
                                r142 = r142;
                            } catch (Exception e5) {
                                e = e5;
                                r2 = r5;
                                r143 = r142;
                                Log.e("DeviceManager", "Device registration failed", e);
                                message = e.getMessage();
                                if (message != null) {
                                }
                                message2 = e.getMessage();
                                if (message2 == null) {
                                }
                                cVar = new k92.c(message2);
                                return cVar;
                            }
                        }
                        if (vxVarA2.j(nVar2) != obj) {
                            r42 = r142;
                            r52 = r5;
                            r142 = r42;
                            r5 = r52;
                            nVar2.a = r5;
                            nVar2.b = r142;
                            nVar2.e = 3;
                            objD = r5.a(r142, nVar2);
                            r53 = r5;
                            r144 = r142;
                            if (objD != obj) {
                                r43 = r53;
                                try {
                                    ibaVar = (iba) objD;
                                    if (ibaVar == null && (str5 = ibaVar.a) != null) {
                                        nVar2.a = r43;
                                        nVar2.b = r144;
                                        nVar2.e = 4;
                                        Object objM = r43.m(str5, nVar2);
                                        r44 = r43;
                                        r145 = r144;
                                        if (objM == obj) {
                                        }
                                        r44.l(r145);
                                        return k92.a.a;
                                    }
                                    nVar2.a = r43;
                                    nVar2.b = r144;
                                    nVar2.e = 5;
                                    objD = r43.d(r144, nVar2);
                                    r45 = r43;
                                    r146 = r144;
                                    if (objD == obj) {
                                        list = (List) objD;
                                        if (list.size() < 5) {
                                            Log.w("DeviceManager", "Device limit exceeded: " + list.size() + "/5");
                                            r45.g.setValue(list);
                                            return new k92.b(list);
                                        }
                                        String str6 = r45.l;
                                        ke7 ke7VarA = je7.a(r45.a, "user_devices");
                                        List listAm = y41.am(new iba(r146, (String) r45.k.getValue(), r45.l, Instant.now().toString()));
                                        kn4 kn4Var = new kn4(ke7VarA.a.d.b);
                                        LinkedHashMap linkedHashMap = kn4Var.b;
                                        vd9 vd9Var = ke7VarA.a.e;
                                        dy4.a aVar = dy4.d;
                                        String strA = vd9Var.a(iw7.b(List.class, KTypeProjection.INSTANCE.invariant(iw7.a(iba.class))), listAm);
                                        aVar.getClass();
                                        ey4 ey4VarF = yy4.f((wy4) aVar.c(wy4.INSTANCE.serializer(), strA));
                                        ArrayList arrayList = new ArrayList(y41.p(ey4VarF, 10));
                                        Iterator it = ey4VarF.a.iterator();
                                        while (it.hasNext()) {
                                            arrayList.add(yy4.g((wy4) it.next()).a.keySet());
                                        }
                                        List listT = y41.t(y41.ac(arrayList));
                                        if (!listT.isEmpty()) {
                                            linkedHashMap.put("columns", y41.am(y41.ai(listT, ",", null, null, null, 62)));
                                        }
                                        jn4 jn4Var = new jn4(false, kn4Var.d, ey4VarF, aea.a(linkedHashMap), ke7VarA.c, kn4Var.c.m());
                                        l38 l38Var = l38.a;
                                        ie7 ie7Var = ke7VarA.a;
                                        String str7 = ke7VarA.b;
                                        nVar2.a = r45;
                                        nVar2.b = r146;
                                        nVar2.e = 6;
                                        r46 = r45;
                                        r147 = r146;
                                        if (l38Var.a(ie7Var, str7, jn4Var, nVar2) == obj) {
                                        }
                                        r46.l(r147);
                                        return k92.d.a;
                                    }
                                } catch (Exception e6) {
                                    e = e6;
                                    r4 = r43;
                                    r14 = r144;
                                    r2 = r4;
                                    r143 = r14;
                                    Log.e("DeviceManager", "Device registration failed", e);
                                    message = e.getMessage();
                                    if (message != null) {
                                    }
                                    message2 = e.getMessage();
                                    if (message2 == null) {
                                    }
                                    cVar = new k92.c(message2);
                                    return cVar;
                                }
                            }
                        }
                    }
                } catch (Exception e7) {
                    e = e7;
                    r2 = this;
                    r143 = str4;
                    Log.e("DeviceManager", "Device registration failed", e);
                    message = e.getMessage();
                    if (message != null || !n89.l(message, "row-level security", true)) {
                        message2 = e.getMessage();
                        if (message2 == null) {
                            message2 = "Registration failed";
                        }
                        cVar = new k92.c(message2);
                        return cVar;
                    }
                    nVar2.a = r2;
                    nVar2.b = null;
                    nVar2.e = 7;
                    objD = r2.d(r143, nVar2);
                    r22 = r2;
                    break;
                }
                return obj;
            case 1:
                String str8 = nVar2.b;
                DeviceManager deviceManager = nVar2.a;
                s38.b(objD);
                r142 = str8;
                r5 = deviceManager;
                vxVarA2 = e00.a(r5.a);
                nVar2.a = r5;
                nVar2.b = r142;
                nVar2.e = 2;
                if (vxVarA2.j(nVar2) != obj) {
                }
                return obj;
            case 2:
                String str9 = nVar2.b;
                r5 = nVar2.a;
                try {
                    s38.b(objD);
                    r42 = str9;
                    r52 = r5;
                    r142 = r42;
                    r5 = r52;
                } catch (Exception e8) {
                    e = e8;
                    r142 = str9;
                    Log.w("DeviceManager", "Failed to refresh for device-specific tokens, using original", e);
                    r5 = r5;
                    r142 = r142;
                }
                nVar2.a = r5;
                nVar2.b = r142;
                nVar2.e = 3;
                objD = r5.a(r142, nVar2);
                r53 = r5;
                r144 = r142;
                if (objD != obj) {
                }
                return obj;
            case 3:
                String str10 = nVar2.b;
                DeviceManager deviceManager2 = nVar2.a;
                s38.b(objD);
                r144 = str10;
                r53 = deviceManager2;
                r43 = r53;
                ibaVar = (iba) objD;
                if (ibaVar == null) {
                    break;
                }
                nVar2.a = r43;
                nVar2.b = r144;
                nVar2.e = 5;
                objD = r43.d(r144, nVar2);
                r45 = r43;
                r146 = r144;
                if (objD == obj) {
                }
                return obj;
            case 4:
                String str11 = nVar2.b;
                DeviceManager deviceManager3 = nVar2.a;
                s38.b(objD);
                r145 = str11;
                r44 = deviceManager3;
                r44.l(r145);
                return k92.a.a;
            case 5:
                String str12 = nVar2.b;
                DeviceManager deviceManager4 = nVar2.a;
                s38.b(objD);
                r146 = str12;
                r45 = deviceManager4;
                list = (List) objD;
                if (list.size() < 5) {
                }
                break;
            case 6:
                String str13 = nVar2.b;
                DeviceManager deviceManager5 = nVar2.a;
                s38.b(objD);
                r147 = str13;
                r46 = deviceManager5;
                r46.l(r147);
                return k92.d.a;
            case 7:
                DeviceManager deviceManager6 = nVar2.a;
                s38.b(objD);
                r22 = deviceManager6;
                List list2 = (List) objD;
                r22.g.setValue(list2);
                cVar = new k92.b(list2);
                return cVar;
            default:
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0019  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object j(String str, ContinuationImpl continuationImpl) {
        o oVar;
        DeviceManager deviceManager;
        String str2 = str;
        if (continuationImpl instanceof o) {
            oVar = (o) continuationImpl;
            int i = oVar.e;
            if ((i & Integer.MIN_VALUE) != 0) {
                oVar.e = i - Integer.MIN_VALUE;
            } else {
                oVar = new o(this, continuationImpl);
            }
        }
        Object obj = oVar.c;
        ck1 ck1Var = ck1.a;
        int i2 = oVar.e;
        try {
            if (i2 == 0) {
                s38.b(obj);
                ke7 ke7VarA = je7.a(this.a, "user_devices");
                List listAm = y41.am(new iba(str2, (String) this.k.getValue(), this.l, Instant.now().toString()));
                ie7 ie7Var = ke7VarA.a;
                kn4 kn4Var = new kn4(ie7Var.d.b);
                LinkedHashMap linkedHashMap = kn4Var.b;
                vd9 vd9Var = ie7Var.e;
                dy4.a aVar = dy4.d;
                String strA = vd9Var.a(iw7.b(List.class, KTypeProjection.INSTANCE.invariant(iw7.a(iba.class))), listAm);
                aVar.getClass();
                ey4 ey4VarF = yy4.f((wy4) aVar.c(wy4.INSTANCE.serializer(), strA));
                ArrayList arrayList = new ArrayList(y41.p(ey4VarF, 10));
                Iterator it = ey4VarF.a.iterator();
                while (it.hasNext()) {
                    arrayList.add(yy4.g((wy4) it.next()).a.keySet());
                }
                List listT = y41.t(y41.ac(arrayList));
                if (!listT.isEmpty()) {
                    linkedHashMap.put("columns", y41.am(y41.ai(listT, ",", null, null, null, 62)));
                }
                jn4 jn4Var = new jn4(false, kn4Var.d, ey4VarF, aea.a(linkedHashMap), ke7VarA.c, kn4Var.c.m());
                l38 l38Var = l38.a;
                String str3 = ke7VarA.b;
                oVar.a = this;
                oVar.b = str2;
                oVar.e = 1;
                if (l38Var.a(ie7Var, str3, jn4Var, oVar) == ck1Var) {
                    return ck1Var;
                }
                deviceManager = this;
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                str2 = oVar.b;
                deviceManager = oVar.a;
                s38.b(obj);
            }
            deviceManager.l(str2);
            return k92.d.a;
        } catch (Exception e) {
            Log.e("DeviceManager", "Device registration failed", e);
            String message = e.getMessage();
            if (message == null) {
                message = "Registration failed";
            }
            return new k92.c(message);
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object k(ContinuationImpl continuationImpl) {
        p pVar;
        DeviceManager deviceManager;
        DeviceManager deviceManager2;
        if (continuationImpl instanceof p) {
            pVar = (p) continuationImpl;
            int i = pVar.d;
            if ((i & Integer.MIN_VALUE) != 0) {
                pVar.d = i - Integer.MIN_VALUE;
            } else {
                pVar = new p(this, continuationImpl);
            }
        }
        Object objQ = pVar.b;
        ck1 ck1Var = ck1.a;
        int i2 = pVar.d;
        boolean z = false;
        try {
        } catch (Exception e) {
            Log.e("DeviceManager", "Failed to restore session", e);
        }
        if (i2 == 0) {
            s38.b(objQ);
            if (!h()) {
                return Boxing.boxBoolean(false);
            }
            if (this.d) {
                Log.w("DeviceManager", "Circuit breaker is tripped, skipping session restore");
                return Boxing.boxBoolean(false);
            }
            vx vxVarA = e00.a(this.a);
            pVar.a = this;
            pVar.d = 1;
            objQ = vxVarA.q(true, pVar);
            if (objQ == ck1Var) {
                return ck1Var;
            }
            deviceManager = this;
        } else {
            if (i2 != 1) {
                if (i2 != 2) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                deviceManager2 = pVar.a;
                s38.b(objQ);
                boolean z2 = e00.a(deviceManager2.a).o() != null;
                z = z2;
                return Boxing.boxBoolean(z);
            }
            deviceManager = pVar.a;
            s38.b(objQ);
        }
        if (((Boolean) objQ).booleanValue()) {
            vx vxVarA2 = e00.a(deviceManager.a);
            pVar.a = deviceManager;
            pVar.d = 2;
            if (vxVarA2.c(pVar) != ck1Var) {
                deviceManager2 = deviceManager;
                if (e00.a(deviceManager2.a).o() != null) {
                }
                z = z2;
                return Boxing.boxBoolean(z);
            }
            return ck1Var;
        }
        z = z2;
        return Boxing.boxBoolean(z);
    }

    public final void l(String str) {
        this.e.setValue(Boolean.TRUE);
        g().edit().putBoolean("device_registered", true).putString("user_id", str).apply();
    }

    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object m(String str, ContinuationImpl continuationImpl) {
        s sVar;
        if (continuationImpl instanceof s) {
            sVar = (s) continuationImpl;
            int i = sVar.c;
            if ((i & Integer.MIN_VALUE) != 0) {
                sVar.c = i - Integer.MIN_VALUE;
            } else {
                sVar = new s(this, continuationImpl);
            }
        }
        Object obj = sVar.a;
        ck1 ck1Var = ck1.a;
        int i2 = sVar.c;
        try {
            if (i2 == 0) {
                s38.b(obj);
                ke7 ke7VarA = je7.a(this.a, "user_devices");
                Map mapI = wo5.i(new q07("last_active", Instant.now().toString()));
                ie7 ie7Var = ke7VarA.a;
                yj7 yj7Var = ie7Var.d.b;
                ve7 ve7Var = new ve7(yj7Var);
                LinkedHashMap linkedHashMap = ve7Var.b;
                new ae7(yj7Var, linkedHashMap).b(TtmlNode.ATTR_ID, str);
                LinkedHashMap linkedHashMapA = aea.a(linkedHashMap);
                vd9 vd9Var = ie7Var.e;
                dy4.a aVar = dy4.d;
                KTypeProjection.Companion companion = KTypeProjection.INSTANCE;
                KTypeProjection kTypeProjectionInvariant = companion.invariant(iw7.a(String.class));
                KTypeProjection kTypeProjectionInvariant2 = companion.invariant(iw7.a(String.class));
                kw7 kw7Var = iw7.a;
                String strA = vd9Var.a(kw7Var.typeOf(kw7Var.getOrCreateKotlinClass(Map.class), Arrays.asList(kTypeProjectionInvariant, kTypeProjectionInvariant2), false), mapI);
                aVar.getClass();
                q9a q9aVar = new q9a(linkedHashMapA, (wy4) aVar.c(wy4.INSTANCE.serializer(), strA), ke7VarA.c, ve7Var.c.m());
                l38 l38Var = l38.a;
                String str2 = ke7VarA.b;
                sVar.c = 1;
                if (l38Var.a(ie7Var, str2, q9aVar, sVar) == ck1Var) {
                    return ck1Var;
                }
            } else {
                if (i2 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                s38.b(obj);
            }
        } catch (Exception e) {
            Log.e("DeviceManager", "Failed to update last_active", e);
        }
        return e6a.a;
    }
}
