// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This file implements (error and trace) message formatting support.

package types

import (
	
	
	
	
	
	
)

func sprintf( *token.FileSet,  Qualifier,  bool,  string,  ...any) string {
	for ,  := range  {
		switch a := .(type) {
		case nil:
			 = "<nil>"
		case operand:
			panic("got operand instead of *operand")
		case *operand:
			 = operandString(, )
		case token.Pos:
			if  != nil {
				 = .Position().String()
			}
		case ast.Expr:
			 = ExprString()
		case []ast.Expr:
			var  bytes.Buffer
			.WriteByte('[')
			writeExprList(&, )
			.WriteByte(']')
			 = .String()
		case Object:
			 = ObjectString(, )
		case Type:
			var  bytes.Buffer
			 := newTypeWriter(&, )
			.tpSubscripts = 
			.typ()
			 = .String()
		case []Type:
			var  bytes.Buffer
			 := newTypeWriter(&, )
			.tpSubscripts = 
			.WriteByte('[')
			for ,  := range  {
				if  > 0 {
					.WriteString(", ")
				}
				.typ()
			}
			.WriteByte(']')
			 = .String()
		case []*TypeParam:
			var  bytes.Buffer
			 := newTypeWriter(&, )
			.tpSubscripts = 
			.WriteByte('[')
			for ,  := range  {
				if  > 0 {
					.WriteString(", ")
				}
				.typ()
			}
			.WriteByte(']')
			 = .String()
		}
		[] = 
	}
	return fmt.Sprintf(, ...)
}

// check may be nil.
func ( *Checker) ( string,  ...any) string {
	var  *token.FileSet
	var  Qualifier
	if  != nil {
		 = .fset
		 = .qualifier
	}
	return sprintf(, , false, , ...)
}

func ( *Checker) ( token.Pos,  string,  ...any) {
	fmt.Printf("%s:\t%s%s\n",
		.fset.Position(),
		strings.Repeat(".  ", .indent),
		sprintf(.fset, .qualifier, true, , ...),
	)
}

// dump is only needed for debugging
func ( *Checker) ( string,  ...any) {
	fmt.Println(sprintf(.fset, .qualifier, true, , ...))
}

func ( *Checker) ( *Package) string {
	// Qualify the package unless it's the package being type-checked.
	if  != .pkg {
		if .pkgPathMap == nil {
			.pkgPathMap = make(map[string]map[string]bool)
			.seenPkgMap = make(map[*Package]bool)
			.markImports(.pkg)
		}
		// If the same package name was used by multiple packages, display the full path.
		if len(.pkgPathMap[.name]) > 1 {
			return strconv.Quote(.path)
		}
		return .name
	}
	return ""
}

// markImports recursively walks pkg and its imports, to record unique import
// paths in pkgPathMap.
func ( *Checker) ( *Package) {
	if .seenPkgMap[] {
		return
	}
	.seenPkgMap[] = true

	,  := .pkgPathMap[.name]
	if ! {
		 = make(map[string]bool)
		.pkgPathMap[.name] = 
	}
	[.path] = true

	for ,  := range .imports {
		.()
	}
}

// stripAnnotations removes internal (type) annotations from s.
func stripAnnotations( string) string {
	var  strings.Builder
	for ,  := range  {
		// strip #'s and subscript digits
		if  < '₀' || '₀'+10 <=  { // '₀' == U+2080
			.WriteRune()
		}
	}
	if .Len() < len() {
		return .String()
	}
	return 
}