package g9;

import ca.canadapost.core.data.auth.api.L1AuthApi;
import ca.canadapost.core.data.auth.model.Address;
import ca.canadapost.core.data.auth.model.BarCode;
import ca.canadapost.core.data.auth.model.CancelProofingRequest;
import ca.canadapost.core.data.auth.model.Name;
import ca.canadapost.core.data.auth.model.ProofingProfile;
import ca.canadapost.core.data.auth.model.ProofingRequest;
import ca.canadapost.core.data.auth.model.ProofingResponse;
import ca.canadapost.core.data.auth.model.ProofingState;
import ca.canadapost.core.data.auth.model.UserProfile;
import ca.canadapost.core.data.model.PhoneNumber;
import ca.canadapost.core.data.model.ResultWrapper;
import java.util.Date;
import rm.n0;

/* loaded from: classes.dex */
public final class o implements h {

    /* renamed from: m, reason: collision with root package name */
    public static final a f19209m = new a(null);

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

    /* renamed from: b, reason: collision with root package name */
    private final ka.b f19211b;

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

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

    /* renamed from: e, reason: collision with root package name */
    private final fa.a f19214e;

    /* renamed from: f, reason: collision with root package name */
    private final androidx.lifecycle.f0 f19215f;

    /* renamed from: g, reason: collision with root package name */
    private final androidx.lifecycle.a0 f19216g;

    /* renamed from: h, reason: collision with root package name */
    private int f19217h;

    /* renamed from: i, reason: collision with root package name */
    private final androidx.lifecycle.f0 f19218i;

    /* renamed from: j, reason: collision with root package name */
    private final androidx.lifecycle.f0 f19219j;

    /* renamed from: k, reason: collision with root package name */
    private final androidx.lifecycle.f0 f19220k;

    /* renamed from: l, reason: collision with root package name */
    private final androidx.lifecycle.f0 f19221l;

    /* loaded from: classes.dex */
    public static final class a {
        private a() {
        }

