157 lines
6.5 KiB
Java
157 lines
6.5 KiB
Java
package com.discord.widgets.chat.input.applicationcommands;
|
|
|
|
import androidx.annotation.VisibleForTesting;
|
|
import com.discord.models.commands.ApplicationCommand;
|
|
import com.discord.models.commands.ApplicationCommandOption;
|
|
import com.discord.widgets.chat.input.models.OptionRange;
|
|
import d0.g0.e;
|
|
import d0.g0.w;
|
|
import d0.z.d.m;
|
|
import java.util.Iterator;
|
|
import java.util.LinkedHashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Objects;
|
|
import kotlin.ranges.IntRange;
|
|
import kotlin.text.MatchResult;
|
|
import kotlin.text.Regex;
|
|
/* compiled from: WidgetChatInputApplicationCommands.kt */
|
|
public final class WidgetChatInputApplicationCommandsKt {
|
|
public static final Map<ApplicationCommandOption, OptionRange> findOptionRanges(String str, ApplicationCommand applicationCommand) {
|
|
List<ApplicationCommandOption> options;
|
|
IntRange findValueRange;
|
|
m.checkNotNullParameter(str, "$this$findOptionRanges");
|
|
LinkedHashMap linkedHashMap = new LinkedHashMap();
|
|
if (!(applicationCommand == null || (options = applicationCommand.getOptions()) == null)) {
|
|
for (ApplicationCommandOption applicationCommandOption : options) {
|
|
Integer findStartOfParam = findStartOfParam(str, applicationCommandOption.getName());
|
|
if (!(findStartOfParam == null || (findValueRange = findValueRange(str, applicationCommand, applicationCommandOption.getName())) == null)) {
|
|
OptionRange optionRange = (OptionRange) linkedHashMap.put(applicationCommandOption, new OptionRange(new IntRange(findStartOfParam.intValue(), findValueRange.getFirst()), findValueRange));
|
|
}
|
|
}
|
|
}
|
|
return linkedHashMap;
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public static final Integer findStartOfParam(String str, String str2) {
|
|
IntRange range;
|
|
m.checkNotNullParameter(str, "$this$findStartOfParam");
|
|
m.checkNotNullParameter(str2, "paramName");
|
|
MatchResult find$default = Regex.find$default(new Regex("[\\s|\\n]" + str2 + ':'), str, 0, 2, null);
|
|
int first = (find$default == null || (range = find$default.getRange()) == null) ? -1 : range.getFirst();
|
|
if (first == -1) {
|
|
return null;
|
|
}
|
|
return Integer.valueOf(first + 1);
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public static final Integer findStartOfValue(String str, String str2) {
|
|
m.checkNotNullParameter(str, "$this$findStartOfValue");
|
|
m.checkNotNullParameter(str2, "paramName");
|
|
Integer findStartOfParam = findStartOfParam(str, str2);
|
|
int intValue = findStartOfParam != null ? findStartOfParam.intValue() : -1;
|
|
if (intValue != -1) {
|
|
return Integer.valueOf(str2.length() + intValue + 1);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static final IntRange findValueRange(String str, ApplicationCommand applicationCommand, String str2) {
|
|
int i;
|
|
e eVar;
|
|
IntRange range;
|
|
ApplicationCommandOption applicationCommandOption;
|
|
boolean z2;
|
|
List<ApplicationCommandOption> options;
|
|
Object obj;
|
|
m.checkNotNullParameter(str, "$this$findValueRange");
|
|
m.checkNotNullParameter(str2, "paramName");
|
|
Integer findStartOfValue = findStartOfValue(str, str2);
|
|
if (findStartOfValue == null) {
|
|
return null;
|
|
}
|
|
int intValue = findStartOfValue.intValue();
|
|
Regex regex = new Regex(" ([\\w-_]*):");
|
|
String substring = str.substring(intValue);
|
|
m.checkNotNullExpressionValue(substring, "(this as java.lang.String).substring(startIndex)");
|
|
Iterator it = Regex.findAll$default(regex, substring, 0, 2, null).iterator();
|
|
while (true) {
|
|
if (!it.hasNext()) {
|
|
i = -1;
|
|
break;
|
|
}
|
|
Iterator<e> it2 = ((MatchResult) it.next()).getGroups().iterator();
|
|
while (true) {
|
|
if (!it2.hasNext()) {
|
|
eVar = null;
|
|
break;
|
|
}
|
|
eVar = it2.next();
|
|
e eVar2 = eVar;
|
|
if (applicationCommand == null || (options = applicationCommand.getOptions()) == null) {
|
|
applicationCommandOption = null;
|
|
} else {
|
|
Iterator<T> it3 = options.iterator();
|
|
while (true) {
|
|
if (!it3.hasNext()) {
|
|
obj = null;
|
|
break;
|
|
}
|
|
obj = it3.next();
|
|
if (m.areEqual(eVar2 != null ? eVar2.getValue() : null, ((ApplicationCommandOption) obj).getName())) {
|
|
break;
|
|
}
|
|
}
|
|
applicationCommandOption = (ApplicationCommandOption) obj;
|
|
}
|
|
if (applicationCommandOption != null) {
|
|
z2 = true;
|
|
continue;
|
|
} else {
|
|
z2 = false;
|
|
continue;
|
|
}
|
|
if (z2) {
|
|
break;
|
|
}
|
|
}
|
|
e eVar3 = eVar;
|
|
if (eVar3 == null || (range = eVar3.getRange()) == null) {
|
|
i = -1;
|
|
continue;
|
|
} else {
|
|
i = range.getFirst();
|
|
continue;
|
|
}
|
|
if (i != -1) {
|
|
break;
|
|
}
|
|
}
|
|
return i == -1 ? new IntRange(intValue, str.length()) : new IntRange(intValue, i + intValue);
|
|
}
|
|
|
|
public static final String transformParameterSpannableString(String str) {
|
|
if (str == null) {
|
|
return null;
|
|
}
|
|
if (w.endsWith$default((CharSequence) w.trim(str).toString(), ':', false, 2, (Object) null)) {
|
|
return w.trim(str).toString();
|
|
}
|
|
if (!w.contains$default((CharSequence) str, ':', false, 2, (Object) null)) {
|
|
return str;
|
|
}
|
|
List split$default = w.split$default((CharSequence) str, new char[]{':'}, false, 0, 6, (Object) null);
|
|
StringBuilder sb = new StringBuilder();
|
|
String str2 = (String) split$default.get(0);
|
|
Objects.requireNonNull(str2, "null cannot be cast to non-null type kotlin.CharSequence");
|
|
sb.append(w.trim(str2).toString());
|
|
sb.append(": ");
|
|
String str3 = (String) split$default.get(1);
|
|
Objects.requireNonNull(str3, "null cannot be cast to non-null type kotlin.CharSequence");
|
|
sb.append(w.trim(str3).toString());
|
|
return sb.toString();
|
|
}
|
|
}
|