/*
 * Copyright 2010-2024 JetBrains s.r.o. and Kotlin Programming Language contributors.
 * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
 */

package org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased

import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.analysis.api.KaImplementationDetail
import org.jetbrains.kotlin.analysis.api.KaSession
import org.jetbrains.kotlin.analysis.api.annotations.KaAnnotationList
import org.jetbrains.kotlin.analysis.api.descriptors.Fe10AnalysisContext
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.base.KaFe10Symbol
import org.jetbrains.kotlin.analysis.api.descriptors.symbols.descriptorBased.base.toKtType
import org.jetbrains.kotlin.analysis.api.impl.base.annotations.KaBaseEmptyAnnotationList
import org.jetbrains.kotlin.analysis.api.impl.base.symbols.pointers.KaBaseCachedSymbolPointer
import org.jetbrains.kotlin.analysis.api.lifetime.KaLifetimeToken
import org.jetbrains.kotlin.analysis.api.lifetime.withValidityAssertion
import org.jetbrains.kotlin.analysis.api.symbols.KaNamedFunctionSymbol
import org.jetbrains.kotlin.analysis.api.symbols.KaSymbolOrigin
import org.jetbrains.kotlin.analysis.api.symbols.KaValueParameterSymbol
import org.jetbrains.kotlin.analysis.api.symbols.pointers.KaSymbolPointer
import org.jetbrains.kotlin.analysis.api.types.KaType
import org.jetbrains.kotlin.descriptors.Visibilities
import org.jetbrains.kotlin.descriptors.Visibility
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.createDynamicType

internal class KaFe10DynamicFunctionDescValueParameterSymbol(
    val owner: KaFe10DescNamedFunctionSymbol,
) : KaValueParameterSymbol(), KaFe10Symbol {
    override val analysisContext: Fe10AnalysisContext
        get() = owner.analysisContext

    override val token: KaLifetimeToken
        get() = owner.token

    override val compilerVisibility: Visibility
        get() = withValidityAssertion { Visibilities.Public }

    override val origin: KaSymbolOrigin
        get() = withValidityAssertion { KaSymbolOrigin.JS_DYNAMIC }

    override val psi: PsiElement?
        get() = withValidityAssertion { null }

    override val annotations: KaAnnotationList
        get() = withValidityAssertion { KaBaseEmptyAnnotationList(token) }

    override val name: Name
        get() = withValidityAssertion { Name.identifier("args") }

    override val hasDefaultValue: Boolean
        get() = withValidityAssertion { false }

    override val isVararg: Boolean
        get() = withValidityAssertion { true }

    override val isCrossinline: Boolean
        get() = withValidityAssertion { false }

    override val isNoinline: Boolean
        get() = withValidityAssertion { false }

    override val isImplicitLambdaParameter: Boolean
        get() = withValidityAssertion { false }

    override val returnType: KaType
        get() = withValidityAssertion { createDynamicType(analysisContext.builtIns).toKtType(analysisContext) }

    override fun createPointer(): KaSymbolPointer<KaValueParameterSymbol> = withValidityAssertion {
        Pointer(owner.createPointer(), this)
    }

    override fun equals(other: Any?): Boolean = other is KaFe10DynamicFunctionDescValueParameterSymbol && other.owner == this.owner
    override fun hashCode(): Int = owner.hashCode()


    private class Pointer(
        val ownerPointer: KaSymbolPointer<KaNamedFunctionSymbol>,
        originalSymbol: KaValueParameterSymbol?,
    ) : KaBaseCachedSymbolPointer<KaValueParameterSymbol>(originalSymbol) {
        @KaImplementationDetail
        override fun restoreIfNotCached(analysisSession: KaSession): KaValueParameterSymbol? {
            val owner = ownerPointer.restoreSymbol(analysisSession) as? KaFe10DescNamedFunctionSymbol ?: return null
            return KaFe10DynamicFunctionDescValueParameterSymbol(owner)
        }
    }
}
