package jsonschema

import (
	
	
	
	
)

type objCompiler struct {
	c   *Compiler
	obj map[string]any
	up  urlPtr
	r   *root
	res *resource
	q   *queue
}

func ( *objCompiler) ( *Schema) error {
	// id --
	if  := .res.dialect.draft.getID(.obj);  != "" {
		.ID = 
	}

	// anchor --
	if .DraftVersion < 2019 {
		// anchor is specified in id
		 := .string(.res.dialect.draft.id)
		if  != "" {
			,  := split()
			if  != "" {
				var  error
				.Anchor,  = decode()
				if  != nil {
					return &ParseAnchorError{URL: .Location}
				}
			}
		}
	} else {
		.Anchor = .string("$anchor")
	}

	if  := .compileDraft4();  != nil {
		return 
	}
	if .DraftVersion >= 6 {
		if  := .compileDraft6();  != nil {
			return 
		}
	}
	if .DraftVersion >= 7 {
		if  := .compileDraft7();  != nil {
			return 
		}
	}
	if .DraftVersion >= 2019 {
		if  := .compileDraft2019();  != nil {
			return 
		}
	}
	if .DraftVersion >= 2020 {
		if  := .compileDraft2020();  != nil {
			return 
		}
	}

	// vocabularies
	 := .res.dialect.activeVocabs(.c.roots.assertVocabs, .c.roots.vocabularies)
	for ,  := range  {
		 := .c.roots.vocabularies[]
		if  == nil {
			continue
		}
		,  := .Compile(&CompilerContext{}, .obj)
		if  != nil {
			return 
		}
		if  != nil {
			.Extensions = append(.Extensions, )
		}
	}

	return nil
}

func ( *objCompiler) ( *Schema) error {
	var  error

	if .hasVocab("core") {
		if .Ref,  = .enqueueRef("$ref");  != nil {
			return 
		}
		if .DraftVersion < 2019 && .Ref != nil {
			// All other properties in a "$ref" object MUST be ignored
			return nil
		}
	}

	if .hasVocab("applicator") {
		.AllOf = .enqueueArr("allOf")
		.AnyOf = .enqueueArr("anyOf")
		.OneOf = .enqueueArr("oneOf")
		.Not = .enqueueProp("not")

		if .DraftVersion < 2020 {
			if ,  := .obj["items"];  {
				if ,  := .([]any);  {
					.Items = .enqueueArr("items")
					.AdditionalItems = .enqueueAdditional("additionalItems")
				} else {
					.Items = .enqueueProp("items")
				}
			}
		}

		.Properties = .enqueueMap("properties")
		if  := .enqueueMap("patternProperties");  != nil {
			.PatternProperties = map[Regexp]*Schema{}
			for ,  := range  {
				,  := .c.roots.regexpEngine()
				if  != nil {
					return &InvalidRegexError{.up.format("patternProperties"), , }
				}
				.PatternProperties[] = 
			}
		}
		.AdditionalProperties = .enqueueAdditional("additionalProperties")

		if  := .objVal("dependencies");  != nil {
			.Dependencies = map[string]any{}
			for ,  := range  {
				if ,  := .([]any);  {
					.Dependencies[] = toStrings()
				} else {
					 := .up.ptr.append2("dependencies", )
					.Dependencies[] = .enqueuePtr()
				}
			}
		}
	}

	if .hasVocab("validation") {
		if ,  := .obj["type"];  {
			.Types = newTypes()
		}
		if  := .arrVal("enum");  != nil {
			.Enum = newEnum()
		}
		.MultipleOf = .numVal("multipleOf")
		.Maximum = .numVal("maximum")
		if .boolean("exclusiveMaximum") {
			.ExclusiveMaximum = .Maximum
			.Maximum = nil
		} else {
			.ExclusiveMaximum = .numVal("exclusiveMaximum")
		}
		.Minimum = .numVal("minimum")
		if .boolean("exclusiveMinimum") {
			.ExclusiveMinimum = .Minimum
			.Minimum = nil
		} else {
			.ExclusiveMinimum = .numVal("exclusiveMinimum")
		}

		.MinLength = .intVal("minLength")
		.MaxLength = .intVal("maxLength")
		if  := .strVal("pattern");  != nil {
			.Pattern,  = .c.roots.regexpEngine(*)
			if  != nil {
				return &InvalidRegexError{.up.format("pattern"), *, }
			}
		}

		.MinItems = .intVal("minItems")
		.MaxItems = .intVal("maxItems")
		.UniqueItems = .boolean("uniqueItems")

		.MaxProperties = .intVal("maxProperties")
		.MinProperties = .intVal("minProperties")
		if  := .arrVal("required");  != nil {
			.Required = toStrings()
		}
	}

	// format --
	if .assertFormat(.DraftVersion) {
		if  := .strVal("format");  != nil {
			if * == "regex" {
				.Format = &Format{
					Name:     "regex",
					Validate: .c.roots.regexpEngine.validate,
				}
			} else {
				.Format = .c.formats[*]
				if .Format == nil {
					.Format = formats[*]
				}
			}
		}
	}

	// annotations --
	.Title = .string("title")
	.Description = .string("description")
	if ,  := .obj["default"];  {
		.Default = &
	}

	return nil
}