        public /* synthetic */ a(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class b extends kotlin.coroutines.jvm.internal.d {

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

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

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

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

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f19223q = obj;
            this.f19225s |= Integer.MIN_VALUE;
            return o.this.o(this);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class c extends kotlin.coroutines.jvm.internal.d {

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

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

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

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

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f19227q = obj;
            this.f19229s |= Integer.MIN_VALUE;
            return o.this.a(this);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public static final class d extends kotlin.coroutines.jvm.internal.d {

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

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

        /* renamed from: r, reason: collision with root package name */
        boolean f19232r;

        /* renamed from: s, reason: collision with root package name */
        /* synthetic */ Object f19233s;

        /* renamed from: u, reason: collision with root package name */
        int f19235u;

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

        @Override // kotlin.coroutines.jvm.internal.a
        public final Object invokeSuspend(Object obj) {
            this.f19233s = obj;
            this.f19235u |= Integer.MIN_VALUE;
            return o.this.s(null, null, null, null, false, this);
        }
    }

    public o(L1AuthApi api, ka.b userProfileRepository, g9.d sessionRepository, ca.a languageService, fa.a myMailRepository) {
        kotlin.jvm.internal.t.g(api, "api");
        kotlin.jvm.internal.t.g(userProfileRepository, "userProfileRepository");
        kotlin.jvm.internal.t.g(sessionRepository, "sessionRepository");
        kotlin.jvm.internal.t.g(languageService, "languageService");
        kotlin.jvm.internal.t.g(myMailRepository, "myMailRepository");
        this.f19210a = api;
        this.f19211b = userProfileRepository;
        this.f19212c = sessionRepository;
        this.f19213d = languageService;
        this.f19214e = myMailRepository;
        androidx.lifecycle.f0 f0Var = new androidx.lifecycle.f0();
        this.f19215f = f0Var;
        this.f19216g = f0Var;
        ProofingState proofingState = (ProofingState) c().f();
        this.f19217h = proofingState != null ? proofingState.getCancelledAttempts() : 0;
        this.f19218i = new androidx.lifecycle.f0();
        this.f19219j = new androidx.lifecycle.f0();
        this.f19220k = new androidx.lifecycle.f0();
        this.f19221l = new androidx.lifecycle.f0();
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:12:0x004b  */
    /* JADX WARN: Removed duplicated region for block: B:15:0x0064  */
    /* JADX WARN: Removed duplicated region for block: B:22:0x0035  */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0023  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object o(vm.d dVar) {
        b bVar;
        Object f10;
        int i10;
        ResultWrapper resultWrapper;
        if (dVar instanceof b) {
            bVar = (b) dVar;
            int i11 = bVar.f19225s;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                bVar.f19225s = i11 - Integer.MIN_VALUE;
                Object obj = bVar.f19223q;
                f10 = wm.d.f();
                i10 = bVar.f19225s;
                if (i10 != 0) {
                    rm.y.b(obj);
                    L1AuthApi l1AuthApi = this.f19210a;
                    bVar.f19222p = this;
                    bVar.f19225s = 1;
                    obj = l1AuthApi.getProofingStatus(bVar);
                    if (obj == f10) {
                        return f10;
                    }
                } else {
                    if (i10 != 1) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    this = (o) bVar.f19222p;
                    rm.y.b(obj);
                }
                resultWrapper = (ResultWrapper) obj;
                if (!(resultWrapper instanceof ResultWrapper.Success)) {
                    ResultWrapper.Success success = (ResultWrapper.Success) resultWrapper;
                    this.f19217h = ((ProofingState) success.getData()).getCancelledAttempts();
                    this.f19215f.q(success.getData());
                } else {
                    if (!(resultWrapper instanceof ResultWrapper.Error)) {
                        throw new rm.t();
                    }
                    this.f19215f.q(null);
                }
                return resultWrapper;
            }
        }
        bVar = new b(dVar);
        Object obj2 = bVar.f19223q;
        f10 = wm.d.f();
        i10 = bVar.f19225s;
        if (i10 != 0) {
        }
        resultWrapper = (ResultWrapper) obj2;
        if (!(resultWrapper instanceof ResultWrapper.Success)) {
        }
        return resultWrapper;
    }

    private final ka.a p() {
        String str;
        String str2;
        String str3;
        String str4;
        String str5;
        String country;
        UserProfile userProfile = (UserProfile) this.f19211b.a().f();
        if (userProfile == null || (str = userProfile.getAddressLine1()) == null) {
            str = "";
        }
        if (userProfile == null || (str2 = userProfile.getAddressLine2()) == null) {
            str2 = "";
        }
        if (userProfile == null || (str3 = userProfile.getCity()) == null) {
            str3 = "";
        }
        if (userProfile == null || (str4 = userProfile.getRegion()) == null) {
            str4 = "";
        }
        if (userProfile == null || (str5 = userProfile.getPostalZip()) == null) {
            str5 = "";
        }
        return new ka.a(str, str2, str3, str4, str5, (userProfile == null || (country = userProfile.getCountry()) == null) ? "" : country);
    }

    private final Name q() {
        UserProfile userProfile = (UserProfile) this.f19211b.a().f();
        return new Name(userProfile != null ? userProfile.getFirstName() : null, userProfile != null ? userProfile.getLastName() : null, null, null);
    }

    private final PhoneNumber r() {
        String mobilePhone;
        UserProfile userProfile = (UserProfile) this.f19211b.a().f();
        String mfaPhone = userProfile != null ? userProfile.getMfaPhone() : null;
        if (mfaPhone == null || mfaPhone.length() == 0) {
            String mobilePhone2 = userProfile != null ? userProfile.getMobilePhone() : null;
            if (!(mobilePhone2 == null || mobilePhone2.length() == 0) && userProfile != null) {
                mobilePhone = userProfile.getMobilePhone();
            }
            mobilePhone = null;
        } else {
            if (userProfile != null) {
                mobilePhone = userProfile.getMfaPhone();
            }
            mobilePhone = null;
        }
        return PhoneNumber.Companion.parsePhoneNumber$default(PhoneNumber.INSTANCE, mobilePhone, null, 2, null);
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* JADX WARN: Removed duplicated region for block: B:16:0x020d  */
    /* JADX WARN: Removed duplicated region for block: B:23:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0204 A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:31:0x0205  */
    /* JADX WARN: Removed duplicated region for block: B:35:0x018c A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:39:0x015b  */
    /* JADX WARN: Removed duplicated region for block: B:59:0x01f3  */
    /* JADX WARN: Removed duplicated region for block: B:62:0x0073  */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0031  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object s(Name name, ka.a aVar, PhoneNumber phoneNumber, Date date, boolean z10, vm.d dVar) {
        d dVar2;
        Object obj;
        Object f10;
        int i10;
        ResultWrapper resultWrapper;
        String str;
        o oVar;
        ResultWrapper resultWrapper2;
        ka.b bVar;
        ResultWrapper resultWrapper3;
        ResultWrapper resultWrapper4;
        o oVar2 = this;
        boolean z11 = z10;
        if (dVar instanceof d) {
            dVar2 = (d) dVar;
            int i11 = dVar2.f19235u;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                dVar2.f19235u = i11 - Integer.MIN_VALUE;
                obj = dVar2.f19233s;
                f10 = wm.d.f();
                i10 = dVar2.f19235u;
                if (i10 != 0) {
                    rm.y.b(obj);
                    Address address = new Address(aVar.c(), aVar.d(), aVar.a(), aVar.b(), "Civic", aVar.e(), aVar.f());
                    String normalizePhoneNumber = phoneNumber != null ? PhoneNumber.INSTANCE.normalizePhoneNumber(phoneNumber.toString()) : null;
                    ea.a aVar2 = date != null ? new ea.a(date) : null;
                    String c10 = oVar2.f19213d.c();
                    UserProfile userProfile = (UserProfile) oVar2.f19211b.a().f();
                    String email = userProfile != null ? userProfile.getEmail() : null;
                    String str2 = kotlin.jvm.internal.t.b(oVar2.f19211b.c().f(), kotlin.coroutines.jvm.internal.b.a(true)) ? "AcctQR" : kotlin.jvm.internal.t.b(oVar2.f19214e.s().f(), kotlin.coroutines.jvm.internal.b.a(true)) ? "MailManager" : kotlin.jvm.internal.t.b(oVar2.f19211b.G().f(), kotlin.coroutines.jvm.internal.b.a(true)) ? "SNF" : "FlexDelivery";
                    kotlin.jvm.internal.t.d(c10);
                    ProofingRequest proofingRequest = new ProofingRequest(name, address, normalizePhoneNumber, aVar2, c10, email, "MobileApp", str2, null, null, null, null, null, null, 16128, null);
                    L1AuthApi l1AuthApi = oVar2.f19210a;
                    boolean z12 = e() == 2 || z11;
                    dVar2.f19230p = oVar2;
                    dVar2.f19232r = z11;
                    dVar2.f19235u = 1;
                    obj = l1AuthApi.proofing(proofingRequest, z12, dVar2);
                    if (obj == f10) {
                        return f10;
                    }
                } else if (i10 == 1) {
                    boolean z13 = dVar2.f19232r;
                    o oVar3 = (o) dVar2.f19230p;
                    rm.y.b(obj);
                    z11 = z13;
                    oVar2 = oVar3;
                } else if (i10 == 2) {
                    resultWrapper2 = (ResultWrapper) dVar2.f19231q;
                    oVar = (o) dVar2.f19230p;
                    rm.y.b(obj);
                    bVar = oVar.f19211b;
                    dVar2.f19230p = oVar;
                    dVar2.f19231q = resultWrapper2;
                    dVar2.f19235u = 3;
                    if (bVar.m(true, dVar2) == f10) {
                        return f10;
                    }
                    o oVar4 = oVar;
                    resultWrapper = resultWrapper2;
                    oVar2 = oVar4;
                    dVar2.f19230p = resultWrapper;
                    dVar2.f19231q = null;
                    dVar2.f19235u = 5;
                    obj = oVar2.o(dVar2);
                    if (obj != f10) {
                    }
                } else {
                    if (i10 == 3 || i10 == 4) {
                        resultWrapper2 = (ResultWrapper) dVar2.f19231q;
                        oVar = (o) dVar2.f19230p;
                        rm.y.b(obj);
                        o oVar42 = oVar;
                        resultWrapper = resultWrapper2;
                        oVar2 = oVar42;
                        dVar2.f19230p = resultWrapper;
                        dVar2.f19231q = null;
                        dVar2.f19235u = 5;
                        obj = oVar2.o(dVar2);
                        if (obj != f10) {
                            return f10;
                        }
                        resultWrapper3 = resultWrapper;
                        resultWrapper4 = (ResultWrapper) obj;
                        if (!(resultWrapper4 instanceof ResultWrapper.Success)) {
                            return resultWrapper3;
                        }
                        if (resultWrapper4 instanceof ResultWrapper.Error) {
                            return resultWrapper4;
                        }
                        throw new rm.t();
                    }
                    if (i10 != 5) {
                        throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                    }
                    resultWrapper3 = (ResultWrapper) dVar2.f19230p;
                    rm.y.b(obj);
                    resultWrapper4 = (ResultWrapper) obj;
                    if (!(resultWrapper4 instanceof ResultWrapper.Success)) {
                    }
                }
                resultWrapper = (ResultWrapper) obj;
                if (!(resultWrapper instanceof ResultWrapper.Success)) {
                    ResultWrapper.Success success = (ResultWrapper.Success) resultWrapper;
                    if (((ProofingResponse) success.getData()).isActive()) {
                        ka.b bVar2 = oVar2.f19211b;
                        dVar2.f19230p = oVar2;
                        dVar2.f19231q = resultWrapper;
                        dVar2.f19235u = 2;
                        if (bVar2.D(dVar2) == f10) {
                            return f10;
                        }
                        oVar = oVar2;
                        resultWrapper2 = resultWrapper;
                        bVar = oVar.f19211b;
                        dVar2.f19230p = oVar;
                        dVar2.f19231q = resultWrapper2;
                        dVar2.f19235u = 3;
                        if (bVar.m(true, dVar2) == f10) {
                        }
                        o oVar422 = oVar;
                        resultWrapper = resultWrapper2;
                        oVar2 = oVar422;
                    } else if (kotlin.jvm.internal.t.b(((ProofingResponse) success.getData()).getIppTechnicalIssue(), kotlin.coroutines.jvm.internal.b.a(false)) && ((ProofingResponse) success.getData()).isIncomplete() && !z11) {
                        L1AuthApi l1AuthApi2 = oVar2.f19210a;
                        BarCode barCode = ((ProofingResponse) success.getData()).getBarCode();
                        if (barCode == null || (str = barCode.getBarCodeNbr()) == null) {
                            str = "";
                        }
                        CancelProofingRequest cancelProofingRequest = new CancelProofingRequest(str, false, false, 6, null);
                        dVar2.f19230p = oVar2;
                        dVar2.f19231q = resultWrapper;
                        dVar2.f19235u = 4;
                        if (l1AuthApi2.cancelProofing(cancelProofingRequest, dVar2) == f10) {
                            return f10;
                        }
                        oVar = oVar2;
                        resultWrapper2 = resultWrapper;
                        o oVar4222 = oVar;
                        resultWrapper = resultWrapper2;
                        oVar2 = oVar4222;
                    }
                } else if (resultWrapper instanceof ResultWrapper.Error) {
                    return resultWrapper;
                }
                dVar2.f19230p = resultWrapper;
                dVar2.f19231q = null;
                dVar2.f19235u = 5;
                obj = oVar2.o(dVar2);
                if (obj != f10) {
                }
            }
        }
        dVar2 = new d(dVar);
        obj = dVar2.f19233s;
        f10 = wm.d.f();
        i10 = dVar2.f19235u;
        if (i10 != 0) {
        }
        resultWrapper = (ResultWrapper) obj;
        if (!(resultWrapper instanceof ResultWrapper.Success)) {
        }
        dVar2.f19230p = resultWrapper;
        dVar2.f19231q = null;
        dVar2.f19235u = 5;
        obj = oVar2.o(dVar2);
        if (obj != f10) {
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:20:0x00cf A[RETURN] */
    /* JADX WARN: Removed duplicated region for block: B:24:0x008a  */
    /* JADX WARN: Removed duplicated region for block: B:26:0x008d  */
    /* JADX WARN: Removed duplicated region for block: B:42:0x004a  */
    /* JADX WARN: Removed duplicated region for block: B:8:0x0026  */
    @Override // g9.h
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public Object a(vm.d dVar) {
        c cVar;
        Object f10;
        int i10;
        ProofingState proofingState;
        String str;
        BarCode barCode;
        if (dVar instanceof c) {
            cVar = (c) dVar;
            int i11 = cVar.f19229s;
            if ((i11 & Integer.MIN_VALUE) != 0) {
                cVar.f19229s = i11 - Integer.MIN_VALUE;
                Object obj = cVar.f19227q;
                f10 = wm.d.f();
                i10 = cVar.f19229s;
                if (i10 != 0) {
                    rm.y.b(obj);
                    cVar.f19226p = this;
                    cVar.f19229s = 1;
                    if (k(cVar) == f10) {
                        return f10;
                    }
                } else {
                    if (i10 != 1) {
                        if (i10 != 2) {
                            if (i10 != 3) {
                                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                            }
                            rm.y.b(obj);
                            return n0.f34904a;
                        }
                        this = (o) cVar.f19226p;
                        rm.y.b(obj);
                        cVar.f19226p = null;
                        cVar.f19229s = 3;
                        if (this.o(cVar) == f10) {
                            return f10;
                        }
                        return n0.f34904a;
                    }
                    this = (o) cVar.f19226p;
                    rm.y.b(obj);
                }
                this.d().q(this.q());
                this.i().q(this.p());
                this.b().q(this.r());
                this.f().q(null);
                proofingState = (ProofingState) this.f19215f.f();
                if (proofingState != null) {
                    return n0.f34904a;
                }
                if (proofingState.getIncompleteProofingProfile() == null || proofingState.isAccountLocked()) {
                    return n0.f34904a;
                }
                L1AuthApi l1AuthApi = this.f19210a;
                ProofingProfile incompleteProofingProfile = proofingState.getIncompleteProofingProfile();
                if (incompleteProofingProfile == null || (barCode = incompleteProofingProfile.getBarCode()) == null || (str = barCode.getBarCodeNbr()) == null) {
                    str = "";
                }
                CancelProofingRequest cancelProofingRequest = new CancelProofingRequest(str, false, false, 6, null);
                cVar.f19226p = this;
                cVar.f19229s = 2;
                if (l1AuthApi.cancelProofing(cancelProofingRequest, cVar) == f10) {
                    return f10;
                }
                cVar.f19226p = null;
                cVar.f19229s = 3;
                if (this.o(cVar) == f10) {
                }
                return n0.f34904a;
            }
        }
        cVar = new c(dVar);
        Object obj2 = cVar.f19227q;
        f10 = wm.d.f();
        i10 = cVar.f19229s;
        if (i10 != 0) {
        }
        this.d().q(this.q());
        this.i().q(this.p());
        this.b().q(this.r());
        this.f().q(null);
        proofingState = (ProofingState) this.f19215f.f();
        if (proofingState != null) {
        }
    }

    @Override // g9.h
    public androidx.lifecycle.f0 b() {
        return this.f19220k;
    }

    @Override // g9.h
    public androidx.lifecycle.a0 c() {
        return this.f19216g;
    }

    @Override // g9.h
    public androidx.lifecycle.f0 d() {
        return this.f19218i;
    }

    @Override // g9.h
    public int e() {
        return this.f19217h;
    }

    @Override // g9.h
    public androidx.lifecycle.f0 f() {
        return this.f19221l;
    }

    @Override // g9.h
    public void g() {
        this.f19215f.q(null);
    }

    @Override // g9.h
    public Object h(Name name, ka.a aVar, PhoneNumber phoneNumber, Date date, vm.d dVar) {
        return s(name, aVar, phoneNumber, date, false, dVar);
    }

    @Override // g9.h
    public androidx.lifecycle.f0 i() {
        return this.f19219j;
    }

    @Override // g9.h
    public Object j(Name name, ka.a aVar, vm.d dVar) {
        return s(name, aVar, PhoneNumber.Companion.parsePhoneNumber$default(PhoneNumber.INSTANCE, "1111111111", null, 2, null), null, true, dVar);
    }

    @Override // g9.h
    public Object k(vm.d dVar) {
        Object f10;
        if (!this.f19212c.h() || this.f19215f.f() != null) {
            return n0.f34904a;
        }
        Object o10 = o(dVar);
        f10 = wm.d.f();
        return o10 == f10 ? o10 : n0.f34904a;
    }

    @Override // g9.h
    public Object l(vm.d dVar) {
        if (this.f19212c.h()) {
            return o(dVar);
        }
        return null;
    }
}
