package com.discord.utilities.textprocessing; import c.a.u.b.c.a; import com.airbnb.lottie.parser.AnimatableValueParser; import com.discord.api.message.embed.MessageEmbed; import com.discord.simpleast.code.CodeNode; import com.discord.simpleast.core.node.Node; import com.discord.simpleast.core.node.StyleNode; import com.discord.stores.StoreMessageState; import com.discord.utilities.embed.EmbedResourceUtils; import com.discord.utilities.textprocessing.node.BasicRenderContext; import com.discord.utilities.textprocessing.node.BlockQuoteNode; import com.discord.utilities.textprocessing.node.EmojiNode; import com.discord.utilities.textprocessing.node.SpoilerNode; import com.discord.utilities.textprocessing.node.UrlNode; import d0.g0.t; import d0.t.u; import d0.z.d.e0; import d0.z.d.m; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.List; import java.util.Objects; import kotlin.jvm.internal.DefaultConstructorMarker; /* compiled from: MessagePreprocessor.kt */ public final class MessagePreprocessor implements a { public static final Companion Companion = new Companion(null); private static final int MAX_JUMBO_EMOJI_COUNT = 27; private int customEmojiCount; private final List embeds; private boolean hasLinkConflictingNode; private final Integer maxNodes; private final long myUserId; private boolean shouldJumboify; private List> spoilers; private final Collection visibleSpoilerNodeIndices; /* compiled from: MessagePreprocessor.kt */ public static final class Companion { private Companion() { } public /* synthetic */ Companion(DefaultConstructorMarker defaultConstructorMarker) { this(); } } /* compiled from: MessagePreprocessor.kt */ public static final class ConstrainState { private int limit; public ConstrainState(int i) { this.limit = i; } public static /* synthetic */ ConstrainState copy$default(ConstrainState constrainState, int i, int i2, Object obj) { if ((i2 & 1) != 0) { i = constrainState.limit; } return constrainState.copy(i); } public final int component1() { return this.limit; } public final ConstrainState copy(int i) { return new ConstrainState(i); } public boolean equals(Object obj) { if (this != obj) { return (obj instanceof ConstrainState) && this.limit == ((ConstrainState) obj).limit; } return true; } public final int getLimit() { return this.limit; } public int hashCode() { return this.limit; } public final void setLimit(int i) { this.limit = i; } public String toString() { return c.d.b.a.a.z(c.d.b.a.a.L("ConstrainState(limit="), this.limit, ")"); } } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public MessagePreprocessor(long j, StoreMessageState.State state) { this(j, state != null ? state.getVisibleSpoilerNodeIndices() : null, null, false, null, 24, null); } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public MessagePreprocessor(long j, StoreMessageState.State state, List list, boolean z2, Integer num) { this(j, state != null ? state.getVisibleSpoilerNodeIndices() : null, list, z2, num); } public MessagePreprocessor(long j, Collection collection, List list, boolean z2, Integer num) { this.myUserId = j; this.visibleSpoilerNodeIndices = collection; this.embeds = list; this.shouldJumboify = z2; this.maxNodes = num; } /* JADX INFO: this call moved to the top of the method (can break code semantics) */ public /* synthetic */ MessagePreprocessor(long j, Collection collection, List list, boolean z2, Integer num, int i, DefaultConstructorMarker defaultConstructorMarker) { this(j, (i & 2) != 0 ? null : collection, (i & 4) != 0 ? null : list, (i & 8) != 0 ? true : z2, (i & 16) != 0 ? null : num); } public static final /* synthetic */ void access$mergeConsecutiveQuoteNodes(MessagePreprocessor messagePreprocessor, Collection collection) { messagePreprocessor.mergeConsecutiveQuoteNodes(collection); } private final void constrainAST(Collection> collection) { if (this.maxNodes != null) { Objects.requireNonNull(collection, "null cannot be cast to non-null type kotlin.collections.MutableCollection>"); constrainAST(e0.asMutableCollection(collection), new ConstrainState(this.maxNodes.intValue())); } } private final void constrainAST(Collection> collection, ConstrainState constrainState) { Iterator> it = collection.iterator(); while (it.hasNext()) { Node next = it.next(); if (!m.areEqual(next.getClass(), c.a.u.b.a.a.class)) { constrainState.setLimit(constrainState.getLimit() - 1); } if (constrainState.getLimit() <= 0) { it.remove(); } else if (next.hasChildren()) { Collection> children = next.getChildren(); Objects.requireNonNull(children, "null cannot be cast to non-null type kotlin.collections.MutableCollection>"); constrainAST(e0.asMutableCollection(children), constrainState); if (!next.hasChildren()) { it.remove(); } } } } private final void mergeConsecutiveQuoteNodes(Collection> collection) { ArrayList arrayList = new ArrayList(); Iterator> it = collection.iterator(); while (true) { BlockQuoteNode blockQuoteNode = null; while (true) { if (it.hasNext()) { Node next = it.next(); if (next instanceof BlockQuoteNode) { if (blockQuoteNode == null) { blockQuoteNode = (BlockQuoteNode) next; } else { arrayList.add(next); Collection> children = next.getChildren(); if (children != null) { for (Node node : children) { blockQuoteNode.addChild(node); } } } } } else { collection.removeAll(arrayList); return; } } } } private final void processQuoteChildren(Collection> collection) { Objects.requireNonNull(collection, "null cannot be cast to non-null type kotlin.collections.MutableCollection>"); mergeConsecutiveQuoteNodes(e0.asMutableCollection(collection)); MessagePreprocessor$processQuoteChildren$1 messagePreprocessor$processQuoteChildren$1 = new MessagePreprocessor$processQuoteChildren$1(this); Iterator> it = collection.iterator(); while (it.hasNext()) { AnimatableValueParser.I2((Node) it.next(), messagePreprocessor$processQuoteChildren$1); } } private final void processSpoilerChildren() { List> list = this.spoilers; if (!(!(list == null || list.isEmpty()))) { list = null; } if (list != null) { ArrayList arrayList = new ArrayList(); for (Object obj : list) { if (!((SpoilerNode) obj).isRevealed()) { arrayList.add(obj); } } for (SpoilerNode spoilerNode : arrayList) { AnimatableValueParser.G2(spoilerNode.getChildren(), MessagePreprocessor$processSpoilerChildren$3$1.INSTANCE); } } } private final void stripSimpleEmbedLink(Collection> collection) { List list; if (collection.size() == 1 && (list = this.embeds) != null && list.size() == 1) { MessageEmbed messageEmbed = this.embeds.get(0); if ((((Node) u.elementAt(collection, 0)) instanceof UrlNode) && EmbedResourceUtils.INSTANCE.isSimpleEmbed(messageEmbed)) { collection.clear(); } } } public final long getMyUserId() { return this.myUserId; } public final List> getSpoilers() { return this.spoilers; } public final boolean isLinkifyConflicting() { if (!this.hasLinkConflictingNode) { List> list = this.spoilers; if (list == null || list.isEmpty()) { return false; } } return true; } public final void process(Collection> collection) { m.checkNotNullParameter(collection, "ast"); stripSimpleEmbedLink(e0.asMutableCollection(collection)); AnimatableValueParser.G2(collection, this); if (this.shouldJumboify) { AnimatableValueParser.G2(collection, MessagePreprocessor$process$1.INSTANCE); } processSpoilerChildren(); processQuoteChildren(collection); constrainAST(collection); } /* JADX WARNING: Code restructure failed: missing block: B:5:0x0017, code lost: if (r0 <= 27) goto L_0x0020; */ /* JADX WARNING: Removed duplicated region for block: B:14:0x0033 */ /* JADX WARNING: Removed duplicated region for block: B:18:0x003c */ /* JADX WARNING: Removed duplicated region for block: B:19:0x003f */ @Override // c.a.u.b.c.a public void processNode(Node node) { boolean z2; boolean z3; m.checkNotNullParameter(node, "node"); boolean z4 = false; if (this.shouldJumboify) { if (node instanceof EmojiNode) { int i = this.customEmojiCount + 1; this.customEmojiCount = i; } else { if (!(node instanceof StyleNode)) { if (node instanceof c.a.u.b.a.a) { z3 = t.isBlank(((c.a.u.b.a.a) node).getContent()); if (z3) { z2 = true; this.shouldJumboify = z2; if (!(node instanceof CodeNode)) { this.hasLinkConflictingNode = true; return; } else if (node instanceof UrlNode) { this.hasLinkConflictingNode = true; return; } else if (node instanceof SpoilerNode) { List list = this.spoilers; if (list == null) { list = new ArrayList(); this.spoilers = list; } int size = list.size(); list.add(node); Collection collection = this.visibleSpoilerNodeIndices; if (collection != null) { z4 = collection.contains(Integer.valueOf(size)); } ((SpoilerNode) node).updateState(size, z4); return; } else { return; } } } } z3 = true; if (z3) { } } z3 = false; if (z3) { } } z2 = false; this.shouldJumboify = z2; if (!(node instanceof CodeNode)) { } } }