package rd;

import androidx.lifecycle.a0;
import androidx.lifecycle.f0;
import ca.canadapost.core.data.auth.api.IForgotPasswordApi;
import ca.canadapost.core.data.auth.model.ErrorMessage;
import ca.canadapost.core.data.auth.model.ForgotPasswordResponse;
import ca.canadapost.core.data.db.cupboard.model.EPostLogin;
import ca.canadapost.core.data.model.RepositoryError;
import ca.canadapost.core.data.model.ResultWrapper;
import java.io.IOException;
import java.util.List;
import kotlin.coroutines.jvm.internal.d;
import kotlin.jvm.internal.t;
import rm.y;

/* loaded from: classes4.dex */
public final class a implements rd.c {

    /* renamed from: a, reason: collision with root package name */
    private final IForgotPasswordApi f34616a;

    /* renamed from: b, reason: collision with root package name */
    private final ca.a f34617b;

    /* renamed from: c, reason: collision with root package name */
    private final ja.c f34618c;

    /* renamed from: d, reason: collision with root package name */
    private final f0 f34619d;

    /* renamed from: e, reason: collision with root package name */
    private final a0 f34620e;

    /* renamed from: f, reason: collision with root package name */
    private final f0 f34621f;

    /* renamed from: g, reason: collision with root package name */
    private final a0 f34622g;

    /* renamed from: h, reason: collision with root package name */
    private final f0 f34623h;

    /* renamed from: i, reason: collision with root package name */
    private final a0 f34624i;

    /* renamed from: j, reason: collision with root package name */
    private int f34625j;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: rd.a$a, reason: collision with other inner class name */
    /* loaded from: classes4.dex */
    public static final class C0819a extends d {

        /* renamed from: p, reason: collision with root package name */
        Object f34626p;

        /* renamed from: q, reason: collision with root package name */
        /* synthetic */ Object f34627q;

        /* renamed from: s, reason: collision with root package name */
        int f34629s;

