package decoder

import (
	
	
	
	
	

	
	
	
	
)

// The UnmarshalJSONFrom methods which the decoder of v1 calls, before UnmarshalJSON: the one which takes the Decoder
// of github.com/goccy/go-json/jsontext, which go-json replaces encoding/json/jsontext by, first, and the one which
// takes the Decoder of encoding/json/jsontext, which encoding/json of Go 1.27 calls ( see stdUnmarshalerFromType ).
// The value is read whole, as for UnmarshalJSON, and the method reads it from a decoder of its own. A method which
// declines, by errors.ErrUnsupported, leaves the value to the decoder after it, as encoding/json/v2 documents it.
// A key of a map is not decoded by the method, as encoding/json doesn't ( see compileMapKey ).

// unmarshalerFromType is the interface of UnmarshalJSONFrom with the Decoder of github.com/goccy/go-json/jsontext.
var unmarshalerFromType = reflect.TypeFor[unmarshalerFrom]()

type unmarshalerFrom interface {
	UnmarshalJSONFrom(*jsontext.Decoder) error
}

// implementsUnmarshalFrom reports whether the decoder of v1 calls UnmarshalJSONFrom of a value of the type: not of a
// json.Number, which has the one of encoding/json/jsontext from Go 1.27, and which the decoder decodes by itself, with
// the options and at the places which the method looks at.
func implementsUnmarshalFrom( reflect.Type) bool {
	return  != reflect.PointerTo(jsonNumberType) &&
		(.Implements(unmarshalerFromType) || stdUnmarshalerFromType != nil && .Implements(stdUnmarshalerFromType))
}

// fromDecoder is a decoder of jsontext which an UnmarshalJSONFrom method reads from, and the reader of its input.
type fromDecoder struct {
	r   bytes.Reader
	dec *jsontext.Decoder
}

var fromDecoders = sync.Pool{New: func() any {
	 := new(fromDecoder)
	.dec = jsontext.NewDecoder(&.r)
	return 
}}

// fromOptions are the options of the decoder which UnmarshalJSONFrom of github.com/goccy/go-json/jsontext reads from,
// made of the options of the decoding: the ones of encoding/json, which DefaultOptionsV1 of the root package sets,
// and RejectUnknownMembers and UnmarshalAnyWithRawNumber for DisallowUnknownFieldsOption and UseNumberOption, which a
// Decoder of encoding/json adds for DisallowUnknownFields and UseNumber, as encoding/json gives its methods, by which
// UnmarshalDecode of v2 within the method decodes as the decoder of v1 does.
type fromOptions OptionFlags

func ( fromOptions) ( *options.Config) {
	 := options.DefaultV1Flags
	if OptionFlags()&DisallowUnknownFieldsOption != 0 {
		 |= options.RejectUnknownMembers
	}
	if OptionFlags()&UseNumberOption != 0 {
		 |= options.UnmarshalAnyWithRawNumber
	}
	.Set |= 
	.Value |= 
}

// unmarshalFrom calls UnmarshalJSONFrom of m with a decoder of the value, by the options of the decoding flags ( see
// fromOptions ), and returns its error, and whether it declined.
func unmarshalFrom( unmarshalerFrom,  []byte,  OptionFlags) (bool, error) {
	 := fromDecoders.Get().(*fromDecoder)
	defer fromDecoders.Put()
	.r.Reset()
	.dec.Reset(&.r, fromOptions())
	 := .UnmarshalJSONFrom(.dec)
	,  := .dec.StackIndex(0)
	,  := errors.MethodCallResult(, .dec.StackDepth(), )
	// the pool keeps no reference to the input, only the copy which the decoder read into its buffer
	.r.Reset(nil)
	return , 
}

// unmarshalFromDecoder decodes a value by UnmarshalJSONFrom of the pointer type typ, and by next where the method
// declines.
type unmarshalFromDecoder struct {
	typ  reflect.Type
	from func(v any, value []byte, flags OptionFlags) (bool, error)
	next Decoder
}

func newUnmarshalFromDecoder( reflect.Type,  Decoder) *unmarshalFromDecoder {
	// the method of encoding/json/jsontext, which implementsUnmarshalFrom found, if the type has not the one of
	// github.com/goccy/go-json/jsontext
	 := stdUnmarshalFrom
	if .Implements(unmarshalerFromType) {
		 = func( any,  []byte,  OptionFlags) (bool, error) {
			return unmarshalFrom(.(unmarshalerFrom), , )
		}
	}
	return &unmarshalFromDecoder{typ: , from: , next: }
}

func ( *unmarshalFromDecoder) ( *RuntimeContext, ,  int64,  unsafe.Pointer) (int64, error) {
	 := .Buf
	 := skipWhiteSpace(, )
	,  := skipValue(, , )
	if  != nil {
		return 0, 
	}
	 := *(*any)(unsafe.Pointer(&emptyInterface{
		typ: runtime.TypePtr(.typ),
		ptr: ,
	}))
	,  := .from(, [:], .Option.Flags)
	if  {
		return .next.Decode(, , , )
	}
	if  != nil {
		.methodError()
	}
	return , nil
}

func ( *unmarshalFromDecoder) ( *RuntimeContext, ,  int64) ([][]byte, int64, error) {
	return nil, 0, fmt.Errorf("json: unmarshal json from decoder does not support decode path")
}