package v4;

import android.net.Uri;
import android.os.Bundle;
import com.google.firebase.sessions.settings.RemoteSettings;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/* loaded from: classes.dex */
public final class l {

    /* renamed from: l, reason: collision with root package name */
    private static final b f39138l = new b(null);

    /* renamed from: m, reason: collision with root package name */
    private static final Pattern f39139m = Pattern.compile("^[a-zA-Z]+[+\\w\\-.]*:");

    /* renamed from: a, reason: collision with root package name */
    private final String f39140a;

    /* renamed from: b, reason: collision with root package name */
    private final String f39141b;

    /* renamed from: c, reason: collision with root package name */
    private final String f39142c;

    /* renamed from: d, reason: collision with root package name */
    private final List f39143d = new ArrayList();

    /* renamed from: e, reason: collision with root package name */
    private final Map f39144e = new LinkedHashMap();

    /* renamed from: f, reason: collision with root package name */
    private String f39145f;

    /* renamed from: g, reason: collision with root package name */
    private final rm.o f39146g;

    /* renamed from: h, reason: collision with root package name */
    private boolean f39147h;

    /* renamed from: i, reason: collision with root package name */
    private String f39148i;

    /* renamed from: j, reason: collision with root package name */
    private final rm.o f39149j;

    /* renamed from: k, reason: collision with root package name */
    private boolean f39150k;

    /* loaded from: classes.dex */
    public static final class a {

        /* renamed from: d, reason: collision with root package name */
        public static final C0926a f39151d = new C0926a(null);

        /* renamed from: a, reason: collision with root package name */
        private String f39152a;

        /* renamed from: b, reason: collision with root package name */
        private String f39153b;

        /* renamed from: c, reason: collision with root package name */
        private String f39154c;

        /* renamed from: v4.l$a$a, reason: collision with other inner class name */
        /* loaded from: classes.dex */
        public static final class C0926a {
            private C0926a() {
            }

            public /* synthetic */ C0926a(kotlin.jvm.internal.k kVar) {
                this();
            }
        }

        public final l a() {
            return new l(this.f39152a, this.f39153b, this.f39154c);
        }

        public final a b(String action) {
            kotlin.jvm.internal.t.g(action, "action");
            if (!(action.length() > 0)) {
                throw new IllegalArgumentException("The NavDeepLink cannot have an empty action.".toString());
            }
            this.f39153b = action;
            return this;
        }

        public final a c(String mimeType) {
            kotlin.jvm.internal.t.g(mimeType, "mimeType");
            this.f39154c = mimeType;
            return this;
        }

        public final a d(String uriPattern) {
            kotlin.jvm.internal.t.g(uriPattern, "uriPattern");
            this.f39152a = uriPattern;
            return this;
        }
    }

    /* loaded from: classes.dex */
    private static final class b {
        private b() {
        }

