package com.revenuecat.purchases.utils;

import J4.c;
import R4.e;
import R4.k;
import R4.n;
import com.revenuecat.purchases.DebugEvent;
import com.revenuecat.purchases.DebugEventName;
import com.revenuecat.purchases.LogHandler;
import com.revenuecat.purchases.LogLevel;
import com.revenuecat.purchases.common.Config;
import com.revenuecat.purchases.common.FileHelper;
import com.revenuecat.purchases.common.LogWrapperKt;
import com.revenuecat.purchases.utils.Event;
import kotlin.jvm.internal.D;
import kotlin.jvm.internal.h;
import kotlin.jvm.internal.o;
import kotlin.jvm.internal.p;
import org.json.JSONObject;
import w4.C3137A;
import x4.z;
import y4.C3277e;

/* JADX INFO: loaded from: classes3.dex */
public class EventsFileHelper<T extends Event> {
    public static final Companion Companion = new Companion(null);
    public static final int MAX_EVENT_PROPERTY_SIZE = 80;
    private c debugEventCallback;
    private final c eventDeserializer;
    private final c eventSerializer;
    private final FileHelper fileHelper;
    private final String filePath;

    public static final class Companion {
        public /* synthetic */ Companion(h hVar) {
            this();
        }

        private Companion() {
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.utils.EventsFileHelper$clear$1, reason: invalid class name */
    public static final class AnonymousClass1 extends p implements c {
        final /* synthetic */ EventsFileHelper<T> this$0;

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public AnonymousClass1(EventsFileHelper<T> eventsFileHelper) {
            super(1);
            this.this$0 = eventsFileHelper;
        }

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

        public final void invoke(Throwable e6) {
            o.h(e6, "e");
            c debugEventCallback = this.this$0.getDebugEventCallback();
            if (debugEventCallback != null) {
                DebugEventName debugEventName = DebugEventName.REMOVE_LINES_EXCEPTION;
                C3277e c3277e = new C3277e();
                String strC = D.a(e6.getClass()).c();
                if (strC == null) {
                    strC = "Unknown";
                }
                c3277e.put("exceptionType", strC);
                String message = e6.getMessage();
                if (message != null) {
                    c3277e.put("message", S4.p.m1(80, message));
                }
                debugEventCallback.invoke(new DebugEvent(debugEventName, c3277e.b()));
            }
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.utils.EventsFileHelper$readFile$1, reason: invalid class name and case insensitive filesystem */
    public static final class C18141 extends p implements c {
        final /* synthetic */ c $block;
        final /* synthetic */ EventsFileHelper<T> this$0;

        /* JADX INFO: renamed from: com.revenuecat.purchases.utils.EventsFileHelper$readFile$1$1, reason: invalid class name and collision with other inner class name */
        public static final class C00471 extends p implements c {
            final /* synthetic */ EventsFileHelper<T> this$0;

            /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
            public C00471(EventsFileHelper<T> eventsFileHelper) {
                super(1);
                this.this$0 = eventsFileHelper;
            }

            @Override // J4.c
            public final T invoke(String line) {
                o.h(line, "line");
                return (T) this.this$0.mapToEvent(line);
            }
        }

        /* JADX WARN: 'super' call moved to the top of the method (can break code semantics) */
        public C18141(c cVar, EventsFileHelper<T> eventsFileHelper) {
            super(1);
            this.$block = cVar;
            this.this$0 = eventsFileHelper;
        }

        @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");
            this.$block.invoke(n.b0(sequence, new C00471(this.this$0)));
        }
    }

    /* JADX INFO: renamed from: com.revenuecat.purchases.utils.EventsFileHelper$readFileAsJson$1, reason: invalid class name and case insensitive filesystem */
    public static final class C18151 extends p implements c {
        final /* synthetic */ c $block;

        /* JADX INFO: renamed from: com.revenuecat.purchases.utils.EventsFileHelper$readFileAsJson$1$1, reason: invalid class name and collision with other inner class name */
        public static final class C00481 extends p implements c {
            public static final C00481 INSTANCE = new C00481();

            public C00481() {
                super(1);
            }

            @Override // J4.c
            public final JSONObject invoke(String it) {
                o.h(it, "it");
                return new JSONObject(it);
            }
        }

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

        @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");
            this.$block.invoke(n.b0(sequence, C00481.INSTANCE));
        }
    }

    public EventsFileHelper(FileHelper fileHelper, String filePath, c cVar, c cVar2) {
        o.h(fileHelper, "fileHelper");
        o.h(filePath, "filePath");
        this.fileHelper = fileHelper;
        this.filePath = filePath;
        this.eventSerializer = cVar;
        this.eventDeserializer = cVar2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public final T mapToEvent(String str) {
        c cVar = this.eventDeserializer;
        if (cVar == null) {
            c cVar2 = this.debugEventCallback;
            if (cVar2 != null) {
                cVar2.invoke(new DebugEvent(DebugEventName.DESERIALIZATION_ERROR, z.f26066a));
                return null;
            }
        } else {
            try {
                return (T) cVar.invoke(str);
            } catch (SerializationException e6) {
                c cVar3 = this.debugEventCallback;
                if (cVar3 != null) {
                    DebugEventName debugEventName = DebugEventName.DESERIALIZATION_ERROR;
                    C3277e c3277e = new C3277e();
                    c3277e.put("exceptionType", "SerializationException");
                    String message = e6.getMessage();
                    if (message != null) {
                        c3277e.put("message", S4.p.m1(80, message));
                    }
                    cVar3.invoke(new DebugEvent(debugEventName, c3277e.b()));
                }
                LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error parsing event from file: " + str, e6);
            } catch (IllegalArgumentException e7) {
                c cVar4 = this.debugEventCallback;
                if (cVar4 != null) {
                    DebugEventName debugEventName2 = DebugEventName.DESERIALIZATION_ERROR;
                    C3277e c3277e2 = new C3277e();
                    c3277e2.put("exceptionType", "IllegalArgumentException");
                    String message2 = e7.getMessage();
                    if (message2 != null) {
                        c3277e2.put("message", S4.p.m1(80, message2));
                    }
                    cVar4.invoke(new DebugEvent(debugEventName2, c3277e2.b()));
                }
                LogWrapperKt.getCurrentLogHandler().e("[Purchases] - ERROR", "Error parsing event from file: " + str, e7);
            }
        }
        return null;
    }

    public final synchronized void appendEvent(T event) {
        String string;
        try {
            o.h(event, "event");
            try {
                FileHelper fileHelper = this.fileHelper;
                String str = this.filePath;
                StringBuilder sb = new StringBuilder();
                c cVar = this.eventSerializer;
                if (cVar == null || (string = (String) cVar.invoke(event)) == null) {
                    string = event.toString();
                }
                sb.append(string);
                sb.append('\n');
                fileHelper.appendToFile(str, sb.toString());
            } catch (Exception e6) {
                c cVar2 = this.debugEventCallback;
                if (cVar2 != null) {
                    DebugEventName debugEventName = DebugEventName.APPEND_EVENT_EXCEPTION;
                    C3277e c3277e = new C3277e();
                    String strC = D.a(e6.getClass()).c();
                    if (strC == null) {
                        strC = "Unknown";
                    }
                    c3277e.put("exceptionType", strC);
                    String message = e6.getMessage();
                    if (message != null) {
                        c3277e.put("message", S4.p.m1(80, message));
                    }
                    cVar2.invoke(new DebugEvent(debugEventName, c3277e.b()));
                }
                throw e6;
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void clear(int i6) {
        this.fileHelper.removeFirstLinesFromFile(this.filePath, i6, new AnonymousClass1(this));
    }

    public final synchronized void deleteFile() {
        if (!this.fileHelper.deleteFile(this.filePath)) {
            LogLevel logLevel = LogLevel.VERBOSE;
            LogHandler currentLogHandler = LogWrapperKt.getCurrentLogHandler();
            if (Config.INSTANCE.getLogLevel().compareTo(logLevel) <= 0) {
                currentLogHandler.v("[Purchases] - " + logLevel.name(), "Failed to delete events file in " + this.filePath + '.');
            }
        }
    }

    public final synchronized double fileSizeInKB() {
        return this.fileHelper.fileSizeInKB(this.filePath);
    }

    public final c getDebugEventCallback() {
        return this.debugEventCallback;
    }

    public final synchronized void readFile(c block) {
        try {
            o.h(block, "block");
            if (this.eventDeserializer == null || this.fileHelper.fileIsEmpty(this.filePath)) {
                block.invoke(e.f6091a);
            } else {
                this.fileHelper.readFilePerLines(this.filePath, new C18141(block, this));
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final synchronized void readFileAsJson(c block) {
        try {
            o.h(block, "block");
            if (this.fileHelper.fileIsEmpty(this.filePath)) {
                block.invoke(e.f6091a);
            } else {
                this.fileHelper.readFilePerLines(this.filePath, new C18151(block));
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    public final void setDebugEventCallback(c cVar) {
        this.debugEventCallback = cVar;
    }

    public /* synthetic */ EventsFileHelper(FileHelper fileHelper, String str, c cVar, c cVar2, int i6, h hVar) {
        this(fileHelper, str, (i6 & 4) != 0 ? null : cVar, (i6 & 8) != 0 ? null : cVar2);
    }
}
