package dump

import (
	
	
	
)

// Options controls the dump behavior.
type Options interface {
	Output() io.Writer
	RequestHeaderOutput() io.Writer
	RequestBodyOutput() io.Writer
	ResponseHeaderOutput() io.Writer
	ResponseBodyOutput() io.Writer
	RequestHeader() bool
	RequestBody() bool
	ResponseHeader() bool
	ResponseBody() bool
	Async() bool
	Clone() Options
}

func ( *Dumper) ( io.ReadCloser) io.ReadCloser {
	return &dumpResponseBodyReadCloser{, }
}

type dumpResponseBodyReadCloser struct {
	io.ReadCloser
	dump *Dumper
}

func ( *dumpResponseBodyReadCloser) ( []byte) ( int,  error) {
	,  = .ReadCloser.Read()
	.dump.DumpResponseBody([:])
	if  == io.EOF {
		.dump.DumpDefault([]byte("\r\n"))
	}
	return
}

func ( *Dumper) ( io.WriteCloser) io.WriteCloser {
	return &dumpRequestBodyWriteCloser{, }
}

type dumpRequestBodyWriteCloser struct {
	io.WriteCloser
	dump *Dumper
}

func ( *dumpRequestBodyWriteCloser) ( []byte) ( int,  error) {
	,  = .WriteCloser.Write()
	.dump.DumpRequestBody([:])
	return
}

type dumpRequestHeaderWriter struct {
	w    io.Writer
	dump *Dumper
}

func ( *dumpRequestHeaderWriter) ( []byte) ( int,  error) {
	,  = .w.Write()
	.dump.DumpRequestHeader([:])
	return
}

func ( *Dumper) ( io.Writer) io.Writer {
	return &dumpRequestHeaderWriter{
		w:    ,
		dump: ,
	}
}

type dumpRequestBodyWriter struct {
	w    io.Writer
	dump *Dumper
}

func ( *dumpRequestBodyWriter) ( []byte) ( int,  error) {
	,  = .w.Write()
	.dump.DumpRequestBody([:])
	return
}

func ( *Dumper) ( io.Writer) io.Writer {
	return &dumpRequestBodyWriter{
		w:    ,
		dump: ,
	}
}

// GetResponseHeaderDumpers return Dumpers which need dump response header.
func ( context.Context,  *Dumper) Dumpers {
	 := GetDumpers(, )
	var  []*Dumper
	for ,  := range  {
		if .ResponseHeader() {
			 = append(, )
		}
	}
	return Dumpers()
}

// Dumpers is an array of Dumpper
type Dumpers []*Dumper

// ShouldDump is true if Dumper is not empty.
func ( Dumpers) () bool {
	return len() > 0
}

func ( Dumpers) ( []byte) {
	for ,  := range  {
		.DumpResponseHeader()
	}
}

// Dumper is the dump tool.
type Dumper struct {
	Options
	ch chan *dumpTask
}

type dumpTask struct {
	Data   []byte
	Output io.Writer
}

// NewDumper create a new Dumper.
func ( Options) *Dumper {
	 := &Dumper{
		Options: ,
		ch:      make(chan *dumpTask, 20),
	}
	return 
}

func ( *Dumper) ( Options) {
	.Options = 
}

func ( *Dumper) () *Dumper {
	if  == nil {
		return nil
	}
	return &Dumper{
		Options: .Options.Clone(),
		ch:      make(chan *dumpTask, 20),
	}
}

func ( *Dumper) ( []byte,  io.Writer) {
	if len() == 0 ||  == nil {
		return
	}
	if .Async() {
		 := make([]byte, len())
		copy(, )
		.ch <- &dumpTask{Data: , Output: }
		return
	}
	.Write()
}

func ( *Dumper) ( []byte) {
	.DumpTo(, .Output())
}

func ( *Dumper) ( []byte) {
	.DumpTo(, .RequestHeaderOutput())
}

func ( *Dumper) ( []byte) {
	.DumpTo(, .RequestBodyOutput())
}

func ( *Dumper) ( []byte) {
	.DumpTo(, .ResponseHeaderOutput())
}

func ( *Dumper) ( []byte) {
	.DumpTo(, .ResponseBodyOutput())
}

func ( *Dumper) () {
	.ch <- nil
}

func ( *Dumper) () {
	for  := range .ch {
		if  == nil {
			return
		}
		.Output.Write(.Data)
	}
}

type dumperKeyType int

const DumperKey dumperKeyType = iota

func ( context.Context,  *Dumper) []*Dumper {
	 := []*Dumper{}
	if  != nil {
		 = append(, )
	}
	if  == nil {
		return 
	}
	if ,  := .Value(DumperKey).(*Dumper);  {
		 = append(, )
	}
	return 
}

func ( *http.Response,  *http.Request,  *Dumper) {
	 := GetDumpers(.Context(), )
	for ,  := range  {
		if .ResponseBody() {
			.Body = .WrapResponseBodyReadCloser(.Body)
		}
	}
}