package com.ss.android.ugc.effectmanager;

import X.7xU;
import X.C4103LeY;
import X.C4105Lea;
import X.M12;
import android.text.TextUtils;
import com.bytedance.covode.number.Covode;
import com.ss.android.ugc.effectmanager.common.logger.EPLog;
import com.ss.android.ugc.effectmanager.common.utils.CollectionUtil;
import com.ss.android.ugc.effectmanager.common.utils.JsonExtKt;
import com.ss.android.ugc.effectmanager.effect.model.Effect;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import kotlin.h.b.n;
import org.json.JSONObject;

public final class AlgorithmUtils {
    public static final AlgorithmUtils INSTANCE = new AlgorithmUtils();

    static {
        Covode.recordClassIndex(125148);
    }

    private final List<String> getRequirementsOfEffect(Effect effect) {
        return C4105Lea.LJII(effect.getRequirements());
    }

    private final Map<String, List<String>> convertMap(Map<String, ? extends Object> map) {
        if (CollectionUtil.isMapEmpty(map)) {
            return null;
        }
        HashMap hashMap = new HashMap();
        for (Map.Entry next : map.entrySet()) {
            Object key = next.getKey();
            Object value = next.getValue();
            if (CollectionUtil.isCollection(value)) {
                List<String> convertListObjToStr = CollectionUtil.convertListObjToStr(CollectionUtil.convertObjectToList(value));
                if (!CollectionUtil.isListEmpty(convertListObjToStr)) {
                    n.LIZ(convertListObjToStr, "");
                    hashMap.put(key, convertListObjToStr);
                }
            }
        }
        return hashMap;
    }

    public static final Map<String, List<String>> getModelNamesOfEffect(Effect effect) {
        M12.LIZ((Object) effect);
        String modelNames = effect.getModelNames();
        EPLog.d("AlgorithmUtils", "getModelNamesOfEffect->effectId = " + effect.getEffectId() + ", modelNames = " + modelNames);
        Map<String, List<String>> map = null;
        if (TextUtils.isEmpty(modelNames)) {
            return null;
        }
        try {
            map = INSTANCE.convertMap(JsonExtKt.toMap(new JSONObject(modelNames)));
            return map;
        } catch (Exception e) {
            EPLog.e("AlgorithmUtils", "getModelNamesOfEffect", e);
            return map;
        }
    }

