package kotlin.reflect.jvm.internal.impl.resolve.scopes;

import com.google.android.gms.measurement.api.AppMeasurementSdk;
import defpackage.cr4;
import defpackage.j65;
import defpackage.kp6;
import defpackage.pq6;
import defpackage.qq3;
import defpackage.sq3;
import defpackage.v65;
import defpackage.vx8;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import kotlin.reflect.jvm.internal.impl.descriptors.ClassifierDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.DeclarationDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.PropertyDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.SimpleFunctionDescriptor;
import kotlin.reflect.jvm.internal.impl.descriptors.Substitutable;
import kotlin.reflect.jvm.internal.impl.incremental.components.LookupLocation;
import kotlin.reflect.jvm.internal.impl.name.Name;
import kotlin.reflect.jvm.internal.impl.resolve.calls.inference.CapturedTypeConstructorKt;
import kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope;
import kotlin.reflect.jvm.internal.impl.resolve.scopes.ResolutionScope;
import kotlin.reflect.jvm.internal.impl.types.TypeSubstitution;
import kotlin.reflect.jvm.internal.impl.types.TypeSubstitutor;
import kotlin.reflect.jvm.internal.impl.utils.CollectionsKt;

/* JADX INFO: loaded from: classes3.dex */
@vx8
public final class SubstitutingScope implements MemberScope {

    @kp6
    private final j65 _allDescriptors$delegate;

    @kp6
    private final TypeSubstitutor capturingSubstitutor;

    @pq6
    private Map<DeclarationDescriptor, DeclarationDescriptor> substitutedDescriptors;

    @kp6
    private final j65 substitutor$delegate;

    @kp6
    private final MemberScope workerScope;

