package androidx.datastore.core;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;
import kotlin.jvm.functions.Function2;

/* JADX INFO: loaded from: classes.dex */
public final class r implements e0 {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final File f1953a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final z f1954b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final u f1955c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final sd.a f1956d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final AtomicBoolean f1957e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final kotlinx.coroutines.sync.c f1958f;

    public r(File file, z zVar, u uVar, sd.a aVar) {
        nb.g.i(zVar, "serializer");
        nb.g.i(uVar, "coordinator");
        this.f1953a = file;
        this.f1954b = zVar;
        this.f1955c = uVar;
        this.f1956d = aVar;
        this.f1957e = new AtomicBoolean(false);
        this.f1958f = kotlinx.coroutines.sync.d.a();
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0079  */
    /* JADX WARN: Removed duplicated region for block: B:33:0x0081 A[Catch: all -> 0x0082, TRY_ENTER, TRY_LEAVE, TryCatch #1 {all -> 0x0082, blocks: (B:33:0x0081, B:43:0x0094, B:42:0x0091, B:39:0x008c), top: B:53:0x0020, inners: #0 }] */
    /* JADX WARN: Removed duplicated region for block: B:47:0x009c  */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /* JADX WARN: Type inference failed for: r0v11, types: [androidx.datastore.core.r] */
    /* JADX WARN: Type inference failed for: r0v14 */
    /* JADX WARN: Type inference failed for: r0v15 */
    /* JADX WARN: Type inference failed for: r0v16 */
    /* JADX WARN: Type inference failed for: r0v2, types: [androidx.datastore.core.FileStorageConnection$readScope$1, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r0v3 */
    /* JADX WARN: Type inference failed for: r0v4, types: [androidx.datastore.core.r] */
    /* JADX WARN: Type inference failed for: r0v5 */
    /* JADX WARN: Type inference failed for: r0v6 */
    /* JADX WARN: Type inference failed for: r0v7 */
    /* JADX WARN: Type inference failed for: r0v8 */
    /* JADX WARN: Type inference failed for: r0v9, types: [androidx.datastore.core.r] */
    /* JADX WARN: Type inference failed for: r9v0, types: [sd.p] */
    /* JADX WARN: Type inference failed for: r9v1 */
    /* JADX WARN: Type inference failed for: r9v11 */
    /* JADX WARN: Type inference failed for: r9v12 */
    /* JADX WARN: Type inference failed for: r9v13 */
    /* JADX WARN: Type inference failed for: r9v16, types: [boolean] */
    /* JADX WARN: Type inference failed for: r9v17 */
    /* JADX WARN: Type inference failed for: r9v2 */
    /* JADX WARN: Type inference failed for: r9v6 */
    /* JADX WARN: Type inference failed for: r9v8 */
    /* JADX WARN: Type inference failed for: r9v9, types: [androidx.datastore.core.StorageConnectionKt$readData$2] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object a(sd.p pVar, Continuation continuation) throws Throwable {
        ?? fileStorageConnection$readScope$1;
        b bVar;
        Throwable th;
        ?? r02;
        ?? r92;
        if (continuation instanceof FileStorageConnection$readScope$1) {
            FileStorageConnection$readScope$1 fileStorageConnection$readScope$12 = (FileStorageConnection$readScope$1) continuation;
            int i10 = fileStorageConnection$readScope$12.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                fileStorageConnection$readScope$12.label = i10 - Integer.MIN_VALUE;
                fileStorageConnection$readScope$1 = fileStorageConnection$readScope$12;
            } else {
                fileStorageConnection$readScope$1 = new FileStorageConnection$readScope$1(this, continuation);
            }
        }
        Object obj = fileStorageConnection$readScope$1.result;
        CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
        int i11 = fileStorageConnection$readScope$1.label;
        try {
            if (i11 != 0) {
                if (i11 != 1) {
                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                }
                pVar = fileStorageConnection$readScope$1.Z$0;
                bVar = (b) fileStorageConnection$readScope$1.L$1;
                fileStorageConnection$readScope$1 = (r) fileStorageConnection$readScope$1.L$0;
                try {
                    kotlin.a.d(obj);
                    r02 = fileStorageConnection$readScope$1;
                    r92 = pVar;
                    try {
                        bVar.close();
                        th = null;
                    } catch (Throwable th2) {
                        th = th2;
                    }
                    if (th == null) {
                        throw th;
                    }
                    if (r92 != 0) {
                        r02.f1958f.g(null);
                    }
                    return obj;
                } catch (Throwable th3) {
                    th = th3;
                    try {
                        bVar.close();
                    } catch (Throwable th4) {
                        kotlin.a.a(th, th4);
                    }
                    throw th;
                }
            }
            kotlin.a.d(obj);
            if (!(!this.f1957e.get())) {
                throw new IllegalStateException("StorageConnection has already been disposed.".toString());
            }
            boolean zC = this.f1958f.c(null);
            try {
                p pVar2 = new p(this.f1953a, this.f1954b);
                try {
                    Boolean boolValueOf = Boolean.valueOf(zC);
                    fileStorageConnection$readScope$1.L$0 = this;
                    fileStorageConnection$readScope$1.L$1 = pVar2;
                    fileStorageConnection$readScope$1.Z$0 = zC;
                    fileStorageConnection$readScope$1.label = 1;
                    Object objA = ((StorageConnectionKt$readData$2) pVar).a(pVar2, boolValueOf, fileStorageConnection$readScope$1);
                    if (objA == coroutineSingletons) {
                        return coroutineSingletons;
                    }
                    r02 = this;
                    bVar = pVar2;
                    obj = objA;
                    r92 = zC;
                    bVar.close();
                    th = null;
                    if (th == null) {
                    }
                } catch (Throwable th5) {
                    fileStorageConnection$readScope$1 = this;
                    bVar = pVar2;
                    th = th5;
                    pVar = zC;
                    bVar.close();
                    throw th;
                }
            } catch (Throwable th6) {
                fileStorageConnection$readScope$1 = this;
                th = th6;
                pVar = zC;
                if (pVar != 0) {
                }
                throw th;
            }
        } catch (Throwable th7) {
            th = th7;
            if (pVar != 0) {
                fileStorageConnection$readScope$1.f1958f.g(null);
            }
            throw th;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:43:0x00e9 A[Catch: all -> 0x0119, IOException -> 0x011b, TRY_ENTER, TryCatch #3 {all -> 0x0119, blocks: (B:43:0x00e9, B:45:0x00ef, B:48:0x00f8, B:49:0x0118, B:56:0x0124, B:65:0x0133, B:67:0x0139, B:68:0x013c, B:63:0x0131, B:62:0x012e, B:33:0x00a5, B:34:0x00c1), top: B:79:0x00a5 }] */
    /* JADX WARN: Removed duplicated region for block: B:56:0x0124 A[Catch: all -> 0x0119, IOException -> 0x011b, TRY_ENTER, TRY_LEAVE, TryCatch #3 {all -> 0x0119, blocks: (B:43:0x00e9, B:45:0x00ef, B:48:0x00f8, B:49:0x0118, B:56:0x0124, B:65:0x0133, B:67:0x0139, B:68:0x013c, B:63:0x0131, B:62:0x012e, B:33:0x00a5, B:34:0x00c1), top: B:79:0x00a5 }] */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0015  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Object b(Function2 function2, Continuation continuation) throws IOException {
        FileStorageConnection$writeScope$1 fileStorageConnection$writeScope$1;
        File file;
        r rVar;
        kotlinx.coroutines.sync.a aVar;
        s sVar;
        Throwable th;
        b bVar;
        File file2;
        r rVar2;
        if (continuation instanceof FileStorageConnection$writeScope$1) {
            fileStorageConnection$writeScope$1 = (FileStorageConnection$writeScope$1) continuation;
            int i10 = fileStorageConnection$writeScope$1.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                fileStorageConnection$writeScope$1.label = i10 - Integer.MIN_VALUE;
            } else {
                fileStorageConnection$writeScope$1 = new FileStorageConnection$writeScope$1(this, continuation);
            }
        }
        Object obj = fileStorageConnection$writeScope$1.result;
        CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
        int i11 = fileStorageConnection$writeScope$1.label;
        try {
            try {
                try {
                    try {
                        if (i11 == 0) {
                            kotlin.a.d(obj);
                            if (!(!this.f1957e.get())) {
                                throw new IllegalStateException("StorageConnection has already been disposed.".toString());
                            }
                            File file3 = this.f1953a;
                            File parentFile = file3.getCanonicalFile().getParentFile();
                            if (parentFile != null) {
                                parentFile.mkdirs();
                                if (!parentFile.isDirectory()) {
                                    throw new IOException("Unable to create parent directories of " + file3);
                                }
                            }
                            fileStorageConnection$writeScope$1.L$0 = this;
                            fileStorageConnection$writeScope$1.L$1 = function2;
                            kotlinx.coroutines.sync.c cVar = this.f1958f;
                            fileStorageConnection$writeScope$1.L$2 = cVar;
                            fileStorageConnection$writeScope$1.label = 1;
                            if (cVar.e(fileStorageConnection$writeScope$1) == coroutineSingletons) {
                                return coroutineSingletons;
                            }
                            rVar = this;
                            aVar = cVar;
                        } else {
                            if (i11 != 1) {
                                if (i11 != 2) {
                                    throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
                                }
                                bVar = (b) fileStorageConnection$writeScope$1.L$3;
                                File file4 = (File) fileStorageConnection$writeScope$1.L$2;
                                aVar = (kotlinx.coroutines.sync.a) fileStorageConnection$writeScope$1.L$1;
                                rVar2 = (r) fileStorageConnection$writeScope$1.L$0;
                                try {
                                    kotlin.a.d(obj);
                                    file2 = file4;
                                    try {
                                        bVar.close();
                                        th = null;
                                    } catch (Throwable th2) {
                                        th = th2;
                                    }
                                    if (th == null) {
                                        throw th;
                                    }
                                    if (file2.exists() && !o6.b.f(file2, rVar2.f1953a)) {
                                        throw new IOException("Unable to rename " + file2 + " to " + rVar2.f1953a + ". This likely means that there are multiple instances of DataStore for this file. Ensure that you are only creating a single instance of datastore for this file.");
                                    }
                                    aVar.g(null);
                                    return kd.e.f29109a;
                                } catch (Throwable th3) {
                                    th = th3;
                                    try {
                                        bVar.close();
                                    } catch (Throwable th4) {
                                        kotlin.a.a(th, th4);
                                    }
                                    throw th;
                                }
                            }
                            kotlinx.coroutines.sync.a aVar2 = (kotlinx.coroutines.sync.a) fileStorageConnection$writeScope$1.L$2;
                            Function2 function22 = (Function2) fileStorageConnection$writeScope$1.L$1;
                            rVar = (r) fileStorageConnection$writeScope$1.L$0;
                            kotlin.a.d(obj);
                            aVar = aVar2;
                            function2 = function22;
                        }
                        fileStorageConnection$writeScope$1.L$0 = rVar;
                        fileStorageConnection$writeScope$1.L$1 = aVar;
                        fileStorageConnection$writeScope$1.L$2 = file;
                        fileStorageConnection$writeScope$1.L$3 = sVar;
                        fileStorageConnection$writeScope$1.label = 2;
                        if (function2.invoke(sVar, fileStorageConnection$writeScope$1) == coroutineSingletons) {
                            return coroutineSingletons;
                        }
                        file2 = file;
                        rVar2 = rVar;
                        bVar = sVar;
                        bVar.close();
                        th = null;
                        if (th == null) {
                        }
                    } catch (Throwable th5) {
                        th = th5;
                        bVar = sVar;
                        bVar.close();
                        throw th;
                    }
                    z zVar = rVar.f1954b;
                    nb.g.i(zVar, "serializer");
                    sVar = new s(file, zVar);
                } catch (IOException e10) {
                    e = e10;
                    if (file.exists()) {
                        file.delete();
                    }
                    throw e;
                }
                file = new File(rVar.f1953a.getAbsolutePath() + ".tmp");
            } catch (Throwable th6) {
                aVar.g(null);
                throw th6;
            }
        } catch (IOException e11) {
            e = e11;
            file = coroutineSingletons;
        }
    }

    @Override // androidx.datastore.core.b
    public final void close() {
        this.f1957e.set(true);
        this.f1956d.invoke();
    }
}
