package jsonschema

import (
	
	
	
)

// Compiler compiles json schema into *Schema.
type Compiler struct {
	schemas       map[urlPtr]*Schema
	roots         *roots
	formats       map[string]*Format
	decoders      map[string]*Decoder
	mediaTypes    map[string]*MediaType
	assertFormat  bool
	assertContent bool
}

// NewCompiler create Compiler Object.
func () *Compiler {
	return &Compiler{
		schemas:       map[urlPtr]*Schema{},
		roots:         newRoots(),
		formats:       map[string]*Format{},
		decoders:      map[string]*Decoder{},
		mediaTypes:    map[string]*MediaType{},
		assertFormat:  false,
		assertContent: false,
	}
}

// DefaultDraft overrides the draft used to
// compile schemas without `$schema` field.
//
// By default, this library uses the latest
// draft supported.
//
// The use of this option is HIGHLY encouraged
// to ensure continued correct operation of your
// schema. The current default value will not stay
// the same overtime.
func ( *Compiler) ( *Draft) {
	.roots.defaultDraft = 
}

// AssertFormat always enables format assertions.
//
// Default Behavior:
// for draft-07: enabled.
// for draft/2019-09: disabled unless metaschema says `format` vocabulary is required.
// for draft/2020-12: disabled unless metaschema says `format-assertion` vocabulary is required.
func ( *Compiler) () {
	.assertFormat = true
}

// AssertContent enables content assertions.
//
// Content assertions include keywords:
//   - contentEncoding
//   - contentMediaType
//   - contentSchema
//
// Default behavior is always disabled.
func ( *Compiler) () {
	.assertContent = true
}

// RegisterFormat registers custom format.
//
// NOTE:
//   - "regex" format can not be overridden
//   - format assertions are disabled for draft >= 2019-09
//     see [Compiler.AssertFormat]
func ( *Compiler) ( *Format) {
	if .Name != "regex" {
		.formats[.Name] = 
	}
}

// RegisterContentEncoding registers custom contentEncoding.
//
// NOTE: content assertions are disabled by default.
// see [Compiler.AssertContent].
func ( *Compiler) ( *Decoder) {
	.decoders[.Name] = 
}

// RegisterContentMediaType registers custom contentMediaType.
//
// NOTE: content assertions are disabled by default.
// see [Compiler.AssertContent].
func ( *Compiler) ( *MediaType) {
	.mediaTypes[.Name] = 
}

// RegisterVocabulary registers custom vocabulary.
//
// NOTE:
//   - vocabularies are disabled for draft >= 2019-09
//     see [Compiler.AssertVocabs]
func ( *Compiler) ( *Vocabulary) {
	.roots.vocabularies[.URL] = 
}

// AssertVocabs always enables user-defined vocabularies assertions.
//
// Default Behavior:
// for draft-07: enabled.
// for draft/2019-09: disabled unless metaschema enables a vocabulary.
// for draft/2020-12: disabled unless metaschema enables a vocabulary.
func ( *Compiler) () {
	.roots.assertVocabs = true
}

// AddResource adds schema resource which gets used later in reference
// resolution.
//
// The argument url can be file path or url. Any fragment in url is ignored.
// The argument doc must be valid json value.
func ( *Compiler) ( string,  any) error {
	,  := absolute()
	if  != nil {
		return 
	}
	if isMeta(string(.url)) {
		return &ResourceExistsError{string(.url)}
	}
	if !.roots.loader.add(.url, ) {
		return &ResourceExistsError{string(.url)}
	}
	return nil
}

// UseLoader overrides the default [URLLoader] used
// to load schema resources.
func ( *Compiler) ( URLLoader) {
	.roots.loader.loader = 
}

// UseRegexpEngine changes the regexp-engine used.
// By default it uses regexp package from go standard
// library.
//
// NOTE: must be called before compiling any schemas.
func ( *Compiler) ( RegexpEngine) {
	if  == nil {
		 = goRegexpCompile
	}
	.roots.regexpEngine = 
}

