package com.discord.stores; import c.d.b.a.a; import com.discord.api.message.MessageReference; import com.discord.models.domain.ModelMessageDelete; import com.discord.models.message.Message; import com.discord.stores.updates.ObservationDeck; import com.discord.stores.updates.ObservationDeckProvider; import d0.d0.f; import d0.t.g0; import d0.t.h0; import d0.t.m0; import d0.t.n0; import d0.t.o; import d0.t.u; import d0.z.d.m; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import kotlin.jvm.internal.DefaultConstructorMarker; import rx.Observable; /* compiled from: StoreMessageReplies.kt */ public final class StoreMessageReplies extends StoreV2 { public static final Companion Companion = new Companion(null); private static final Map NO_RESULTS = h0.emptyMap(); private final HashMap> channelMap; private final Dispatcher dispatcher; private final ObservationDeck observationDeck; private Map> repliedChannelMessagesCacheSnapshot; private final MessageCache repliedMessagesCache; private Map repliedMessagesCacheSnapshot; private final StoreMessages storeMessages; /* compiled from: StoreMessageReplies.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } } /* compiled from: StoreMessageReplies.kt */ public static final class MessageCache extends LinkedHashMap { @Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap public final /* bridge */ Set> entrySet() { return getEntries(); } public /* bridge */ Set getEntries() { return super.entrySet(); } public /* bridge */ Set getKeys() { return super.keySet(); } public /* bridge */ int getSize() { return super.size(); } public /* bridge */ Collection getValues() { return super.values(); } @Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap public final /* bridge */ Set keySet() { return getKeys(); } @Override // java.util.LinkedHashMap public boolean removeEldestEntry(Map.Entry entry) { return size() > 1000; } @Override // java.util.AbstractMap, java.util.Map, java.util.HashMap public final /* bridge */ int size() { return getSize(); } @Override // java.util.LinkedHashMap, java.util.AbstractMap, java.util.Map, java.util.HashMap public final /* bridge */ Collection values() { return getValues(); } } /* compiled from: StoreMessageReplies.kt */ public static abstract class MessageState { /* compiled from: StoreMessageReplies.kt */ public static final class Deleted extends MessageState { public static final Deleted INSTANCE = new Deleted(); private Deleted() { super(null); } } /* compiled from: StoreMessageReplies.kt */ public static final class Loaded extends MessageState { private final Message message; /* JADX INFO: super call moved to the top of the method (can break code semantics) */ public Loaded(Message message) { super(null); m.checkNotNullParameter(message, "message"); this.message = message; } public static /* synthetic */ Loaded copy$default(Loaded loaded, Message message, int i, Object obj) { if ((i & 1) != 0) { message = loaded.message; } return loaded.copy(message); } public final Message component1() { return this.message; } public final Loaded copy(Message message) { m.checkNotNullParameter(message, "message"); return new Loaded(message); } public boolean equals(Object obj) { if (this != obj) { return (obj instanceof Loaded) && m.areEqual(this.message, ((Loaded) obj).message); } return true; } public final Message getMessage() { return this.message; } public int hashCode() { Message message = this.message; if (message != null) { return message.hashCode(); } return 0; } public String toString() { StringBuilder L = a.L("Loaded(message="); L.append(this.message); L.append(")"); return L.toString(); } } /* compiled from: StoreMessageReplies.kt */ public static final class Unloaded extends MessageState { public static final Unloaded INSTANCE = new Unloaded(); private Unloaded() { super(null); } } private MessageState() { } public /* synthetic */ MessageState(DefaultConstructorMarker defaultConstructorMarker) { this(); } } public StoreMessageReplies(Dispatcher dispatcher, StoreMessages storeMessages, ObservationDeck observationDeck) { m.checkNotNullParameter(dispatcher, "dispatcher"); m.checkNotNullParameter(storeMessages, "storeMessages"); m.checkNotNullParameter(observationDeck, "observationDeck"); this.dispatcher = dispatcher; this.storeMessages = storeMessages; this.observationDeck = observationDeck; this.repliedMessagesCache = new MessageCache<>(); this.channelMap = new HashMap<>(); this.repliedChannelMessagesCacheSnapshot = h0.emptyMap(); this.repliedMessagesCacheSnapshot = h0.emptyMap(); } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ StoreMessageReplies(Dispatcher dispatcher, StoreMessages storeMessages, ObservationDeck observationDeck, int i, DefaultConstructorMarker defaultConstructorMarker) { this(dispatcher, storeMessages, (i & 4) != 0 ? ObservationDeckProvider.get() : observationDeck); } public static final /* synthetic */ Map access$getCachedChannelMessages(StoreMessageReplies storeMessageReplies, long j) { return storeMessageReplies.getCachedChannelMessages(j); } @StoreThread private final boolean deleteMessage(long j, long j2) { if (!this.repliedMessagesCache.containsKey(Long.valueOf(j))) { return false; } updateCache(j, j2, MessageState.Deleted.INSTANCE); return true; } private final Map getCachedChannelMessages(long j) { Map map = (Map) this.repliedChannelMessagesCacheSnapshot.get(Long.valueOf(j)); return map != null ? map : NO_RESULTS; } @StoreThread private final boolean processMessage(com.discord.api.message.Message message) { return processMessage(new Message(message)); } @StoreThread private final boolean processMessage(Message message) { boolean z2; if (this.repliedMessagesCache.containsKey(Long.valueOf(message.getId()))) { updateCache(message.getId(), message.getChannelId(), new MessageState.Loaded(message)); z2 = true; } else { z2 = false; } if (!message.isEmbeddedMessageType()) { return z2; } MessageReference messageReference = message.getMessageReference(); if (messageReference == null) { return z2; } com.discord.api.message.Message referencedMessage = message.getReferencedMessage(); Long a = messageReference.a(); Long c2 = messageReference.c(); if (referencedMessage != null) { processReferencedMessage(new Message(referencedMessage)); return true; } else if (a == null || c2 == null) { return true; } else { Message message2 = this.storeMessages.getMessage(a.longValue(), c2.longValue()); if (message2 != null) { processReferencedMessage(message2); return true; } updateCache(c2.longValue(), a.longValue(), MessageState.Unloaded.INSTANCE); return true; } } private final void processReferencedMessage(Message message) { updateCache(message.getId(), message.getChannelId(), new MessageState.Loaded(message)); Integer type = message.getType(); if (type != null && type.intValue() == 19) { processMessage(message); } } private final void snapShotAllMessages() { this.repliedMessagesCacheSnapshot = new HashMap(this.repliedMessagesCache); } @StoreThread private final void snapShotChannelMessages() { HashMap> hashMap = this.channelMap; LinkedHashMap linkedHashMap = new LinkedHashMap(g0.mapCapacity(hashMap.size())); Iterator it = hashMap.entrySet().iterator(); while (it.hasNext()) { Map.Entry entry = (Map.Entry) it.next(); Object key = entry.getKey(); Set set = (Set) entry.getValue(); LinkedHashMap linkedHashMap2 = new LinkedHashMap(f.coerceAtLeast(g0.mapCapacity(o.collectionSizeOrDefault(set, 10)), 16)); for (Object obj : set) { Object obj2 = (MessageState) this.repliedMessagesCache.get(Long.valueOf(((Number) obj).longValue())); if (obj2 == null) { obj2 = MessageState.Unloaded.INSTANCE; } linkedHashMap2.put(obj, obj2); } linkedHashMap.put(key, linkedHashMap2); } this.repliedChannelMessagesCacheSnapshot = linkedHashMap; } @StoreThread private final void updateCache(long j, long j2, MessageState messageState) { this.repliedMessagesCache.put(Long.valueOf(j), messageState); HashMap> hashMap = this.channelMap; Long valueOf = Long.valueOf(j2); Set of = m0.setOf(Long.valueOf(j)); Set set = this.channelMap.get(Long.valueOf(j2)); if (set == null) { set = n0.emptySet(); } hashMap.put(valueOf, u.union(of, set)); } /* JADX DEBUG: Type inference failed for r0v0. Raw type applied. Possible types: java.util.Map, java.util.Map */ public final Map getAllMessageReferences() { return this.repliedMessagesCacheSnapshot; } @StoreThread public final void handleLoadMessages(Collection collection) { m.checkNotNullParameter(collection, "messages"); ArrayList arrayList = new ArrayList(o.collectionSizeOrDefault(collection, 10)); for (Message message : collection) { arrayList.add(Boolean.valueOf(processMessage(message))); } if (arrayList.contains(Boolean.TRUE)) { markChanged(); } } @StoreThread public final void handleMessageCreate(com.discord.api.message.Message message) { m.checkNotNullParameter(message, "message"); if (processMessage(message)) { markChanged(); } } @StoreThread public final void handleMessageDelete(ModelMessageDelete modelMessageDelete) { boolean z2; m.checkNotNullParameter(modelMessageDelete, "messageDeleteBulk"); List messageIds = modelMessageDelete.getMessageIds(); m.checkNotNullExpressionValue(messageIds, "messageDeleteBulk.messageIds"); Iterator it = messageIds.iterator(); loop0: while (true) { z2 = false; while (true) { if (!it.hasNext()) { break loop0; } Long l = (Long) it.next(); m.checkNotNullExpressionValue(l, "messageId"); if (deleteMessage(l.longValue(), modelMessageDelete.getChannelId()) || z2) { z2 = true; } } } if (z2) { markChanged(); } } @StoreThread public final void handleMessageUpdate(com.discord.api.message.Message message) { MessageState messageState; m.checkNotNullParameter(message, "message"); if (this.repliedMessagesCache.containsKey(Long.valueOf(message.o())) && (messageState = this.repliedMessagesCache.get(Long.valueOf(message.o()))) != null && (messageState instanceof MessageState.Loaded)) { Message merge = ((MessageState.Loaded) messageState).getMessage().merge(message); updateCache(merge.getId(), merge.getChannelId(), new MessageState.Loaded(merge)); markChanged(); } } public final Observable> observeMessageReferencesForChannel(long j) { Observable> r = ObservationDeck.connectRx$default(this.observationDeck, new ObservationDeck.UpdateSource[]{this}, false, null, null, new StoreMessageReplies$observeMessageReferencesForChannel$1(this, j), 14, null).r(); m.checkNotNullExpressionValue(r, "observationDeck\n … .distinctUntilChanged()"); return r; } @Override // com.discord.stores.StoreV2 public void snapshotData() { super.snapshotData(); snapShotChannelMessages(); snapShotAllMessages(); } }