package gotypes

import (
	
	
	
)

// Subst returns t with every type parameter in m replaced by its mapped type.
// Only the nodes on the path to a substituted parameter are rebuilt; a subtree
// that mentions none of the mapped parameters is returned unchanged, so special
// types such as the predeclared comparable constraint and named cross-package
// interfaces keep their identity. Aliases keep their names, with substitution
// applied to their type arguments.
//
// ctxt deduplicates any generic types Subst must re-instantiate; pass the caller’s
// shared context so identical instantiations resolve to one instance. A nil ctxt
// is valid (a throwaway): correctness never depends on instance identity, because
// the result is rendered to source text and inspected structurally (go/types
// Identical, underlying-type assertions, reachability walks), never keyed by an
// instance pointer.
func ( *types.Context,  types.Type,  map[*types.TypeParam]types.Type) types.Type {
	if len() == 0 {
		return 
	}
	return (&subster{ctxt: , m: }).subst()
}

type subster struct {
	ctxt *types.Context
	m    map[*types.TypeParam]types.Type
}

func ( *subster) ( types.Type) types.Type {
	switch u := .(type) {
	case nil, *types.Basic:
		return 
	case *types.TypeParam:
		,  := .m[]
		if ! {
			return 
		}
		return 
	case *types.Pointer:
		 := .(.Elem())
		if  == .Elem() {
			return 
		}
		return types.NewPointer()
	case *types.Slice:
		 := .(.Elem())
		if  == .Elem() {
			return 
		}
		return types.NewSlice()
	case *types.Array:
		 := .(.Elem())
		if  == .Elem() {
			return 
		}
		return types.NewArray(, .Len())
	case *types.Chan:
		 := .(.Elem())
		if  == .Elem() {
			return 
		}
		return types.NewChan(.Dir(), )
	case *types.Map:
		,  := .(.Key()), .(.Elem())
		if  == .Key() &&  == .Elem() {
			return 
		}
		return types.NewMap(, )
	case *types.Union:
		return .substUnion()
	case *types.Interface:
		return .substInterface()
	case *types.Signature:
		,  := .substTuple(.Params()), .substTuple(.Results())
		if  == .Params() &&  == .Results() {
			return 
		}
		return types.NewSignatureType(nil, nil, nil,
			, , .Variadic())
	case *types.Struct:
		return .substStruct()
	case *types.Named:
		return .substInstance(, .Origin(), .TypeArgs())
	case *types.Alias:
		return .substInstance(, .Origin(), .TypeArgs())
	default:
		// Unreachable: tuples are handled by substTuple, and every
		// other type kind is covered above.
		panic(fmt.Sprintf("plumb: subst on unsupported type %T", ))
	}
}

func ( *subster) ( *types.Union) *types.Union {
	 := false

	 := slices.Collect(.Terms())
	for ,  := range  {
		 := .subst(.Type())
		if  == .Type() {
			continue
		}
		 = true
		[] = types.NewTerm(.Tilde(), )
	}

	if ! {
		return 
	}

	return types.NewUnion()
}

func ( *subster) ( *types.Interface) *types.Interface {
	 := false

	 := slices.Collect(.ExplicitMethods())
	for ,  := range  {
		 := .subst(.Type()).(*types.Signature)
		if  == .Type() {
			continue
		}
		 = true
		[] = types.NewFunc(.Pos(), .Pkg(), .Name(), )
	}

	 := slices.Collect(.EmbeddedTypes())
	for ,  := range  {
		 := .subst()
		if  ==  {
			continue
		}
		 = true
		[] = 
	}

	if ! {
		return 
	}

	 := types.NewInterfaceType(, )
	if .IsImplicit() {
		.MarkImplicit()
	}
	.Complete()
	return 
}

func ( *subster) ( *types.Struct) *types.Struct {
	 := false

	 := slices.Collect(.Fields())
	for ,  := range  {
		 := .subst(.Type())
		if  == .Type() {
			continue
		}
		 = true
		[] = types.NewField(.Pos(), .Pkg(), .Name(), , .Embedded())
	}

	if ! {
		return 
	}

	 := make([]string, len())
	for  := range  {
		[] = .Tag()
	}
	return types.NewStruct(, )
}

func ( *subster) ( *types.Tuple) *types.Tuple {
	 := false

	 := slices.Collect(.Variables())
	for ,  := range  {
		 := .subst(.Type())
		if  == .Type() {
			continue
		}
		 = true
		[] = types.NewVar(.Pos(), .Pkg(), .Name(), )
	}

	if ! {
		return 
	}

	return types.NewTuple(...)
}

func ( *subster) (,  types.Type,  *types.TypeList) types.Type {
	 := false

	 := slices.Collect(.Types())
	for ,  := range  {
		 := .subst()
		if  ==  {
			continue
		}
		 = true
		[] = 
	}

	if ! {
		return 
	}

	// The origin and argument count come from an existing instance. With
	// validation disabled, instantiation cannot fail unless that invariant breaks.
	,  := types.Instantiate(.ctxt, , , false)
	if  != nil {
		panic(fmt.Sprintf("plumb: Subst: re-instantiating %s failed: %v", , ))
	}
	return 
}