func ( *Compiler) ( *queue,  urlPtr) *Schema {
	if ,  := .schemas[];  {
		// already got compiled
		return 
	}
	if  := .get();  != nil {
		return 
	}
	 := newSchema()
	.append()
	return 
}

// MustCompile is like [Compile] but panics if compilation fails.
// It simplifies safe initialization of global variables holding
// compiled schema.
func ( *Compiler) ( string) *Schema {
	,  := .Compile()
	if  != nil {
		panic(fmt.Sprintf("jsonschema: Compile(%q): %v", , ))
	}
	return 
}

// Compile compiles json-schema at given loc.
func ( *Compiler) ( string) (*Schema, error) {
	,  := absolute()
	if  != nil {
		return nil, 
	}
	,  := .roots.resolveFragment(*)
	if  != nil {
		return nil, 
	}
	return .doCompile()
}

func ( *Compiler) ( urlPtr) (*Schema, error) {
	 := &queue{}
	 := 0

	.enqueue(, )
	for .len() >  {
		 := .at()
		if  := .roots.ensureSubschema(.up);  != nil {
			return nil, 
		}
		 := .roots.roots[.up.url]
		,  := .up.lookup(.doc)
		if  != nil {
			return nil, 
		}
		if  := .compileValue(, , , );  != nil {
			return nil, 
		}
		++
	}
	for ,  := range * {
		.schemas[.up] = 
	}
	return .schemas[], nil
}

func ( *Compiler) ( any,  *Schema,  *root,  *queue) error {
	 := .resource(.up.ptr)
	.DraftVersion = .dialect.draft.version

	 := urlPtr{.up.url, .ptr}
	.resource = .enqueue(, )

	// if resource, enqueue dynamic anchors for compilation
	if .DraftVersion >= 2020 && .up == .resource.up {
		 := .resource(.up.ptr)
		for ,  := range .anchors {
			if slices.Contains(.dynamicAnchors, ) {
				 := urlPtr{.up.url, }
				 := .enqueue(, )
				if .dynamicAnchors == nil {
					.dynamicAnchors = map[string]*Schema{}
				}
				.dynamicAnchors[string()] = 
			}
		}
	}

	switch v := .(type) {
	case bool:
		.Bool = &
	case map[string]any:
		if  := .compileObject(, , , );  != nil {
			return 
		}
	}

	.allPropsEvaluated = .AdditionalProperties != nil
	if .DraftVersion < 2020 {
		.allItemsEvaluated = .AdditionalItems != nil
		switch items := .Items.(type) {
		case *Schema:
			.allItemsEvaluated = true
		case []*Schema:
			.numItemsEvaluated = len()
		}
	} else {
		.allItemsEvaluated = .Items2020 != nil
		.numItemsEvaluated = len(.PrefixItems)
	}

	return nil
}

func ( *Compiler) ( map[string]any,  *Schema,  *root,  *queue) error {
	if len() == 0 {
		 := true
		.Bool = &
		return nil
	}
	 := objCompiler{
		c:   ,
		obj: ,
		up:  .up,
		r:   ,
		res: .resource(.up.ptr),
		q:   ,
	}
	return .compile()
}

// queue --

type queue []*Schema

func ( *queue) ( *Schema) {
	* = append(*, )
}

func ( *queue) ( int) *Schema {
	return (*)[]
}

func ( *queue) () int {
	return len(*)
}

func ( *queue) ( urlPtr) *Schema {
	 := slices.IndexFunc(*, func( *Schema) bool { return .up ==  })
	if  != -1 {
		return (*)[]
	}
	return nil
}

// regexp --

// Regexp is the representation of compiled regular expression.
type Regexp interface {
	fmt.Stringer

	// MatchString reports whether the string s contains
	// any match of the regular expression.
	MatchString(string) bool
}

// RegexpEngine parses a regular expression and returns,
// if successful, a Regexp object that can be used to
// match against text.
type RegexpEngine func(string) (Regexp, error)

func ( RegexpEngine) ( any) error {
	,  := .(string)
	if ! {
		return nil
	}
	,  := ()
	return 
}

func goRegexpCompile( string) (Regexp, error) {
	return regexp.Compile()
}