package jsonschema

import (
	
	
)

// Position tells possible tokens in json.
type Position interface {
	collect(v any, ptr jsonPointer) map[jsonPointer]any
}

// --

type AllProp struct{}

func (AllProp) ( any,  jsonPointer) map[jsonPointer]any {
	,  := .(map[string]any)
	if ! {
		return nil
	}
	 := map[jsonPointer]any{}
	for ,  := range  {
		[.append()] = 
	}
	return 
}

// --

type AllItem struct{}

func (AllItem) ( any,  jsonPointer) map[jsonPointer]any {
	,  := .([]any)
	if ! {
		return nil
	}
	 := map[jsonPointer]any{}
	for ,  := range  {
		[.append(strconv.Itoa())] = 
	}
	return 
}

// --

type Prop string

func ( Prop) ( any,  jsonPointer) map[jsonPointer]any {
	,  := .(map[string]any)
	if ! {
		return nil
	}
	,  := [string()]
	if ! {
		return nil
	}
	return map[jsonPointer]any{
		.append(string()): ,
	}
}

// --

type Item int

func ( Item) ( any,  jsonPointer) map[jsonPointer]any {
	,  := .([]any)
	if ! {
		return nil
	}
	if  < 0 || int() >= len() {
		return nil
	}
	return map[jsonPointer]any{
		.append(strconv.Itoa(int())): [int()],
	}
}

// --

// SchemaPath tells where to look for subschema inside keyword.
type SchemaPath []Position

func schemaPath( string) SchemaPath {
	var  SchemaPath
	for ,  := range strings.Split(, "/") {
		var  Position
		switch  {
		case "*":
			 = AllProp{}
		case "[]":
			 = AllItem{}
		default:
			if ,  := strconv.Atoi();  == nil {
				 = Item()
			} else {
				 = Prop()
			}
		}
		 = append(, )
	}
	return 
}

func ( SchemaPath) ( any,  jsonPointer) map[jsonPointer]any {
	if len() == 0 {
		return map[jsonPointer]any{
			: ,
		}
	}
	,  := [0], [1:]
	 := .collect(, )
	 := map[jsonPointer]any{}
	for ,  := range  {
		 = .(, )
		for ,  := range  {
			[] = 
		}
	}
	return 
}

func ( SchemaPath) () string {
	var  strings.Builder
	for ,  := range  {
		if .Len() != 0 {
			.WriteByte('/')
		}
		switch pos := .(type) {
		case AllProp:
			.WriteString("*")
		case AllItem:
			.WriteString("[]")
		case Prop:
			.WriteString(string())
		case Item:
			.WriteString(strconv.Itoa(int()))
		}
	}
	return .String()
}