        C0819a(vm.d dVar) {
            super(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f34627q = obj;
            this.f34629s |= Integer.MIN_VALUE;
            return a.this.b(null, this);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class b extends d {

        /* renamed from: p, reason: collision with root package name */
        Object f34630p;

        /* renamed from: q, reason: collision with root package name */
        /* synthetic */ Object f34631q;

        /* renamed from: s, reason: collision with root package name */
        int f34633s;

        b(vm.d dVar) {
            super(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f34631q = obj;
            this.f34633s |= Integer.MIN_VALUE;
            return a.this.d(null, null, null, this);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes4.dex */
    public static final class c extends d {

        /* renamed from: p, reason: collision with root package name */
        Object f34634p;

        /* renamed from: q, reason: collision with root package name */
        /* synthetic */ Object f34635q;

        /* renamed from: s, reason: collision with root package name */
        int f34637s;

        c(vm.d dVar) {
            super(dVar);
        }

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f34635q = obj;
            this.f34637s |= Integer.MIN_VALUE;
            return a.this.c(null, null, null, this);
        }
    }

    public a(IForgotPasswordApi forgotPasswordApi, ca.a languageService, ja.c sharedPreferences) {
        t.g(forgotPasswordApi, "forgotPasswordApi");
        t.g(languageService, "languageService");
        t.g(sharedPreferences, "sharedPreferences");
        this.f34616a = forgotPasswordApi;
        this.f34617b = languageService;
        this.f34618c = sharedPreferences;
        f0 f0Var = new f0();
        this.f34619d = f0Var;
        this.f34620e = f0Var;
        f0 f0Var2 = new f0();
        this.f34621f = f0Var2;
        this.f34622g = f0Var2;
        f0 f0Var3 = new f0();
        this.f34623h = f0Var3;
        this.f34624i = f0Var3;
        this.f34625j = 1;
    }

    private final String e(ForgotPasswordResponse forgotPasswordResponse) {
        String str = forgotPasswordResponse.errorMessage;
        boolean z10 = true;
        if (!(str == null || str.length() == 0)) {
            return forgotPasswordResponse.errorMessage;
        }
        List<ErrorMessage> list = forgotPasswordResponse.errorMessageList;
        if (list != null && !list.isEmpty()) {
            z10 = false;
        }
        if (z10) {
            return null;
        }
        List<ErrorMessage> list2 = forgotPasswordResponse.errorMessageList;
        t.d(list2);
        ErrorMessage errorMessage = list2.get(0);
        if (errorMessage != null) {
            return errorMessage.message;
        }
        return null;
    }

    private final boolean h(ForgotPasswordResponse forgotPasswordResponse) {
        String str = forgotPasswordResponse.errorMessage;
        if (str == null || str.length() == 0) {
            List<ErrorMessage> list = forgotPasswordResponse.errorMessageList;
            if (list == null || list.isEmpty()) {
                return false;
            }
        }
        return true;
    }

    private final EPostLogin i() {
        try {
            if (this.f34618c.a("EPOST_LOGIN")) {
                return (EPostLogin) new com.google.gson.d().j(this.f34618c.getString("EPOST_LOGIN", ""), EPostLogin.class);
            }
            return null;
        } catch (Exception unused) {
            return null;
        }
    }

    private final void j(String str) {
        this.f34619d.n(str);
        this.f34621f.n(null);
        this.f34623h.n(null);
    }

    private final void k(EPostLogin ePostLogin) {
        EPostLogin i10 = i();
        if (i10 != null) {
            ePostLogin._id = i10._id;
        }
        this.f34618c.putString("EPOST_LOGIN", new com.google.gson.d().u(ePostLogin));
    }

    private final void l() {
        EPostLogin ePostLogin = new EPostLogin();
        ePostLogin.setAutoLogin(Boolean.FALSE);
        ePostLogin.setKeepUsername(Boolean.TRUE);
        ePostLogin.setUsername((String) getUsername().f());
        ePostLogin.setPassword((String) f().f());
        k(ePostLogin);
    }

    @Override // rd.c
    public void a() {
        this.f34625j = 1;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0076 A[Catch: Exception -> 0x0091, TryCatch #0 {Exception -> 0x0091, blocks: (B:11:0x002a, B:12:0x006e, B:14:0x0076, B:17:0x007f, B:22:0x003e), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:17:0x007f A[Catch: Exception -> 0x0091, TRY_LEAVE, TryCatch #0 {Exception -> 0x0091, blocks: (B:11:0x002a, B:12:0x006e, B:14:0x0076, B:17:0x007f, B:22:0x003e), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0036  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0024  */
    @Override // rd.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object b(String str, vm.d dVar) {
        C0819a c0819a;
        Object f10;
        int i10;
        ForgotPasswordResponse forgotPasswordResponse;
        try {
            if (dVar instanceof C0819a) {
                c0819a = (C0819a) dVar;
                int i11 = c0819a.f34629s;
                if ((i11 & Integer.MIN_VALUE) != 0) {
                    c0819a.f34629s = i11 - Integer.MIN_VALUE;
                    C0819a c0819a2 = c0819a;
                    Object obj = c0819a2.f34627q;
                    f10 = wm.d.f();
                    i10 = c0819a2.f34629s;
                    if (i10 != 0) {
                        y.b(obj);
                        this.f34621f.n(str);
                        IForgotPasswordApi iForgotPasswordApi = this.f34616a;
                        String str2 = (String) getUsername().f();
                        String c10 = this.f34617b.c();
                        String str3 = (String) g().f();
                        String valueOf = String.valueOf(this.f34625j);
                        c0819a2.f34626p = this;
                        c0819a2.f34629s = 1;
                        obj = iForgotPasswordApi.forgotPassword3Request(str2, c10, str3, valueOf, str, str, c0819a2);
                        if (obj == f10) {
                            return f10;
                        }
                    } else {
                        if (i10 != 1) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        this = (a) c0819a2.f34626p;
                        y.b(obj);
                    }
                    forgotPasswordResponse = (ForgotPasswordResponse) obj;
                    if (!this.h(forgotPasswordResponse)) {
                        return new ResultWrapper.Error(null, this.e(forgotPasswordResponse), RepositoryError.ServiceError, null, 8, null);
                    }
                    this.l();
                    return new ResultWrapper.Success(forgotPasswordResponse);
                }
            }
            if (i10 != 0) {
            }
            forgotPasswordResponse = (ForgotPasswordResponse) obj;
            if (!this.h(forgotPasswordResponse)) {
            }
        } catch (Exception e10) {
            return new ResultWrapper.Error(e10, "Error while getting Response", RepositoryError.ServiceError, null, 8, null);
        }
        c0819a = new C0819a(dVar);
        C0819a c0819a22 = c0819a;
        Object obj2 = c0819a22.f34627q;
        f10 = wm.d.f();
        i10 = c0819a22.f34629s;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0064 A[Catch: Exception -> 0x008b, IOException -> 0x009c, TryCatch #2 {IOException -> 0x009c, Exception -> 0x008b, blocks: (B:11:0x002a, B:12:0x005c, B:14:0x0064, B:17:0x0075, B:22:0x003c), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0075 A[Catch: Exception -> 0x008b, IOException -> 0x009c, TRY_LEAVE, TryCatch #2 {IOException -> 0x009c, Exception -> 0x008b, blocks: (B:11:0x002a, B:12:0x005c, B:14:0x0064, B:17:0x0075, B:22:0x003c), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0036  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0024  */
    @Override // rd.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object c(String str, String str2, String str3, vm.d dVar) {
        c cVar;
        Object f10;
        int i10;
        ForgotPasswordResponse forgotPasswordResponse;
        try {
            if (dVar instanceof c) {
                cVar = (c) dVar;
                int i11 = cVar.f34637s;
                if ((i11 & Integer.MIN_VALUE) != 0) {
                    cVar.f34637s = i11 - Integer.MIN_VALUE;
                    c cVar2 = cVar;
                    Object obj = cVar2.f34635q;
                    f10 = wm.d.f();
                    i10 = cVar2.f34637s;
                    if (i10 != 0) {
                        y.b(obj);
                        j(str3);
                        IForgotPasswordApi iForgotPasswordApi = this.f34616a;
                        String c10 = this.f34617b.c();
                        String valueOf = String.valueOf(this.f34625j);
                        cVar2.f34634p = this;
                        cVar2.f34637s = 1;
                        obj = IForgotPasswordApi.a.a(iForgotPasswordApi, str3, c10, str, str2, valueOf, null, cVar2, 32, null);
                        if (obj == f10) {
                            return f10;
                        }
                    } else {
                        if (i10 != 1) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        this = (a) cVar2.f34634p;
                        y.b(obj);
                    }
                    forgotPasswordResponse = (ForgotPasswordResponse) obj;
                    if (!this.h(forgotPasswordResponse)) {
                        this.f34625j = forgotPasswordResponse.attempts;
                        return new ResultWrapper.Error(null, this.e(forgotPasswordResponse), RepositoryError.ServiceError, null, 8, null);
                    }
                    this.f34623h.n(forgotPasswordResponse.tempPassword);
                    this.f34625j = forgotPasswordResponse.attempts;
                    return new ResultWrapper.Success(forgotPasswordResponse);
                }
            }
            if (i10 != 0) {
            }
            forgotPasswordResponse = (ForgotPasswordResponse) obj;
            if (!this.h(forgotPasswordResponse)) {
            }
        } catch (IOException e10) {
            return new ResultWrapper.Error(e10, "Error while getting Response", RepositoryError.NoNetworkError, null, 8, null);
        } catch (Exception e11) {
            return new ResultWrapper.Error(e11, "Error while getting Response", RepositoryError.ServiceError, null, 8, null);
        }
        cVar = new c(dVar);
        c cVar22 = cVar;
        Object obj2 = cVar22.f34635q;
        f10 = wm.d.f();
        i10 = cVar22.f34637s;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x0060 A[Catch: Exception -> 0x0087, IOException -> 0x0098, TryCatch #2 {IOException -> 0x0098, Exception -> 0x0087, blocks: (B:11:0x002a, B:12:0x0058, B:14:0x0060, B:17:0x0071, B:22:0x003c), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0071 A[Catch: Exception -> 0x0087, IOException -> 0x0098, TRY_LEAVE, TryCatch #2 {IOException -> 0x0098, Exception -> 0x0087, blocks: (B:11:0x002a, B:12:0x0058, B:14:0x0060, B:17:0x0071, B:22:0x003c), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:21:0x0036  */
    /* JADX WARN: Removed duplicated region for block: B:9:0x0024  */
    @Override // rd.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object d(String str, String str2, String str3, vm.d dVar) {
        b bVar;
        Object f10;
        int i10;
        ForgotPasswordResponse forgotPasswordResponse;
        try {
            if (dVar instanceof b) {
                bVar = (b) dVar;
                int i11 = bVar.f34633s;
                if ((i11 & Integer.MIN_VALUE) != 0) {
                    bVar.f34633s = i11 - Integer.MIN_VALUE;
                    b bVar2 = bVar;
                    Object obj = bVar2.f34631q;
                    f10 = wm.d.f();
                    i10 = bVar2.f34633s;
                    if (i10 != 0) {
                        y.b(obj);
                        j(str3);
                        IForgotPasswordApi iForgotPasswordApi = this.f34616a;
                        String c10 = this.f34617b.c();
                        String valueOf = String.valueOf(this.f34625j);
                        bVar2.f34630p = this;
                        bVar2.f34633s = 1;
                        obj = iForgotPasswordApi.forgotPassword2Request(str3, c10, str, str2, valueOf, bVar2);
                        if (obj == f10) {
                            return f10;
                        }
                    } else {
                        if (i10 != 1) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        this = (a) bVar2.f34630p;
                        y.b(obj);
                    }
                    forgotPasswordResponse = (ForgotPasswordResponse) obj;
                    if (!this.h(forgotPasswordResponse)) {
                        this.f34625j = forgotPasswordResponse.attempts;
                        return new ResultWrapper.Error(null, this.e(forgotPasswordResponse), RepositoryError.ServiceError, null, 8, null);
                    }
                    this.f34623h.n(forgotPasswordResponse.tempPassword);
                    this.f34625j = forgotPasswordResponse.attempts;
                    return new ResultWrapper.Success(forgotPasswordResponse);
                }
            }
            if (i10 != 0) {
            }
            forgotPasswordResponse = (ForgotPasswordResponse) obj;
            if (!this.h(forgotPasswordResponse)) {
            }
        } catch (IOException e10) {
            return new ResultWrapper.Error(e10, "Error while getting Response", RepositoryError.NoNetworkError, null, 8, null);
        } catch (Exception e11) {
            return new ResultWrapper.Error(e11, "Error while getting Response", RepositoryError.ServiceError, null, 8, null);
        }
        bVar = new b(dVar);
        b bVar22 = bVar;
        Object obj2 = bVar22.f34631q;
        f10 = wm.d.f();
        i10 = bVar22.f34633s;
    }

    public a0 f() {
        return this.f34622g;
    }

    public a0 g() {
        return this.f34624i;
    }

    @Override // rd.c
    public a0 getUsername() {
        return this.f34620e;
    }
}
