package v4;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

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

    /* renamed from: a */
    private final Context f39161a;

    /* renamed from: b */
    private final Intent f39162b;

    /* renamed from: c */
    private p f39163c;

    /* renamed from: d */
    private final List f39164d;

    /* renamed from: e */
    private Bundle f39165e;

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

        /* renamed from: a */
        private final int f39166a;

        /* renamed from: b */
        private final Bundle f39167b;

        public a(int i10, Bundle bundle) {
            this.f39166a = i10;
            this.f39167b = bundle;
        }

        public final Bundle a() {
            return this.f39167b;
        }

        public final int b() {
            return this.f39166a;
        }
    }

    public m(Context context) {
        Intent launchIntentForPackage;
        kotlin.jvm.internal.t.g(context, "context");
        this.f39161a = context;
        if (context instanceof Activity) {
            launchIntentForPackage = new Intent(context, context.getClass());
        } else {
            launchIntentForPackage = context.getPackageManager().getLaunchIntentForPackage(context.getPackageName());
            if (launchIntentForPackage == null) {
                launchIntentForPackage = new Intent();
            }
        }
        launchIntentForPackage.addFlags(268468224);
        this.f39162b = launchIntentForPackage;
        this.f39164d = new ArrayList();
    }

    private final void c() {
        int[] R0;
        ArrayList arrayList = new ArrayList();
        ArrayList<? extends Parcelable> arrayList2 = new ArrayList<>();
        o oVar = null;
        for (a aVar : this.f39164d) {
            int b10 = aVar.b();
            Bundle a10 = aVar.a();
            o d10 = d(b10);
            if (d10 == null) {
                throw new IllegalArgumentException("Navigation destination " + o.f39171y.b(this.f39161a, b10) + " cannot be found in the navigation graph " + this.f39163c);
            }
            int[] r10 = d10.r(oVar);
            int length = r10.length;
            int i10 = 0;
            while (i10 < length) {
                int i11 = r10[i10];
                i10++;
                arrayList.add(Integer.valueOf(i11));
                arrayList2.add(a10);
            }
            oVar = d10;
        }
        R0 = sm.c0.R0(arrayList);
        this.f39162b.putExtra("android-support-nav:controller:deepLinkIds", R0);
        this.f39162b.putParcelableArrayListExtra("android-support-nav:controller:deepLinkArgs", arrayList2);
    }

    private final o d(int i10) {
        sm.k kVar = new sm.k();
        p pVar = this.f39163c;
        kotlin.jvm.internal.t.d(pVar);
        kVar.add(pVar);
        while (!kVar.isEmpty()) {
            o oVar = (o) kVar.removeFirst();
            if (oVar.D() == i10) {
                return oVar;
            }
            if (oVar instanceof p) {
                Iterator it = ((p) oVar).iterator();
                while (it.hasNext()) {
                    kVar.add((o) it.next());
                }
            }
        }
        return null;
    }

    public static /* synthetic */ m g(m mVar, int i10, Bundle bundle, int i11, Object obj) {
        if ((i11 & 2) != 0) {
            bundle = null;
        }
        return mVar.f(i10, bundle);
    }

    private final void h() {
        Iterator it = this.f39164d.iterator();
        while (it.hasNext()) {
            int b10 = ((a) it.next()).b();
            if (d(b10) == null) {
                throw new IllegalArgumentException("Navigation destination " + o.f39171y.b(this.f39161a, b10) + " cannot be found in the navigation graph " + this.f39163c);
            }
        }
    }

    public final m a(int i10, Bundle bundle) {
        this.f39164d.add(new a(i10, bundle));
        if (this.f39163c != null) {
            h();
        }
        return this;
    }

    public final androidx.core.app.v b() {
        if (this.f39163c == null) {
            throw new IllegalStateException("You must call setGraph() before constructing the deep link".toString());
        }
        if (!(!this.f39164d.isEmpty())) {
            throw new IllegalStateException("You must call setDestination() or addDestination() before constructing the deep link".toString());
        }
        c();
        androidx.core.app.v g10 = androidx.core.app.v.l(this.f39161a).g(new Intent(this.f39162b));
        kotlin.jvm.internal.t.f(g10, "create(context)\n        …rentStack(Intent(intent))");
        int n10 = g10.n();
        int i10 = 0;
        while (i10 < n10) {
            int i11 = i10 + 1;
            Intent m10 = g10.m(i10);
            if (m10 != null) {
                m10.putExtra("android-support-nav:controller:deepLinkIntent", this.f39162b);
            }
            i10 = i11;
        }
        return g10;
    }

    public final m e(Bundle bundle) {
        this.f39165e = bundle;
        this.f39162b.putExtra("android-support-nav:controller:deepLinkExtras", bundle);
        return this;
    }

    public final m f(int i10, Bundle bundle) {
        this.f39164d.clear();
        this.f39164d.add(new a(i10, bundle));
        if (this.f39163c != null) {
            h();
        }
        return this;
    }

    /* JADX WARN: 'this' call moved to the top of the method (can break code semantics) */
    public m(j navController) {
        this(navController.z());
        kotlin.jvm.internal.t.g(navController, "navController");
        this.f39163c = navController.D();
    }
}
