package jsonschema

import (
	
	
	
)

type root struct {
	url                 url
	doc                 any
	resources           map[jsonPointer]*resource
	subschemasProcessed map[jsonPointer]struct{}
}

func ( *root) () *resource {
	return .resources[""]
}

func ( *root) ( jsonPointer) *resource {
	for {
		if ,  := .resources[];  {
			return 
		}
		 := strings.LastIndexByte(string(), '/')
		if  == -1 {
			break
		}
		 = [:]
	}
	return .rootResource()
}

func ( *root) ( fragment,  *resource) (urlPtr, error) {
	var  jsonPointer
	switch f := .convert().(type) {
	case jsonPointer:
		 = .ptr.concat()
	case anchor:
		,  := .anchors[]
		if ! {
			return urlPtr{}, &AnchorNotFoundError{
				URL:       .url.String(),
				Reference: (&urlFrag{.id, }).String(),
			}
		}
		 = 
	}
	return urlPtr{.url, }, nil
}

func ( *root) ( fragment) (urlPtr, error) {
	return .resolveFragmentIn(, .rootResource())
}

// resolves urlFrag to urlPtr from root.
// returns nil if it is external.
func ( *root) ( urlFrag) (*urlPtr, error) {
	var  *resource
	if .url == .url {
		 = .rootResource()
	} else {
		// look for resource with id==uf.url
		for ,  := range .resources {
			if .id == .url {
				 = 
				break
			}
		}
		if  == nil {
			return nil, nil // external url
		}
	}
	,  := .resolveFragmentIn(.frag, )
	return &, 
}

func ( *root) ( any,  jsonPointer,  *resource) error {
	,  := .(map[string]any)
	if ! {
		return nil
	}

	 := func( anchor) error {
		,  := .anchors[]
		if  {
			if  ==  {
				// anchor with same root_ptr already exists
				return nil
			}
			return &DuplicateAnchorError{
				string(), .url.String(), string(), string(),
			}
		}
		.anchors[] = 
		return nil
	}

	if .dialect.draft.version < 2019 {
		if ,  := ["$ref"];  {
			// All other properties in a "$ref" object MUST be ignored
			return nil
		}
		// anchor is specified in id
		if ,  := strVal(, .dialect.draft.id);  {
			, ,  := splitFragment()
			if  != nil {
				 := urlPtr{.url, }
				return &ParseAnchorError{.String()}
			}
			if ,  := .convert().(anchor);  {
				if  := ();  != nil {
					return 
				}
			}
		}
	}
	if .dialect.draft.version >= 2019 {
		if ,  := strVal(, "$anchor");  {
			if  := (anchor());  != nil {
				return 
			}
		}
	}
	if .dialect.draft.version >= 2020 {
		if ,  := strVal(, "$dynamicAnchor");  {
			if  := (anchor());  != nil {
				return 
			}
			.dynamicAnchors = append(.dynamicAnchors, anchor())
		}
	}

	return nil
}

func ( *root) () *root {
	 := map[jsonPointer]struct{}{}
	for  := range .subschemasProcessed {
		[] = struct{}{}
	}
	 := map[jsonPointer]*resource{}
	for ,  := range .resources {
		[] = .clone()
	}
	return &root{
		url:                 .url,
		doc:                 .doc,
		resources:           ,
		subschemasProcessed: ,
	}
}

// --

type resource struct {
	ptr            jsonPointer
	id             url
	dialect        dialect
	anchors        map[anchor]jsonPointer
	dynamicAnchors []anchor
}

func newResource( jsonPointer,  url) *resource {
	return &resource{ptr: , id: , anchors: make(map[anchor]jsonPointer)}
}

func ( *resource) () *resource {
	 := map[anchor]jsonPointer{}
	for ,  := range .anchors {
		[] = 
	}
	return &resource{
		ptr:            .ptr,
		id:             .id,
		dialect:        .dialect,
		anchors:        ,
		dynamicAnchors: slices.Clone(.dynamicAnchors),
	}
}

//--

type UnsupportedVocabularyError struct {
	URL        string
	Vocabulary string
}

func ( *UnsupportedVocabularyError) () string {
	return fmt.Sprintf("unsupported vocabulary %q in %q", .Vocabulary, .URL)
}

// --

type AnchorNotFoundError struct {
	URL       string
	Reference string
}

func ( *AnchorNotFoundError) () string {
	return fmt.Sprintf("anchor in %q not found in schema %q", .Reference, .URL)
}