package jsontext

import (
	

	
	
)

func init() {
	textcoder.Attach = attachEncoder
	textcoder.Detach = detachEncoder
	textcoder.Position = encoderPosition
	textcoder.Place = encoderPlace
	textcoder.Configure = configureEncoder
	textcoder.Fail = func( any,  error) {
		if  := &.(*Encoder).e; .attached && .failErr == nil {
			.failErr = 
		}
	}
	textcoder.Invalidate = func( any) {
		 := &.(*Encoder).e
		.invalid = true
		.vs.st = nil
	}
	textcoder.UnmarshalPlace = unmarshalPlace
	textcoder.TakeSkipper = takeSkipper
	textcoder.ReleaseSkipper = releaseSkipper
	textcoder.ValidValue = validValue
}

// encoderPosition returns the levels of enc and the offset of its next value.
func encoderPosition( any,  []textcoder.Level) ([]textcoder.Level, int64) {
	 := &.(*Encoder).e
	if !.ready() {
		.setUp()
	}
	 := &.st
	if .outer != nil {
		 = .full()
	}
	for  := range .levels {
		 := &.levels[]
		 := textcoder.Level{Object: .object, Count: .count}
		if  < len(.levels)-1 {
			.Count-- // the value of the next level, which is open, is not counted
		}
		if .object && .last >= .first {
			if .Name = .names.get(.last); .Name == nil {
				// the name "", which is a name, as a level without a name has none ( see textcoder.Level )
				.Name = []byte{}
			}
		}
		 = append(, )
	}
	, , , ,  := encoderPlace()
	return , 
}

// encoderPlace returns the innermost level of enc, whether it is the top level, the offset of its next value,
// without the levels around it, the Outer of its attach, and its depth in the whole output.
func encoderPlace( any) (textcoder.Level, bool, int64, textcoder.Outer, int) {
	 := &.(*Encoder).e
	if !.ready() {
		.setUp()
	}
	 := .st.last()
	 := KindNull
	if .needName() {
		 = KindString
	}
	 := textcoder.Level{Object: .object, Count: .count}
	return , .st.depth() == 0, .base + int64(len(.buf)) + int64(.delimLen()), .st.outer, .st.depth() + .st.base
}

// configureEncoder makes enc write by the options opts until the returned function is called, with the strings of
// the values as they are: they were escaped by the options of the call which wrote them.
func configureEncoder( any,  options.Options) func() {
	 := &.(*Encoder).e
	 := .cfg
	.cfg = config{}
	.ApplyTo(&.cfg)
	.cfg.Value = .cfg.Value&^(escapeForHTML|escapeForJS) | preserveRawStrings
	.derive()
	return func() {
		.cfg = 
		.derive()
	}
}

// attachEncoder makes the Encoder enc write after out, at the place whose innermost level is inner ( see
// textcoder.Attach ).
func attachEncoder( unsafe.Pointer,  *textcoder.Attachment) {
	 := &(*Encoder)().e
	 := &.st
	.closedPast, .failErr = false, nil
	if .Same && .attached && .ready() && cap(.levels) >= 2 {
		// attached again with the same options, as most calls of the methods are: the state of the grammar and of
		// the scanner is reset, as init resets it, without the rest. The levels are written as reset and push
		// write them, the top level and the place, which no name of an object is given for.
		.invalid = false
		if .Top {
			.levels = .levels[:1]
			.levels[0] = level{count: .Count}
		} else {
			.levels = .levels[:2]
			.levels[0] = level{count: 1}
			.levels[1] = level{object: .Object, count: .Count, last: -1}
		}
		if  := &.names; len(.ends) != 0 || len(.indexes) != 0 {
			.reset()
		}
		 := &.vs
		.write, .keep = true, false
		.run = 0
		.wsFrom, .wsEnd = 0, 0
		if .in != nil || .out != nil {
			.in, .out = nil, nil
		}
	} else {
		.cfg = *.Opts
		.w, .attached = nil, false // which init doesn't derive again for
		.init(nil)
		.attached = true
		// compact, as derive makes it for an attached encoder: init derived it before the encoder was attached.
		.vs.ws, .vs.spaced = whitespace{}, false
		if !.Top {
			_ = .push(.Object)
		}
		// the names of an object, which the value written at its place can't have, are not given.
		.last().count = .Count
		.outer = .Outer
	}
	.outerDepth, .outerCount = len(.levels)-1, .Count
	.base = .OuterDepth - .outerDepth
	.setOutput(.Out, .Base)
	.Skip, .Depth = 0, len(.levels)-1
	if len(.Out) == 0 && !.Top {
		.Skip = .skipDelim()
	}
}

// setOutput sets the output of an attached encoder to out, at the offset base of the whole output. The pointers are
// written only if they change, as a write of a pointer costs a write barrier while the GC marks: the output is
// mostly the same array, whose length is set by a slice of itself.
func ( *encoder) ( []byte,  int64) {
	if unsafe.SliceData(.buf) == unsafe.SliceData() && cap(.buf) == cap() {
		.buf = .buf[:len()]
	} else {
		.buf = 
	}
	.base, .maxValue = , 0
}

// skipDelim returns the length of the delimiter which the attached encoder writes before the value at the start
// of its output, which isn't a part of it ( see textcoder.Attach ), and moves the offset of the output before it.
func ( *encoder) () int {
	 := KindNull
	if .st.last().needName() {
		 = KindString
	}
	 := .delimLen()
	.base -= int64()
	return 
}

// detachEncoder sets the output of enc and the place where it is in p.
func detachEncoder( unsafe.Pointer,  *textcoder.Attachment) {
	 := &(*Encoder)().e
	.Out, .Depth, .Count, .Failed = .buf, .st.depth(), .st.last().count, .failErr
	.failErr = nil
	if .closedPast {
		.Depth-- // the level which the value was asked to end, as encoding/json/jsontext ends it
	}
}