package androidx.datastore.core;

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.concurrent.atomic.AtomicBoolean;
import kotlin.coroutines.Continuation;
import kotlin.coroutines.intrinsics.CoroutineSingletons;

/* JADX INFO: loaded from: classes.dex */
public class p implements x {

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

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

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

    public p(File file, z zVar) {
        nb.g.i(file, "file");
        nb.g.i(zVar, "serializer");
        this.f1945a = file;
        this.f1946b = zVar;
        this.f1947c = new AtomicBoolean(false);
    }

    /* JADX WARN: Multi-variable type inference failed */
    /* JADX WARN: Removed duplicated region for block: B:7:0x0013  */
    /* JADX WARN: Type inference failed for: r2v0, types: [int] */
    /* JADX WARN: Type inference failed for: r2v1 */
    /* JADX WARN: Type inference failed for: r2v11, types: [androidx.datastore.core.p] */
    /* JADX WARN: Type inference failed for: r2v5 */
    /* JADX WARN: Type inference failed for: r2v9 */
    /* JADX WARN: Type inference failed for: r7v0, types: [androidx.datastore.core.p, java.lang.Object] */
    /* JADX WARN: Type inference failed for: r7v1 */
    /* JADX WARN: Type inference failed for: r7v2, types: [androidx.datastore.core.p] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public static Object a(p pVar, Continuation continuation) throws IOException {
        FileReadScope$readData$1 fileReadScope$readData$1;
        Throwable th;
        Closeable closeable;
        Throwable th2;
        Closeable closeable2;
        if (continuation instanceof FileReadScope$readData$1) {
            fileReadScope$readData$1 = (FileReadScope$readData$1) continuation;
            int i10 = fileReadScope$readData$1.label;
            if ((i10 & Integer.MIN_VALUE) != 0) {
                fileReadScope$readData$1.label = i10 - Integer.MIN_VALUE;
            } else {
                fileReadScope$readData$1 = new FileReadScope$readData$1(pVar, continuation);
            }
        }
        Object obj = fileReadScope$readData$1.result;
        CoroutineSingletons coroutineSingletons = CoroutineSingletons.f29226a;
        ?? r22 = fileReadScope$readData$1.label;
        try {
        } catch (FileNotFoundException unused) {
            pVar = r22;
        }
        if (r22 != 0) {
            if (r22 == 1) {
                closeable2 = (Closeable) fileReadScope$readData$1.L$1;
                r22 = (p) fileReadScope$readData$1.L$0;
                try {
                    kotlin.a.d(obj);
                    ua.b.g(closeable2, null);
                    return obj;
                } catch (Throwable th3) {
                    th2 = th3;
                    try {
                        throw th2;
                    } finally {
                    }
                }
            }
            if (r22 != 2) {
                throw new IllegalStateException("call to 'resume' before 'invoke' with coroutine");
            }
            closeable = (Closeable) fileReadScope$readData$1.L$0;
            try {
                kotlin.a.d(obj);
                ua.b.g(closeable, null);
                return obj;
            } catch (Throwable th4) {
                th = th4;
                try {
                    throw th2;
                } finally {
                }
            }
        }
        kotlin.a.d(obj);
        if (!(!pVar.f1947c.get())) {
            throw new IllegalStateException("This scope has already been closed.".toString());
        }
        try {
            FileInputStream fileInputStream = new FileInputStream(pVar.f1945a);
            try {
                z zVar = pVar.f1946b;
                fileReadScope$readData$1.L$0 = pVar;
                fileReadScope$readData$1.L$1 = fileInputStream;
                fileReadScope$readData$1.label = 1;
                Object objB = zVar.b(fileInputStream);
                if (objB == coroutineSingletons) {
                    return coroutineSingletons;
                }
                closeable2 = fileInputStream;
                obj = objB;
                ua.b.g(closeable2, null);
                return obj;
            } catch (Throwable th5) {
                r22 = pVar;
                closeable2 = fileInputStream;
                th2 = th5;
                throw th2;
            }
        } catch (FileNotFoundException unused2) {
            boolean zExists = pVar.f1945a.exists();
            z zVar2 = pVar.f1946b;
            if (!zExists) {
                return zVar2.a();
            }
            FileInputStream fileInputStream2 = new FileInputStream(pVar.f1945a);
            try {
                fileReadScope$readData$1.L$0 = fileInputStream2;
                fileReadScope$readData$1.L$1 = null;
                fileReadScope$readData$1.label = 2;
                Object objB2 = zVar2.b(fileInputStream2);
                if (objB2 == coroutineSingletons) {
                    return coroutineSingletons;
                }
                obj = objB2;
                closeable = fileInputStream2;
                ua.b.g(closeable, null);
                return obj;
            } catch (Throwable th6) {
                th = th6;
                closeable = fileInputStream2;
                throw th2;
            }
        }
    }

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