package parser

import (
	
	
	
)

// A HeadingConfig struct is a data structure that holds configuration of the renderers related to headings.
type HeadingConfig struct {
	AutoHeadingID bool
	Attribute     bool
}

// SetOption implements SetOptioner.
func ( *HeadingConfig) ( OptionName,  any) {
	switch  {
	case optAutoHeadingID:
		.AutoHeadingID = true
	case optAttribute:
		.Attribute = true
	}
}

// A HeadingOption interface sets options for heading parsers.
type HeadingOption interface {
	Option
	SetHeadingOption(*HeadingConfig)
}

// AutoHeadingID is an option name that enables auto IDs for headings.
const optAutoHeadingID OptionName = "AutoHeadingID"

type withAutoHeadingID struct {
}

func ( *withAutoHeadingID) ( *Config) {
	.Options[optAutoHeadingID] = true
}

func ( *withAutoHeadingID) ( *HeadingConfig) {
	.AutoHeadingID = true
}

// WithAutoHeadingID is a functional option that enables custom heading ids and
// auto generated heading ids.
func () HeadingOption {
	return &withAutoHeadingID{}
}

type withHeadingAttribute struct {
	Option
}

func ( *withHeadingAttribute) ( *HeadingConfig) {
	.Attribute = true
}

// WithHeadingAttribute is a functional option that enables custom heading attributes.
func () HeadingOption {
	return &withHeadingAttribute{WithAttribute()}
}

type atxHeadingParser struct {
	HeadingConfig
}

// NewATXHeadingParser return a new BlockParser that can parse ATX headings.
func ( ...HeadingOption) BlockParser {
	 := &atxHeadingParser{}
	for ,  := range  {
		.SetHeadingOption(&.HeadingConfig)
	}
	return 
}

func ( *atxHeadingParser) () []byte {
	return []byte{'#'}
}

func ( *atxHeadingParser) ( ast.Node,  text.Reader,  Context) (ast.Node, State) {
	,  := .PeekLine()
	 := .BlockOffset()
	if  < 0 {
		return nil, NoChildren
	}
	 := 
	for ;  < len() && [] == '#'; ++ {
	}
	 :=  - 
	if  ==  ||  > 6 {
		return nil, NoChildren
	}
	if  == len() { // alone '#' (without a new line character)
		return ast.NewHeading(), NoChildren
	}
	 := util.TrimLeftSpaceLength([:])
	if  == 0 {
		return nil, NoChildren
	}

	 := min(+, len()-1)
	 := ast.NewHeading()
	 := text.NewSegment(
		.Start+-.Padding,
		.Start+len()-.Padding)
	 = .TrimRightSpace(.Source())
	if .Len() == 0 {
		.AdvanceToEOL()
		return , NoChildren
	}

	if .Attribute {
		.Lines().Append()
		parseLastLineAttributes(, , )
		 = .Lines().At(0)
		.Lines().Clear()
	}

	// handle closing sequence of '#' characters
	 = .Value(.Source())
	 := len()
	if  == 0 { // empty headings like '##[space]'
		 = 0
	} else {
		 =  - 1
		for ; [] == '#' &&  > 0; -- {
		}
		if  == 0 && [0] == '#' { // empty headings like '### ###'
			.AdvanceToEOL()
			return , NoChildren
		}
		if  != -1 && util.IsSpace([]) {
			 = 
			 -= util.TrimRightSpaceLength([0:])
		}
	}
	.Stop = .Start + 
	.Lines().Append()
	.AdvanceToEOL()

	return , NoChildren
}

func ( *atxHeadingParser) ( ast.Node,  text.Reader,  Context) State {
	return Close
}

func ( *atxHeadingParser) ( ast.Node,  text.Reader,  Context) {
	if .AutoHeadingID {
		,  := .AttributeString("id")
		if ! {
			generateAutoHeadingID(.(*ast.Heading), , )
		} else {
			.IDs().Put(.([]byte))
		}
	}
}

func ( *atxHeadingParser) () bool {
	return true
}

func ( *atxHeadingParser) () bool {
	return false
}

func generateAutoHeadingID( *ast.Heading,  text.Reader,  Context) {
	var  []byte
	 := .Lines().Len() - 1
	if  > -1 {
		 := .Lines().At()
		 = .Value(.Source())
	}
	 := .IDs().Generate(, ast.KindHeading)
	.SetAttribute(attrNameID, )
}

func parseLastLineAttributes( ast.Node,  text.Reader,  Context) {
	 := .Lines().Len() - 1
	if  < 0 { // empty headings
		return
	}
	 := .Lines().At()
	 := .Value(.Source())
	 := text.NewReader()
	var  text.Segment
	var  int
	for {
		 := .Peek()
		if  == text.EOF ||  == '\n' {
			break
		}
		if  == '\\' {
			.Advance(1)
			if util.IsPunct(.Peek()) {
				.Advance(1)
			}
			continue
		}
		if  == '{' {
			,  = .Position()
			,  := ParseAttributes()
			if  {
				if ,  := .PeekLine();  == nil || util.IsBlank() {
					for ,  := range  {
						.SetAttribute(.Name, .Value)
					}
					.Stop = .Start + .Start
					 = .TrimRightSpace(.Source())
					.Lines().Set(, )
					return
				}
			}
			.SetPosition(, )
		}
		.Advance(1)
	}
}