package ast

import (
	
	

	textm 
	
)

// A BaseInline struct implements the Node interface partialliy.
type BaseInline struct {
	BaseNode
}

// Type implements Node.Type.
func ( *BaseInline) () NodeType {
	return TypeInline
}

// IsRaw implements Node.IsRaw.
func ( *BaseInline) () bool {
	return false
}

// HasBlankPreviousLines implements Node.HasBlankPreviousLines.
func ( *BaseInline) () bool {
	panic("can not call with inline nodes.")
}

// SetBlankPreviousLines implements Node.SetBlankPreviousLines.
func ( *BaseInline) ( bool) {
	panic("can not call with inline nodes.")
}

// Lines implements Node.Lines.
func ( *BaseInline) () *textm.Segments {
	panic("can not call with inline nodes.")
}

// SetLines implements Node.SetLines.
func ( *BaseInline) ( *textm.Segments) {
	panic("can not call with inline nodes.")
}

// A Text struct represents a textual content of the Markdown text.
type Text struct {
	BaseInline
	// Segment is a position in a source text.
	Segment textm.Segment

	flags uint8
}

const (
	textSoftLineBreak = 1 << iota
	textHardLineBreak
	textRaw
	textCode
)

func textFlagsString( uint8) string {
	 := []string{}
	if &textSoftLineBreak != 0 {
		 = append(, "SoftLineBreak")
	}
	if &textHardLineBreak != 0 {
		 = append(, "HardLineBreak")
	}
	if &textRaw != 0 {
		 = append(, "Raw")
	}
	if &textCode != 0 {
		 = append(, "Code")
	}
	return strings.Join(, ", ")
}

// Inline implements Inline.Inline.
func ( *Text) () {
}

// SoftLineBreak returns true if this node ends with a new line,
// otherwise false.
func ( *Text) () bool {
	return .flags&textSoftLineBreak != 0
}

// SetSoftLineBreak sets whether this node ends with a new line.
func ( *Text) ( bool) {
	if  {
		.flags |= textSoftLineBreak
	} else {
		.flags = .flags &^ textSoftLineBreak
	}
}

// IsRaw returns true if this text should be rendered without unescaping
// back slash escapes and resolving references.
func ( *Text) () bool {
	return .flags&textRaw != 0
}

// SetRaw sets whether this text should be rendered as raw contents.
func ( *Text) ( bool) {
	if  {
		.flags |= textRaw
	} else {
		.flags = .flags &^ textRaw
	}
}

// HardLineBreak returns true if this node ends with a hard line break.
// See https://spec.commonmark.org/0.30/#hard-line-breaks for details.
func ( *Text) () bool {
	return .flags&textHardLineBreak != 0
}

// SetHardLineBreak sets whether this node ends with a hard line break.
func ( *Text) ( bool) {
	if  {
		.flags |= textHardLineBreak
	} else {
		.flags = .flags &^ textHardLineBreak
	}
}

// Merge merges a Node n into this node.
// Merge returns true if the given node has been merged, otherwise false.
func ( *Text) ( Node,  []byte) bool {
	,  := .(*Text)
	if ! {
		return false
	}
	if .Segment.Stop != .Segment.Start || .Segment.Padding != 0 ||
		[.Segment.Stop-1] == '\n' || .IsRaw() != .IsRaw() {
		return false
	}
	.Segment.Stop = .Segment.Stop
	.SetSoftLineBreak(.SoftLineBreak())
	.SetHardLineBreak(.HardLineBreak())
	return true
}

// Text implements Node.Text.
func ( *Text) ( []byte) []byte {
	return .Segment.Value()
}

// Dump implements Node.Dump.
func ( *Text) ( []byte,  int) {
	 := textFlagsString(.flags)
	if len() != 0 {
		 = "(" +  + ")"
	}
	fmt.Printf("%sText%s: \"%s\"\n", strings.Repeat("    ", ), , strings.TrimRight(string(.Text()), "\n"))
}

// KindText is a NodeKind of the Text node.
var KindText = NewNodeKind("Text")

// Kind implements Node.Kind.
func ( *Text) () NodeKind {
	return KindText
}

// NewText returns a new Text node.
func () *Text {
	return &Text{
		BaseInline: BaseInline{},
	}
}

// NewTextSegment returns a new Text node with the given source position.
func ( textm.Segment) *Text {
	return &Text{
		BaseInline: BaseInline{},
		Segment:    ,
	}
}

