package td;

import ca.canadapost.core.data.auth.api.IForgotUsernameApi;
import ca.canadapost.core.data.auth.model.ErrorMessage;
import ca.canadapost.core.data.auth.model.ForgotUsernameResponse;
import ca.canadapost.core.data.db.cupboard.model.EPostLogin;
import ca.canadapost.core.data.model.RepositoryError;
import ca.canadapost.core.data.model.ResultWrapper;
import ja.d;
import java.io.IOException;
import java.util.List;
import kotlin.jvm.internal.t;
import rm.y;

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

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

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

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

    /* renamed from: d, reason: collision with root package name */
    private int f36294d;

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

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

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

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

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

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

    public a(IForgotUsernameApi forgotUsernameApi, ca.a languageService, d sharedPreferences) {
        t.g(forgotUsernameApi, "forgotUsernameApi");
        t.g(languageService, "languageService");
        t.g(sharedPreferences, "sharedPreferences");
        this.f36291a = forgotUsernameApi;
        this.f36292b = languageService;
        this.f36293c = sharedPreferences;
    }

    private final String c(ForgotUsernameResponse forgotUsernameResponse) {
        List<ErrorMessage> list = forgotUsernameResponse.errorMessageList;
        if (list == null || list.isEmpty()) {
            return null;
        }
        List<ErrorMessage> list2 = forgotUsernameResponse.errorMessageList;
        t.d(list2);
        ErrorMessage errorMessage = list2.get(0);
        if (errorMessage != null) {
            return errorMessage.message;
        }
        return null;
    }

    private final boolean d(ForgotUsernameResponse forgotUsernameResponse) {
        List<ErrorMessage> list = forgotUsernameResponse.errorMessageList;
        return !(list == null || list.isEmpty());
    }

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

    private final void f(EPostLogin ePostLogin) {
        EPostLogin e10 = e();
        if (e10 != null) {
            ePostLogin._id = e10._id;
        }
        this.f36293c.putString("EPOST_LOGIN", new com.google.gson.d().u(ePostLogin));
    }

    private final void g(String str) {
        EPostLogin ePostLogin = new EPostLogin();
        ePostLogin.setAutoLogin(Boolean.FALSE);
        ePostLogin.setKeepUsername(Boolean.TRUE);
        ePostLogin.setUsername(str);
        f(ePostLogin);
    }

    @Override // td.c
    public void a() {
        this.f36294d = 0;
    }

    /* JADX WARN: Removed duplicated region for block: B:14:0x005f A[Catch: Exception -> 0x0089, IOException -> 0x009a, TryCatch #2 {IOException -> 0x009a, Exception -> 0x0089, blocks: (B:11:0x002a, B:12:0x0057, B:14:0x005f, B:17:0x0073, B:22:0x0039), top: B:7:0x0022 }] */
    /* JADX WARN: Removed duplicated region for block: B:17:0x0073 A[Catch: Exception -> 0x0089, IOException -> 0x009a, TRY_LEAVE, TryCatch #2 {IOException -> 0x009a, Exception -> 0x0089, blocks: (B:11:0x002a, B:12:0x0057, B:14:0x005f, B:17:0x0073, B:22:0x0039), 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 // td.c
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object b(String str, String str2, String str3, String str4, vm.d dVar) {
        C0854a c0854a;
        Object f10;
        int i10;
        ForgotUsernameResponse forgotUsernameResponse;
        try {
            if (dVar instanceof C0854a) {
                c0854a = (C0854a) dVar;
                int i11 = c0854a.f36298s;
                if ((i11 & Integer.MIN_VALUE) != 0) {
                    c0854a.f36298s = i11 - Integer.MIN_VALUE;
                    C0854a c0854a2 = c0854a;
                    Object obj = c0854a2.f36296q;
                    f10 = wm.d.f();
                    i10 = c0854a2.f36298s;
                    if (i10 != 0) {
                        y.b(obj);
                        IForgotUsernameApi iForgotUsernameApi = this.f36291a;
                        String a10 = this.f36292b.a();
                        String valueOf = String.valueOf(this.f36294d);
                        c0854a2.f36295p = this;
                        c0854a2.f36298s = 1;
                        obj = iForgotUsernameApi.forgotUsername2Request(a10, valueOf, str2, str3, str4, str, c0854a2);
                        if (obj == f10) {
                            return f10;
                        }
                    } else {
                        if (i10 != 1) {
                            throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                        }
                        this = (a) c0854a2.f36295p;
                        y.b(obj);
                    }
                    forgotUsernameResponse = (ForgotUsernameResponse) obj;
                    if (!this.d(forgotUsernameResponse)) {
                        this.f36294d = forgotUsernameResponse.attempts;
                        return new ResultWrapper.Error(null, this.c(forgotUsernameResponse), RepositoryError.ServiceError, null, 8, null);
                    }
                    String response = forgotUsernameResponse.response;
                    t.f(response, "response");
                    this.g(response);
                    this.f36294d = forgotUsernameResponse.attempts;
                    return new ResultWrapper.Success(forgotUsernameResponse);
                }
            }
            if (i10 != 0) {
            }
            forgotUsernameResponse = (ForgotUsernameResponse) obj;
            if (!this.d(forgotUsernameResponse)) {
            }
        } 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);
        }
        c0854a = new C0854a(dVar);
        C0854a c0854a22 = c0854a;
        Object obj2 = c0854a22.f36296q;
        f10 = wm.d.f();
        i10 = c0854a22.f36298s;
    }
}
