package com.revenuecat.purchases.common;

import H4.f;
import J4.c;
import R4.a;
import R4.k;
import R4.n;
import android.content.Context;
import com.revenuecat.purchases.utils.FileExtensionsKt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import kotlin.jvm.internal.o;
import kotlin.jvm.internal.p;
import w4.C3137A;

/* JADX INFO: loaded from: classes3.dex */
public final class FileHelper {
    private final Context applicationContext;

    /* JADX INFO: renamed from: com.revenuecat.purchases.common.FileHelper$readFilePerLines$1, reason: invalid class name */
    public static final class AnonymousClass1 extends p implements c {
        final /* synthetic */ c $block;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(c cVar) {
            super(1);
            this.$block = cVar;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((BufferedReader) obj);
            return C3137A.f25453a;
        }

        public final void invoke(BufferedReader bufferedReader) {
            o.h(bufferedReader, "bufferedReader");
            this.$block.invoke(new a(new f(bufferedReader, 0)));
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.common.FileHelper$removeFirstLinesFromFile$1, reason: invalid class name and case insensitive filesystem */
    public static final class C14061 extends p implements c {
        final /* synthetic */ int $numberOfLinesToRemove;
        final /* synthetic */ StringBuilder $textToAppend;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C14061(int i6, StringBuilder sb) {
            super(1);
            this.$numberOfLinesToRemove = i6;
            this.$textToAppend = sb;
        }

        @Override // J4.c
        public /* bridge */ /* synthetic */ Object invoke(Object obj) {
            invoke((k) obj);
            return C3137A.f25453a;
        }

        public final void invoke(k sequence) {
            o.h(sequence, "sequence");
            k kVarZ = n.Z(sequence, this.$numberOfLinesToRemove);
            StringBuilder sb = this.$textToAppend;
            Iterator it = kVarZ.iterator();
            while (it.hasNext()) {
                sb.append((String) it.next());
                sb.append("\n");
            }
        }
    }

    public FileHelper(Context applicationContext) {
        o.h(applicationContext, "applicationContext");
        this.applicationContext = applicationContext;
    }

    private final File getFileInFilesDir(String str) {
        return new File(getFilesDir(), str);
    }

    private final File getFilesDir() {
        File filesDir = this.applicationContext.getFilesDir();
        o.g(filesDir, "applicationContext.filesDir");
        return filesDir;
    }

    private final void openBufferedReader(String str, c cVar) throws IOException {
        FileInputStream fileInputStream = new FileInputStream(getFileInFilesDir(str));
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
            try {
                BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
                try {
                    cVar.invoke(bufferedReader);
                    bufferedReader.close();
                    inputStreamReader.close();
                    fileInputStream.close();
                } finally {
                }
            } finally {
            }
        } finally {
        }
    }

    public static /* synthetic */ void removeFirstLinesFromFile$default(FileHelper fileHelper, String str, int i6, c cVar, int i7, Object obj) {
        if ((i7 & 4) != 0) {
            cVar = null;
        }
        fileHelper.removeFirstLinesFromFile(str, i6, cVar);
    }

    public final void appendToFile(String filePath, String contentToAppend) throws IOException {
        o.h(filePath, "filePath");
        o.h(contentToAppend, "contentToAppend");
        File fileInFilesDir = getFileInFilesDir(filePath);
        File parentFile = fileInFilesDir.getParentFile();
        if (parentFile != null) {
            parentFile.mkdirs();
        }
        FileOutputStream fileOutputStream = new FileOutputStream(fileInFilesDir, true);
        try {
            byte[] bytes = contentToAppend.getBytes(S4.a.f6257a);
            o.g(bytes, "getBytes(...)");
            fileOutputStream.write(bytes);
            fileOutputStream.close();
        } finally {
        }
    }

    public final boolean deleteFile(String filePath) {
        o.h(filePath, "filePath");
        return getFileInFilesDir(filePath).delete();
    }

    public final boolean fileIsEmpty(String filePath) {
        o.h(filePath, "filePath");
        File fileInFilesDir = getFileInFilesDir(filePath);
        return !fileInFilesDir.exists() || fileInFilesDir.length() == 0;
    }

    public final double fileSizeInKB(String filePath) {
        o.h(filePath, "filePath");
        return FileExtensionsKt.getSizeInKB(getFileInFilesDir(filePath));
    }

    public final void readFilePerLines(String filePath, c block) throws IOException {
        o.h(filePath, "filePath");
        o.h(block, "block");
        openBufferedReader(filePath, new AnonymousClass1(block));
    }

    public final void removeFirstLinesFromFile(String filePath, int i6, c cVar) {
        o.h(filePath, "filePath");
        try {
            StringBuilder sb = new StringBuilder();
            readFilePerLines(filePath, new C14061(i6, sb));
            deleteFile(filePath);
            String string = sb.toString();
            o.g(string, "textToAppend.toString()");
            appendToFile(filePath, string);
        } catch (FileNotFoundException e6) {
            if (cVar != null) {
                cVar.invoke(e6);
            }
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", Z0.o.o("FileHelper: file not found when trying to remove first lines from file: ", filePath, ". Ignoring."), e6);
        } catch (Throwable th) {
            if (cVar != null) {
                cVar.invoke(th);
            }
            LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", Z0.o.o("FileHelper: error removing first lines from file: ", filePath, ". Ignoring."), th);
            throw th;
        }
    }
}