// NewRawTextSegment returns a new Text node with the given source position.
// The new node should be rendered as raw contents.
func ( textm.Segment) *Text {
	 := &Text{
		BaseInline: BaseInline{},
		Segment:    ,
	}
	.SetRaw(true)
	return 
}

// MergeOrAppendTextSegment merges a given s into the last child of the parent if
// it can be merged, otherwise creates a new Text node and appends it to after current
// last child.
func ( Node,  textm.Segment) {
	 := .LastChild()
	,  := .(*Text)
	if  && .Segment.Stop == .Start && !.SoftLineBreak() {
		.Segment = .Segment.WithStop(.Stop)
	} else {
		.AppendChild(, NewTextSegment())
	}
}

// MergeOrReplaceTextSegment merges a given s into a previous sibling of the node n
// if a previous sibling of the node n is *Text, otherwise replaces Node n with s.
func ( Node,  Node,  textm.Segment) {
	 := .PreviousSibling()
	if ,  := .(*Text);  && .Segment.Stop == .Start && !.SoftLineBreak() {
		.Segment = .Segment.WithStop(.Stop)
		.RemoveChild(, )
	} else {
		.ReplaceChild(, , NewTextSegment())
	}
}

// A String struct is a textual content that has a concrete value.
type String struct {
	BaseInline

	Value []byte
	flags uint8
}

// Inline implements Inline.Inline.
func ( *String) () {
}

// IsRaw returns true if this text should be rendered without unescaping
// back slash escapes and resolving references.
func ( *String) () bool {
	return .flags&textRaw != 0
}

// SetRaw sets whether this text should be rendered as raw contents.
func ( *String) ( bool) {
	if  {
		.flags |= textRaw
	} else {
		.flags = .flags &^ textRaw
	}
}

// IsCode returns true if this text should be rendered without any
// modifications.
func ( *String) () bool {
	return .flags&textCode != 0
}

// SetCode sets whether this text should be rendered without any modifications.
func ( *String) ( bool) {
	if  {
		.flags |= textCode
	} else {
		.flags = .flags &^ textCode
	}
}

// Text implements Node.Text.
func ( *String) ( []byte) []byte {
	return .Value
}

// Dump implements Node.Dump.
func ( *String) ( []byte,  int) {
	 := textFlagsString(.flags)
	if len() != 0 {
		 = "(" +  + ")"
	}
	fmt.Printf("%sString%s: \"%s\"\n", strings.Repeat("    ", ), , strings.TrimRight(string(.Value), "\n"))
}

// KindString is a NodeKind of the String node.
var KindString = NewNodeKind("String")

// Kind implements Node.Kind.
func ( *String) () NodeKind {
	return KindString
}

// NewString returns a new String node.
func ( []byte) *String {
	return &String{
		Value: ,
	}
}

// A CodeSpan struct represents a code span of Markdown text.
type CodeSpan struct {
	BaseInline
}

// Inline implements Inline.Inline .
func ( *CodeSpan) () {
}

// IsBlank returns true if this node consists of spaces, otherwise false.
func ( *CodeSpan) ( []byte) bool {
	for  := .FirstChild();  != nil;  = .NextSibling() {
		 := .(*Text).Segment
		if !util.IsBlank(.Value()) {
			return false
		}
	}
	return true
}

// Dump implements Node.Dump.
func ( *CodeSpan) ( []byte,  int) {
	DumpHelper(, , , nil, nil)
}

// KindCodeSpan is a NodeKind of the CodeSpan node.
var KindCodeSpan = NewNodeKind("CodeSpan")

// Kind implements Node.Kind.
func ( *CodeSpan) () NodeKind {
	return KindCodeSpan
}

// NewCodeSpan returns a new CodeSpan node.
func () *CodeSpan {
	return &CodeSpan{
		BaseInline: BaseInline{},
	}
}

// An Emphasis struct represents an emphasis of Markdown text.
type Emphasis struct {
	BaseInline

	// Level is a level of the emphasis.
	Level int
}

// Dump implements Node.Dump.
func ( *Emphasis) ( []byte,  int) {
	 := map[string]string{
		"Level": fmt.Sprintf("%v", .Level),
	}
	DumpHelper(, , , , nil)
}

// KindEmphasis is a NodeKind of the Emphasis node.
var KindEmphasis = NewNodeKind("Emphasis")

// Kind implements Node.Kind.
func ( *Emphasis) () NodeKind {
	return KindEmphasis
}

// NewEmphasis returns a new Emphasis node with the given level.
func ( int) *Emphasis {
	return &Emphasis{
		BaseInline: BaseInline{},
		Level:      ,
	}
}