        public /* synthetic */ b(kotlin.jvm.internal.k kVar) {
            this();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class c implements Comparable {

        /* renamed from: p, reason: collision with root package name */
        private String f39155p;

        /* renamed from: q, reason: collision with root package name */
        private String f39156q;

        public c(String mimeType) {
            List k10;
            kotlin.jvm.internal.t.g(mimeType, "mimeType");
            List h10 = new kotlin.text.l(RemoteSettings.FORWARD_SLASH_STRING).h(mimeType, 0);
            if (!h10.isEmpty()) {
                ListIterator listIterator = h10.listIterator(h10.size());
                while (listIterator.hasPrevious()) {
                    if (!(((String) listIterator.previous()).length() == 0)) {
                        k10 = sm.c0.K0(h10, listIterator.nextIndex() + 1);
                        break;
                    }
                }
            }
            k10 = sm.u.k();
            this.f39155p = (String) k10.get(0);
            this.f39156q = (String) k10.get(1);
        }

        @Override // java.lang.Comparable
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public int compareTo(c other) {
            kotlin.jvm.internal.t.g(other, "other");
            int i10 = kotlin.jvm.internal.t.b(this.f39155p, other.f39155p) ? 2 : 0;
            return kotlin.jvm.internal.t.b(this.f39156q, other.f39156q) ? i10 + 1 : i10;
        }

        public final String e() {
            return this.f39156q;
        }

        public final String g() {
            return this.f39155p;
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static final class d {

        /* renamed from: a, reason: collision with root package name */
        private String f39157a;

        /* renamed from: b, reason: collision with root package name */
        private final List f39158b = new ArrayList();

        public final void a(String name) {
            kotlin.jvm.internal.t.g(name, "name");
            this.f39158b.add(name);
        }

        public final String b(int i10) {
            return (String) this.f39158b.get(i10);
        }

        public final List c() {
            return this.f39158b;
        }

        public final String d() {
            return this.f39157a;
        }

        public final void e(String str) {
            this.f39157a = str;
        }

        public final int f() {
            return this.f39158b.size();
        }
    }

    /* loaded from: classes.dex */
    static final class e extends kotlin.jvm.internal.v implements dn.a {
        e() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final Pattern invoke() {
            String str = l.this.f39148i;
            if (str == null) {
                return null;
            }
            return Pattern.compile(str);
        }
    }

    /* loaded from: classes.dex */
    static final class f extends kotlin.jvm.internal.v implements dn.a {
        f() {
            super(0);
        }

        @Override // dn.a
        /* renamed from: a, reason: merged with bridge method [inline-methods] */
        public final Pattern invoke() {
            String str = l.this.f39145f;
            if (str == null) {
                return null;
            }
            return Pattern.compile(str, 2);
        }
    }

    public l(String str, String str2, String str3) {
        rm.o a10;
        rm.o a11;
        String J;
        String J2;
        String J3;
        this.f39140a = str;
        this.f39141b = str2;
        this.f39142c = str3;
        a10 = rm.q.a(new f());
        this.f39146g = a10;
        a11 = rm.q.a(new e());
        this.f39149j = a11;
        if (str != null) {
            Uri parse = Uri.parse(str);
            this.f39147h = parse.getQuery() != null;
            StringBuilder sb2 = new StringBuilder("^");
            if (!f39139m.matcher(str).find()) {
                sb2.append("http[s]?://");
            }
            Pattern fillInPattern = Pattern.compile("\\{(.+?)\\}");
            if (this.f39147h) {
                Matcher matcher = Pattern.compile("(\\?)").matcher(str);
                if (matcher.find()) {
                    String substring = str.substring(0, matcher.start());
                    kotlin.jvm.internal.t.f(substring, "this as java.lang.String…ing(startIndex, endIndex)");
                    kotlin.jvm.internal.t.f(fillInPattern, "fillInPattern");
                    this.f39150k = c(substring, sb2, fillInPattern);
                }
                for (String paramName : parse.getQueryParameterNames()) {
                    StringBuilder sb3 = new StringBuilder();
                    String queryParameter = parse.getQueryParameter(paramName);
                    if (queryParameter == null) {
                        throw new NullPointerException("null cannot be cast to non-null type kotlin.String");
                    }
                    Matcher matcher2 = fillInPattern.matcher(queryParameter);
                    d dVar = new d();
                    int i10 = 0;
                    while (matcher2.find()) {
                        String group = matcher2.group(1);
                        if (group == null) {
                            throw new NullPointerException("null cannot be cast to non-null type kotlin.String");
                        }
                        dVar.a(group);
                        String substring2 = queryParameter.substring(i10, matcher2.start());
                        kotlin.jvm.internal.t.f(substring2, "this as java.lang.String…ing(startIndex, endIndex)");
                        sb3.append(Pattern.quote(substring2));
                        sb3.append("(.+?)?");
                        i10 = matcher2.end();
                    }
                    if (i10 < queryParameter.length()) {
                        String substring3 = queryParameter.substring(i10);
                        kotlin.jvm.internal.t.f(substring3, "this as java.lang.String).substring(startIndex)");
                        sb3.append(Pattern.quote(substring3));
                    }
                    String sb4 = sb3.toString();
                    kotlin.jvm.internal.t.f(sb4, "argRegex.toString()");
                    J3 = kotlin.text.y.J(sb4, ".*", "\\E.*\\Q", false, 4, null);
                    dVar.e(J3);
                    Map map = this.f39144e;
                    kotlin.jvm.internal.t.f(paramName, "paramName");
                    map.put(paramName, dVar);
                }
            } else {
                kotlin.jvm.internal.t.f(fillInPattern, "fillInPattern");
                this.f39150k = c(str, sb2, fillInPattern);
            }
            String sb5 = sb2.toString();
            kotlin.jvm.internal.t.f(sb5, "uriRegex.toString()");
            J2 = kotlin.text.y.J(sb5, ".*", "\\E.*\\Q", false, 4, null);
            this.f39145f = J2;
        }
        if (this.f39142c != null) {
            if (!Pattern.compile("^[\\s\\S]+/[\\s\\S]+$").matcher(this.f39142c).matches()) {
                throw new IllegalArgumentException(("The given mimeType " + ((Object) g()) + " does not match to required \"type/subtype\" format").toString());
            }
            c cVar = new c(this.f39142c);
            J = kotlin.text.y.J("^(" + cVar.g() + "|[*]+)/(" + cVar.e() + "|[*]+)$", "*|[*]", "[\\s\\S]", false, 4, null);
            this.f39148i = J;
        }
    }

    private final boolean c(String str, StringBuilder sb2, Pattern pattern) {
        boolean U;
        Matcher matcher = pattern.matcher(str);
        U = kotlin.text.z.U(str, ".*", false, 2, null);
        boolean z10 = !U;
        int i10 = 0;
        while (matcher.find()) {
            String group = matcher.group(1);
            if (group == null) {
                throw new NullPointerException("null cannot be cast to non-null type kotlin.String");
            }
            this.f39143d.add(group);
            String substring = str.substring(i10, matcher.start());
            kotlin.jvm.internal.t.f(substring, "this as java.lang.String…ing(startIndex, endIndex)");
            sb2.append(Pattern.quote(substring));
            sb2.append("([^/]+?)");
            i10 = matcher.end();
            z10 = false;
        }
        if (i10 < str.length()) {
            String substring2 = str.substring(i10);
            kotlin.jvm.internal.t.f(substring2, "this as java.lang.String).substring(startIndex)");
            sb2.append(Pattern.quote(substring2));
        }
        sb2.append("($|(\\?(.)*)|(\\#(.)*))");
        return z10;
    }

    private final Pattern i() {
        return (Pattern) this.f39149j.getValue();
    }

    private final Pattern j() {
        return (Pattern) this.f39146g.getValue();
    }

    private final boolean m(Bundle bundle, String str, String str2, v4.f fVar) {
        if (fVar != null) {
            fVar.a().d(bundle, str, str2);
            return false;
        }
        bundle.putString(str, str2);
        return false;
    }

    public final String d() {
        return this.f39141b;
    }

    public final List e() {
        List z02;
        List list = this.f39143d;
        Collection values = this.f39144e.values();
        ArrayList arrayList = new ArrayList();
        Iterator it = values.iterator();
        while (it.hasNext()) {
            sm.z.A(arrayList, ((d) it.next()).c());
        }
        z02 = sm.c0.z0(list, arrayList);
        return z02;
    }

    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof l)) {
            return false;
        }
        l lVar = (l) obj;
        return kotlin.jvm.internal.t.b(this.f39140a, lVar.f39140a) && kotlin.jvm.internal.t.b(this.f39141b, lVar.f39141b) && kotlin.jvm.internal.t.b(this.f39142c, lVar.f39142c);
    }