    /* JADX WARNING: Removed duplicated region for block: B:22:0x007e  */
    /* JADX WARNING: Removed duplicated region for block: B:31:0x00c3  */
    /* JADX WARNING: Removed duplicated region for block: B:34:0x00d2  */
    /* Code decompiled incorrectly, please refer to instructions dump. */
    public static final java.lang.String[] getResourceNameArrayOfEffect(com.ss.android.ugc.effectmanager.effect.model.Effect r10) {
        /*
            X.M12.LIZ((java.lang.Object) r10)
            com.ss.android.ugc.effectmanager.AlgorithmUtils r0 = INSTANCE
            java.util.List r8 = r0.getRequirementsOfEffect(r10)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "getResourceNameArrayOfEffect::requirements, effect: "
            r1.<init>(r0)
            java.lang.String r0 = r10.getEffectId()
            r1.append(r0)
            java.lang.String r5 = ", name: "
            r1.append(r5)
            java.lang.String r0 = r10.getName()
            r1.append(r0)
            java.lang.String r0 = ", returned requirements: "
            r1.append(r0)
            r1.append(r8)
            java.lang.String r0 = r1.toString()
            java.lang.String r3 = "AlgorithmUtils"
            com.ss.android.ugc.effectmanager.common.logger.EPLog.d(r3, r0)
            java.util.Map r6 = getModelNamesOfEffect(r10)
            boolean r0 = com.ss.android.ugc.effectmanager.common.utils.CollectionUtil.isMapEmpty(r6)
            r9 = 1
            r7 = 0
            if (r0 != 0) goto L_0x0065
            java.util.ArrayList r4 = new java.util.ArrayList
            r4.<init>()
            java.util.Iterator r2 = r8.iterator()
        L_0x0049:
            boolean r0 = r2.hasNext()
            if (r0 == 0) goto L_0x0067
            java.lang.Object r1 = r2.next()
            if (r6 != 0) goto L_0x0058
            kotlin.h.b.n.LIZ()
        L_0x0058:
            boolean r0 = r6.containsKey(r1)
            if (r0 == 0) goto L_0x0049
            r4.add(r1)
            goto L_0x0049
        L_0x0062:
            r8.removeAll(r4)
        L_0x0065:
            r0 = 1
            goto L_0x0074
        L_0x0067:
            boolean r0 = com.ss.android.ugc.effectmanager.common.utils.CollectionUtil.isListEmpty(r4)
            if (r0 != 0) goto L_0x0065
            boolean r0 = kotlin.h.b.n.LIZ(r4, r8)
            if (r0 == 0) goto L_0x0062
            r0 = 0
        L_0x0074:
            r4 = 0
            if (r0 == 0) goto L_0x00d2
            boolean r0 = r8.isEmpty()
            r0 = r0 ^ r9
            if (r0 == 0) goto L_0x00d2
            java.lang.String[] r0 = new java.lang.String[r7]
            java.lang.Object[] r0 = r8.toArray(r0)
            java.lang.String r2 = "null cannot be cast to non-null type kotlin.Array<T>"
            if (r0 == 0) goto L_0x00da
            java.lang.String[] r0 = (java.lang.String[]) r0
            java.lang.String[] r1 = com.bef.effectsdk.RequirementResourceMapper.peekResourcesNeededByRequirements(r0)
            if (r1 == 0) goto L_0x009e
            java.util.List r1 = X.C4103LeY.LJIILIIL(r1)
            java.lang.String[] r0 = new java.lang.String[r7]
            java.lang.Object[] r1 = r1.toArray(r0)
            if (r1 == 0) goto L_0x00d4
            java.lang.String[] r1 = (java.lang.String[]) r1
        L_0x009e:
            com.ss.android.ugc.effectmanager.AlgorithmUtils r0 = INSTANCE
            java.lang.String[] r2 = r0.mergeRequirementsAndModelNames(r1, r6)
            java.lang.StringBuilder r1 = new java.lang.StringBuilder
            java.lang.String r0 = "fetchModels::effectId="
            r1.<init>(r0)
            java.lang.String r0 = r10.getEffectId()
            r1.append(r0)
            r1.append(r5)
            java.lang.String r0 = r10.getName()
            r1.append(r0)
            java.lang.String r0 = ", returned toDownloadRequirements: "
            r1.append(r0)
            if (r2 == 0) goto L_0x00c7
            java.util.List r4 = X.C4103LeY.LJIIJ(r2)
        L_0x00c7:
            r1.append(r4)
            java.lang.String r0 = r1.toString()
            com.ss.android.ugc.effectmanager.common.logger.EPLog.d(r3, r0)
            return r2
        L_0x00d2:
            r1 = r4
            goto L_0x009e
        L_0x00d4:
            X.7xU r0 = new X.7xU
            r0.<init>(r2)
            throw r0
        L_0x00da:
            X.7xU r0 = new X.7xU
            r0.<init>(r2)
            throw r0
        */
        throw new UnsupportedOperationException("Method not decompiled: com.ss.android.ugc.effectmanager.AlgorithmUtils.getResourceNameArrayOfEffect(com.ss.android.ugc.effectmanager.effect.model.Effect):java.lang.String[]");
    }

    private final String[] mergeRequirementsAndModelNames(String[] strArr, Map<String, ? extends List<String>> map) {
        if (CollectionUtil.isMapEmpty(map)) {
            return strArr;
        }
        if (map == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        for (Map.Entry<String, ? extends List<String>> value : map.entrySet()) {
            arrayList.addAll((List) value.getValue());
        }
        ArrayList arrayList2 = new ArrayList();
        if (strArr != null) {
            arrayList2.addAll(C4103LeY.LJIIJ(strArr));
        }
        arrayList2.addAll(arrayList);
        Object[] array = arrayList2.toArray(new String[0]);
        if (array != null) {
            return (String[]) array;
        }
        throw new 7xU("null cannot be cast to non-null type kotlin.Array<T>");
    }
}
