package jsonschema

import (
	
	
)

type roots struct {
	defaultDraft *Draft
	roots        map[url]*root
	loader       defaultLoader
	regexpEngine RegexpEngine
	vocabularies map[string]*Vocabulary
	assertVocabs bool
}

func newRoots() *roots {
	return &roots{
		defaultDraft: draftLatest,
		roots:        map[url]*root{},
		loader: defaultLoader{
			docs:   map[url]any{},
			loader: FileLoader{},
		},
		regexpEngine: goRegexpCompile,
		vocabularies: map[string]*Vocabulary{},
	}
}

func ( *roots) ( url) (*root, error) {
	if ,  := .roots[];  {
		return , nil
	}
	,  := .loader.load()
	if  != nil {
		return nil, 
	}
	return .addRoot(, )
}

func ( *roots) ( url,  any) (*root, error) {
	 := &root{
		url:                 ,
		doc:                 ,
		resources:           map[jsonPointer]*resource{},
		subschemasProcessed: map[jsonPointer]struct{}{},
	}
	if  := .collectResources(, , , "", dialect{.defaultDraft, nil});  != nil {
		return nil, 
	}
	if !strings.HasPrefix(.String(), "http://json-schema.org/") &&
		!strings.HasPrefix(.String(), "https://json-schema.org/") {
		if  := .validate(, , "");  != nil {
			return nil, 
		}
	}

	.roots[] = 
	return , nil
}

func ( *roots) ( urlFrag) (urlPtr, error) {
	,  := .orLoad(.url)
	if  != nil {
		return urlPtr{}, 
	}
	return .resolveFragment(.frag)
}

func ( *roots) ( *root,  any,  url,  jsonPointer,  dialect) error {
	if ,  := .subschemasProcessed[];  {
		return nil
	}
	if  := ._collectResources(, , , , );  != nil {
		return 
	}
	.subschemasProcessed[] = struct{}{}
	return nil
}

func ( *roots) ( *root,  any,  url,  jsonPointer,  dialect) error {
	,  := .(map[string]any)
	if ! {
		if .isEmpty() {
			// root resource
			 := newResource(, )
			.dialect = 
			.resources[] = 
		}
		return nil
	}

	 := false
	if ,  := ["$schema"];  {
		if ,  := .(string);  {
			 = true
		}
	}

	,  := .loader.getDraft(urlPtr{.url, }, , .draft, map[url]struct{}{})
	if  != nil {
		return 
	}
	 := .getID()
	if  == "" && !.isEmpty() {
		// ignore $schema
		 = .draft
		 = false
		 = .getID()
	}

	var  *resource
	if  != "" {
		,  := .join()
		if  != nil {
			 := urlPtr{.url, }
			return &ParseIDError{.String()}
		}
		 = .url
		 = newResource(, )
	} else if .isEmpty() {
		// root resource
		 = newResource(, )
	}

	if  != nil {
		 := false
		for ,  := range .resources {
			if .id ==  {
				 = true
				if .ptr !=  {
					return &DuplicateIDError{.String(), .url.String(), string(), string(.ptr)}
				}
			}
		}
		if ! {
			if  {
				,  := .loader.getMetaVocabs(, , .vocabularies)
				if  != nil {
					return 
				}
				.dialect = dialect{, }
			} else {
				.dialect = 
			}
			.resources[] = 
		}
	}

	var  *resource
	for ,  := range .resources {
		if .id ==  {
			 = 
			break
		}
	}
	if  == nil {
		panic("baseres is nil")
	}

	// found base resource
	if  := .collectAnchors(, , );  != nil {
		return 
	}

	// process subschemas
	 := map[jsonPointer]any{}
	for ,  := range .subschemas {
		 := .collect(, )
		for ,  := range  {
			[] = 
		}
	}
	for ,  := range .dialect.activeVocabs(true, .vocabularies) {
		if  := .vocabularies[];  != nil {
			for ,  := range .Subschemas {
				 := .collect(, )
				for ,  := range  {
					[] = 
				}
			}
		}
	}
	for ,  := range  {
		if  := .collectResources(, , , , .dialect);  != nil {
			return 
		}
	}

	return nil
}

func ( *roots) ( urlPtr) error {
	,  := .orLoad(.url)
	if  != nil {
		return 
	}
	if ,  := .subschemasProcessed[.ptr];  {
		return nil
	}
	,  := .lookup(.doc)
	if  != nil {
		return 
	}
	 := .clone()
	if  := .addSubschema(, .ptr);  != nil {
		return 
	}
	if  := .validate(, , .ptr);  != nil {
		return 
	}
	.roots[.url] = 
	return nil
}

func ( *roots) ( *root,  jsonPointer) error {
	,  := (&urlPtr{.url, }).lookup(.doc)
	if  != nil {
		return 
	}
	 := .resource()
	 := .id
	if  := .collectResources(, , , , .dialect);  != nil {
		return 
	}

	// collect anchors
	if ,  := .resources[]; ! {
		 := .resource()
		if  := .collectAnchors(, , );  != nil {
			return 
		}
	}
	return nil
}

func ( *roots) ( *root,  any,  jsonPointer) error {
	 := .resource().dialect
	 := .getSchema(.assertVocabs, .vocabularies)
	if  := .validate(, .regexpEngine, , .resources, .assertVocabs, .vocabularies);  != nil {
		 := urlPtr{.url, }
		return &SchemaValidationError{URL: .String(), Err: }
	}
	return nil
}

// --

type InvalidMetaSchemaURLError struct {
	URL string
	Err error
}

func ( *InvalidMetaSchemaURLError) () string {
	return fmt.Sprintf("invalid $schema in %q: %v", .URL, .Err)
}

// --

type UnsupportedDraftError struct {
	URL string
}

func ( *UnsupportedDraftError) () string {
	return fmt.Sprintf("draft %q is not supported", .URL)
}

// --

type MetaSchemaCycleError struct {
	URL string
}

func ( *MetaSchemaCycleError) () string {
	return fmt.Sprintf("cycle in resolving $schema in %q", .URL)
}

// --

type MetaSchemaMismatchError struct {
	URL string
}

func ( *MetaSchemaMismatchError) () string {
	return fmt.Sprintf("$schema in %q does not match with $schema in root", .URL)
}