    public final Bundle f(Uri deepLink, Map arguments) {
        Matcher matcher;
        String str;
        kotlin.jvm.internal.t.g(deepLink, "deepLink");
        kotlin.jvm.internal.t.g(arguments, "arguments");
        Pattern j10 = j();
        Matcher matcher2 = j10 == null ? null : j10.matcher(deepLink.toString());
        if (matcher2 == null || !matcher2.matches()) {
            return null;
        }
        Bundle bundle = new Bundle();
        int size = this.f39143d.size();
        int i10 = 0;
        while (i10 < size) {
            int i11 = i10 + 1;
            String str2 = (String) this.f39143d.get(i10);
            String value = Uri.decode(matcher2.group(i11));
            v4.f fVar = (v4.f) arguments.get(str2);
            try {
                kotlin.jvm.internal.t.f(value, "value");
                if (m(bundle, str2, value, fVar)) {
                    return null;
                }
                i10 = i11;
            } catch (IllegalArgumentException unused) {
                return null;
            }
        }
        if (this.f39147h) {
            for (String str3 : this.f39144e.keySet()) {
                d dVar = (d) this.f39144e.get(str3);
                String queryParameter = deepLink.getQueryParameter(str3);
                if (queryParameter != null) {
                    kotlin.jvm.internal.t.d(dVar);
                    matcher = Pattern.compile(dVar.d(), 32).matcher(queryParameter);
                    if (!matcher.matches()) {
                        return null;
                    }
                } else {
                    matcher = null;
                }
                Bundle bundle2 = new Bundle();
                try {
                    kotlin.jvm.internal.t.d(dVar);
                    int f10 = dVar.f();
                    int i12 = 0;
                    while (i12 < f10) {
                        int i13 = i12 + 1;
                        if (matcher != null) {
                            str = matcher.group(i13);
                            if (str == null) {
                                str = "";
                            }
                        } else {
                            str = null;
                        }
                        String b10 = dVar.b(i12);
                        v4.f fVar2 = (v4.f) arguments.get(b10);
                        if (str != null) {
                            if (!kotlin.jvm.internal.t.b(str, '{' + b10 + '}') && m(bundle2, b10, str, fVar2)) {
                                return null;
                            }
                        }
                        i12 = i13;
                    }
                    bundle.putAll(bundle2);
                } catch (IllegalArgumentException unused2) {
                }
            }
        }
        for (Map.Entry entry : arguments.entrySet()) {
            String str4 = (String) entry.getKey();
            v4.f fVar3 = (v4.f) entry.getValue();
            if (((fVar3 == null || fVar3.c() || fVar3.b()) ? false : true) && !bundle.containsKey(str4)) {
                return null;
            }
        }
        return bundle;
    }

    public final String g() {
        return this.f39142c;
    }

    public final int h(String mimeType) {
        kotlin.jvm.internal.t.g(mimeType, "mimeType");
        if (this.f39142c != null) {
            Pattern i10 = i();
            kotlin.jvm.internal.t.d(i10);
            if (i10.matcher(mimeType).matches()) {
                return new c(this.f39142c).compareTo(new c(mimeType));
            }
        }
        return -1;
    }

    public int hashCode() {
        String str = this.f39140a;
        int hashCode = ((str == null ? 0 : str.hashCode()) + 0) * 31;
        String str2 = this.f39141b;
        int hashCode2 = (hashCode + (str2 == null ? 0 : str2.hashCode())) * 31;
        String str3 = this.f39142c;
        return hashCode2 + (str3 != null ? str3.hashCode() : 0);
    }

    public final String k() {
        return this.f39140a;
    }

    public final boolean l() {
        return this.f39150k;
    }
}