func ( *objCompiler) ( *Schema) error {
	if .hasVocab("applicator") {
		.Contains = .enqueueProp("contains")
		.PropertyNames = .enqueueProp("propertyNames")
	}
	if .hasVocab("validation") {
		if ,  := .obj["const"];  {
			.Const = &
		}
	}
	return nil
}

func ( *objCompiler) ( *Schema) error {
	if .hasVocab("applicator") {
		.If = .enqueueProp("if")
		if .If != nil {
			 := .boolVal("if")
			if  == nil || * {
				.Then = .enqueueProp("then")
			}
			if  == nil || !* {
				.Else = .enqueueProp("else")
			}
		}
	}

	if .c.assertContent {
		if  := .strVal("contentEncoding");  != nil {
			.ContentEncoding = .c.decoders[*]
			if .ContentEncoding == nil {
				.ContentEncoding = decoders[*]
			}
		}
		if  := .strVal("contentMediaType");  != nil {
			.ContentMediaType = .c.mediaTypes[*]
			if .ContentMediaType == nil {
				.ContentMediaType = mediaTypes[*]
			}
		}
	}

	// annotations --
	.Comment = .string("$comment")
	.ReadOnly = .boolean("readOnly")
	.WriteOnly = .boolean("writeOnly")
	if ,  := .obj["examples"].([]any);  {
		.Examples = 
	}

	return nil
}

func ( *objCompiler) ( *Schema) error {
	var  error

	if .hasVocab("core") {
		if .RecursiveRef,  = .enqueueRef("$recursiveRef");  != nil {
			return 
		}
		.RecursiveAnchor = .boolean("$recursiveAnchor")
	}

	if .hasVocab("validation") {
		if .Contains != nil {
			.MinContains = .intVal("minContains")
			.MaxContains = .intVal("maxContains")
		}
		if  := .objVal("dependentRequired");  != nil {
			.DependentRequired = map[string][]string{}
			for ,  := range  {
				if ,  := .([]any);  {
					.DependentRequired[] = toStrings()
				}
			}
		}
	}

	if .hasVocab("applicator") {
		.DependentSchemas = .enqueueMap("dependentSchemas")
	}

	var  bool
	if .DraftVersion == 2019 {
		 = .hasVocab("applicator")
	} else {
		 = .hasVocab("unevaluated")
	}
	if  {
		.UnevaluatedItems = .enqueueProp("unevaluatedItems")
		.UnevaluatedProperties = .enqueueProp("unevaluatedProperties")
	}

	if .c.assertContent {
		if .ContentMediaType != nil && .ContentMediaType.UnmarshalJSON != nil {
			.ContentSchema = .enqueueProp("contentSchema")
		}
	}

	// annotations --
	.Deprecated = .boolean("deprecated")

	return nil
}

func ( *objCompiler) ( *Schema) error {
	if .hasVocab("core") {
		,  := .enqueueRef("$dynamicRef")
		if  != nil {
			return 
		}
		if  != nil {
			 := .strVal("$dynamicRef")
			, ,  := splitFragment(*)
			if  != nil {
				return 
			}
			var  string
			if ,  := .convert().(anchor);  {
				 = string()
			}
			.DynamicRef = &DynamicRef{, }
		}
		.DynamicAnchor = .string("$dynamicAnchor")
	}

	if .hasVocab("applicator") {
		.PrefixItems = .enqueueArr("prefixItems")
		.Items2020 = .enqueueProp("items")
	}

	return nil
}

