package runtime

import (
	
	
	
)

func getTag( reflect.StructField) string {
	return .Tag.Get("json")
}

func ( reflect.StructField) bool {
	if .PkgPath != "" {
		if .Anonymous {
			 := .Type
			if .Kind() == reflect.Ptr {
				 = .Elem()
			}
			if .Kind() != reflect.Struct {
				return true
			}
		} else {
			// private field
			return true
		}
	}
	 := getTag()
	return  == "-"
}

type StructTag struct {
	Key         string
	IsTaggedKey bool
	IsOmitEmpty bool
	// IsOmitZero is whether the field has the omitzero option, which encoding/json has from Go 1.24: it is
	// false with an older Go, whose encoding/json ignores the option.
	IsOmitZero bool
	IsString   bool
	Field      reflect.StructField
	// QuotedKey is Key as a JSON string, if Key may need an escape: a name of the v2 semantics may have any
	// character.
	QuotedKey string
}

type StructTags []*StructTag

func ( StructTags) ( string) bool {
	for ,  := range  {
		if .Key ==  {
			return true
		}
	}
	return false
}

func isValidTag( string) bool {
	if  == "" {
		return false
	}
	for ,  := range  {
		switch {
		case strings.ContainsRune("!#$%&()*+-./:;<=>?@[]^_{|}~ ", ):
			// Backslash and quote chars are reserved, but
			// otherwise any punctuation chars are allowed
			// in a tag name.
		case !unicode.IsLetter() && !unicode.IsDigit():
			return false
		}
	}
	return true
}

func ( reflect.StructField) *StructTag {
	 := .Name
	 := getTag()
	 := &StructTag{Field: }
	 := strings.Split(, ",")
	if len() > 0 {
		if [0] != "" && isValidTag([0]) {
			 = [0]
			.IsTaggedKey = true
		}
	}
	.Key = 
	if len() > 1 {
		for ,  := range [1:] {
			switch  {
			case "omitempty":
				.IsOmitEmpty = true
			case "omitzero":
				.IsOmitZero = omitZeroOption
			case "string":
				.IsString = true
			}
		}
	}
	return 
}

// NamedField is a field of a struct, or of a struct embedded in it whose fields are promoted, which has a key in the
// JSON object.
type NamedField interface {
	// Depth is how deep the field is in the structs embedded in the struct: 0 for a field of the struct itself.
	Depth() int
	// IsTaggedKey reports whether the key is the name in the field's tag.
	IsTaggedKey() bool
}

// DominantField returns which one of fields, the fields of a struct which have the same key, is written and read
// with the key, as encoding/json chooses it: the least deep one, or, if more than one are at that depth, the only
// one of them with the key in its tag. It returns -1 when none is.
func [ NamedField]( []) int {
	 := [0].Depth()
	for ,  := range [1:] {
		 = min(, .Depth())
	}
	 := -1
	var ,  int
	for ,  := range  {
		if .Depth() !=  {
			continue
		}
		++
		if .IsTaggedKey() {
			++
			 = 
		} else if  == 1 {
			 = 
		}
	}
	if  > 1 &&  != 1 {
		return -1
	}
	return 
}