    public SubstitutingScope(@kp6 MemberScope memberScope, @kp6 final TypeSubstitutor typeSubstitutor) {
        cr4.g(memberScope, "workerScope");
        cr4.g(typeSubstitutor, "givenSubstitutor");
        this.workerScope = memberScope;
        this.substitutor$delegate = v65.a(new qq3(typeSubstitutor) { // from class: kotlin.reflect.jvm.internal.impl.resolve.scopes.SubstitutingScope$$Lambda$0
            private final TypeSubstitutor arg$0;

            {
                this.arg$0 = typeSubstitutor;
            }

            @Override // defpackage.qq3
            public Object invoke() {
                return SubstitutingScope.substitutor_delegate$lambda$0(this.arg$0);
            }
        });
        TypeSubstitution substitution = typeSubstitutor.getSubstitution();
        cr4.f(substitution, "getSubstitution(...)");
        this.capturingSubstitutor = CapturedTypeConstructorKt.wrapWithCapturingSubstitution$default(substitution, false, 1, null).buildSubstitutor();
        this._allDescriptors$delegate = v65.a(new qq3(this) { // from class: kotlin.reflect.jvm.internal.impl.resolve.scopes.SubstitutingScope$$Lambda$1
            private final SubstitutingScope arg$0;

            {
                this.arg$0 = this;
            }

            @Override // defpackage.qq3
            public Object invoke() {
                return SubstitutingScope._allDescriptors_delegate$lambda$1(this.arg$0);
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final Collection _allDescriptors_delegate$lambda$1(SubstitutingScope substitutingScope) {
        return substitutingScope.substitute(ResolutionScope.DefaultImpls.getContributedDescriptors$default(substitutingScope.workerScope, null, null, 3, null));
    }

    private final Collection<DeclarationDescriptor> get_allDescriptors() {
        return (Collection) this._allDescriptors$delegate.getValue();
    }

    private final <D extends DeclarationDescriptor> D substitute(D d) {
        if (this.capturingSubstitutor.isEmpty()) {
            return d;
        }
        if (this.substitutedDescriptors == null) {
            this.substitutedDescriptors = new HashMap();
        }
        Map<DeclarationDescriptor, DeclarationDescriptor> map = this.substitutedDescriptors;
        cr4.d(map);
        DeclarationDescriptor declarationDescriptorSubstitute = map.get(d);
        if (declarationDescriptorSubstitute == null) {
            if (!(d instanceof Substitutable)) {
                throw new IllegalStateException(("Unknown descriptor in scope: " + d).toString());
            }
            declarationDescriptorSubstitute = ((Substitutable) d).substitute(this.capturingSubstitutor);
            if (declarationDescriptorSubstitute == null) {
                throw new AssertionError("We expect that no conflict should happen while substitution is guaranteed to generate invariant projection, but " + d + " substitution fails");
            }
            map.put(d, declarationDescriptorSubstitute);
        }
        return (D) declarationDescriptorSubstitute;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static final TypeSubstitutor substitutor_delegate$lambda$0(TypeSubstitutor typeSubstitutor) {
        return typeSubstitutor.getSubstitution().buildSubstitutor();
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
    @pq6
    public Set<Name> getClassifierNames() {
        return this.workerScope.getClassifierNames();
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.ResolutionScope
    @pq6
    /* JADX INFO: renamed from: getContributedClassifier */
    public ClassifierDescriptor mo172getContributedClassifier(@kp6 Name name, @kp6 LookupLocation lookupLocation) {
        cr4.g(name, AppMeasurementSdk.ConditionalUserProperty.NAME);
        cr4.g(lookupLocation, "location");
        ClassifierDescriptor classifierDescriptorMo172getContributedClassifier = this.workerScope.mo172getContributedClassifier(name, lookupLocation);
        if (classifierDescriptorMo172getContributedClassifier != null) {
            return (ClassifierDescriptor) substitute(classifierDescriptorMo172getContributedClassifier);
        }
        return null;
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.ResolutionScope
    @kp6
    public Collection<DeclarationDescriptor> getContributedDescriptors(@kp6 DescriptorKindFilter descriptorKindFilter, @kp6 sq3<? super Name, Boolean> sq3Var) {
        cr4.g(descriptorKindFilter, "kindFilter");
        cr4.g(sq3Var, "nameFilter");
        return get_allDescriptors();
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope, kotlin.reflect.jvm.internal.impl.resolve.scopes.ResolutionScope
    @kp6
    public Collection<? extends SimpleFunctionDescriptor> getContributedFunctions(@kp6 Name name, @kp6 LookupLocation lookupLocation) {
        cr4.g(name, AppMeasurementSdk.ConditionalUserProperty.NAME);
        cr4.g(lookupLocation, "location");
        return substitute(this.workerScope.getContributedFunctions(name, lookupLocation));
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
    @kp6
    public Collection<? extends PropertyDescriptor> getContributedVariables(@kp6 Name name, @kp6 LookupLocation lookupLocation) {
        cr4.g(name, AppMeasurementSdk.ConditionalUserProperty.NAME);
        cr4.g(lookupLocation, "location");
        return substitute(this.workerScope.getContributedVariables(name, lookupLocation));
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
    @kp6
    public Set<Name> getFunctionNames() {
        return this.workerScope.getFunctionNames();
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.MemberScope
    @kp6
    public Set<Name> getVariableNames() {
        return this.workerScope.getVariableNames();
    }

    @Override // kotlin.reflect.jvm.internal.impl.resolve.scopes.ResolutionScope
    /* JADX INFO: renamed from: recordLookup */
    public void mo176recordLookup(@kp6 Name name, @kp6 LookupLocation lookupLocation) {
        MemberScope.DefaultImpls.recordLookup(this, name, lookupLocation);
    }

    /* JADX WARN: Multi-variable type inference failed */
    private final <D extends DeclarationDescriptor> Collection<D> substitute(Collection<? extends D> collection) {
        if (this.capturingSubstitutor.isEmpty() || collection.isEmpty()) {
            return collection;
        }
        LinkedHashSet linkedHashSetNewLinkedHashSetWithExpectedSize = CollectionsKt.newLinkedHashSetWithExpectedSize(collection.size());
        Iterator it = collection.iterator();
        while (it.hasNext()) {
            linkedHashSetNewLinkedHashSetWithExpectedSize.add(substitute((DeclarationDescriptor) it.next()));
        }
        return linkedHashSetNewLinkedHashSetWithExpectedSize;
    }
}