// enqueue helpers --

func ( *objCompiler) ( jsonPointer) *Schema {
	 := urlPtr{.up.url, }
	return .c.enqueue(.q, )
}

func ( *objCompiler) ( string) (*Schema, error) {
	 := .strVal()
	if  == nil {
		return nil, nil
	}
	return .enqueueRefVal(*)
}

func ( *objCompiler) ( string) (*Schema, error) {
	 := .res.id
	// baseURL := c.r.baseURL(c.up.ptr)
	,  := .join()
	if  != nil {
		return nil, 
	}

	,  := .r.resolve(*)
	if  != nil {
		return nil, 
	}
	if  != nil {
		// local ref
		return .enqueuePtr(.ptr), nil
	}

	// remote ref
	,  := .c.roots.resolveFragment(*)
	if  != nil {
		return nil, 
	}
	return .c.enqueue(.q, ), nil
}

func ( *objCompiler) ( string) *Schema {
	if ,  := .obj[]; ! {
		return nil
	}
	 := .up.ptr.append()
	return .enqueuePtr()
}

func ( *objCompiler) ( string) []*Schema {
	 := .arrVal()
	if  == nil {
		return nil
	}
	 := make([]*Schema, len())
	for  := range  {
		 := .up.ptr.append2(, strconv.Itoa())
		[] = .enqueuePtr()
	}
	return 
}

func ( *objCompiler) ( string) map[string]*Schema {
	 := .objVal()
	if  == nil {
		return nil
	}
	 := make(map[string]*Schema)
	for  := range  {
		 := .up.ptr.append2(, )
		[] = .enqueuePtr()
	}
	return 
}

func ( *objCompiler) ( string) any {
	if  := .boolVal();  != nil {
		return *
	}
	if  := .enqueueProp();  != nil {
		return 
	}
	return nil
}

// --

func ( *objCompiler) ( string) bool {
	return .res.dialect.hasVocab()
}

func ( *objCompiler) ( int) bool {
	if .c.assertFormat ||  < 2019 {
		return true
	}
	if  == 2019 {
		return .hasVocab("format")
	} else {
		return .hasVocab("format-assertion")
	}
}

// value helpers --

func ( *objCompiler) ( string) *bool {
	,  := .obj[]
	if ! {
		return nil
	}
	,  := .(bool)
	if ! {
		return nil
	}
	return &
}

func ( *objCompiler) ( string) bool {
	 := .boolVal()
	return  != nil && *
}

func ( *objCompiler) ( string) *string {
	,  := .obj[]
	if ! {
		return nil
	}
	,  := .(string)
	if ! {
		return nil
	}
	return &
}

func ( *objCompiler) ( string) string {
	if  := .strVal();  != nil {
		return *
	}
	return ""
}

func ( *objCompiler) ( string) *big.Rat {
	,  := .obj[]
	if ! {
		return nil
	}
	switch .(type) {
	case json.Number, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		if ,  := new(big.Rat).SetString(fmt.Sprint());  {
			return 
		}
	}
	return nil
}

func ( *objCompiler) ( string) *int {
	if  := .numVal();  != nil && .IsInt() {
		 := int(.Num().Int64())
		return &
	}
	return nil
}

func ( *objCompiler) ( string) map[string]any {
	,  := .obj[]
	if ! {
		return nil
	}
	,  := .(map[string]any)
	if ! {
		return nil
	}
	return 
}

func ( *objCompiler) ( string) []any {
	,  := .obj[]
	if ! {
		return nil
	}
	,  := .([]any)
	if ! {
		return nil
	}
	return 
}

// --

type InvalidRegexError struct {
	URL   string
	Regex string
	Err   error
}

func ( *InvalidRegexError) () string {
	return fmt.Sprintf("invalid regex %q at %q: %v", .Regex, .URL, .Err)
}

// --

func toStrings( []any) []string {
	var  []string
	for ,  := range  {
		if ,  := .(string);  {
			 = append(, )
		}
	}
	return 
}