type baseLink struct {
	BaseInline

	// Destination is a destination(URL) of this link.
	Destination []byte

	// Title is a title of this link.
	Title []byte
}

// Inline implements Inline.Inline.
func ( *baseLink) () {
}

// A Link struct represents a link of the Markdown text.
type Link struct {
	baseLink
}

// Dump implements Node.Dump.
func ( *Link) ( []byte,  int) {
	 := map[string]string{}
	["Destination"] = string(.Destination)
	["Title"] = string(.Title)
	DumpHelper(, , , , nil)
}

// KindLink is a NodeKind of the Link node.
var KindLink = NewNodeKind("Link")

// Kind implements Node.Kind.
func ( *Link) () NodeKind {
	return KindLink
}

// NewLink returns a new Link node.
func () *Link {
	 := &Link{
		baseLink: baseLink{
			BaseInline: BaseInline{},
		},
	}
	return 
}

// An Image struct represents an image of the Markdown text.
type Image struct {
	baseLink
}

// Dump implements Node.Dump.
func ( *Image) ( []byte,  int) {
	 := map[string]string{}
	["Destination"] = string(.Destination)
	["Title"] = string(.Title)
	DumpHelper(, , , , nil)
}

// KindImage is a NodeKind of the Image node.
var KindImage = NewNodeKind("Image")

// Kind implements Node.Kind.
func ( *Image) () NodeKind {
	return KindImage
}

// NewImage returns a new Image node.
func ( *Link) *Image {
	 := &Image{
		baseLink: baseLink{
			BaseInline: BaseInline{},
		},
	}
	.Destination = .Destination
	.Title = .Title
	for  := .FirstChild();  != nil; {
		 := .NextSibling()
		.RemoveChild(, )
		.AppendChild(, )
		 = 
	}

	return 
}

// AutoLinkType defines kind of auto links.
type AutoLinkType int

const (
	// AutoLinkEmail indicates that an autolink is an email address.
	AutoLinkEmail AutoLinkType = iota + 1
	// AutoLinkURL indicates that an autolink is a generic URL.
	AutoLinkURL
)

// An AutoLink struct represents an autolink of the Markdown text.
type AutoLink struct {
	BaseInline
	// Type is a type of this autolink.
	AutoLinkType AutoLinkType

	// Protocol specified a protocol of the link.
	Protocol []byte

	value *Text
}

// Inline implements Inline.Inline.
func ( *AutoLink) () {}

// Dump implements Node.Dump.
func ( *AutoLink) ( []byte,  int) {
	 := .value.Segment
	 := map[string]string{
		"Value": string(.Value()),
	}
	DumpHelper(, , , , nil)
}

// KindAutoLink is a NodeKind of the AutoLink node.
var KindAutoLink = NewNodeKind("AutoLink")

// Kind implements Node.Kind.
func ( *AutoLink) () NodeKind {
	return KindAutoLink
}

// URL returns an url of this node.
func ( *AutoLink) ( []byte) []byte {
	if .Protocol != nil {
		 := .value.Segment
		 := make([]byte, 0, len(.Protocol)+.Len()+3)
		 = append(, .Protocol...)
		 = append(, ':', '/', '/')
		 = append(, .value.Text()...)
		return 
	}
	return .value.Text()
}

// Label returns a label of this node.
func ( *AutoLink) ( []byte) []byte {
	return .value.Text()
}

// NewAutoLink returns a new AutoLink node.
func ( AutoLinkType,  *Text) *AutoLink {
	return &AutoLink{
		BaseInline:   BaseInline{},
		value:        ,
		AutoLinkType: ,
	}
}

// A RawHTML struct represents an inline raw HTML of the Markdown text.
type RawHTML struct {
	BaseInline
	Segments *textm.Segments
}

// Inline implements Inline.Inline.
func ( *RawHTML) () {}

// Dump implements Node.Dump.
func ( *RawHTML) ( []byte,  int) {
	 := map[string]string{}
	 := []string{}
	for  := 0;  < .Segments.Len(); ++ {
		 := .Segments.At()
		 = append(, string(.Value()))
	}
	["RawText"] = strings.Join(, "")
	DumpHelper(, , , , nil)
}

// KindRawHTML is a NodeKind of the RawHTML node.
var KindRawHTML = NewNodeKind("RawHTML")

// Kind implements Node.Kind.
func ( *RawHTML) () NodeKind {
	return KindRawHTML
}

// NewRawHTML returns a new RawHTML node.
func () *RawHTML {
	return &RawHTML{
		Segments: textm.NewSegments(),
	}
}