package display

import (
	
	
	
	
	

	
	
)

// highlightLine applies visual/selection highlighting to a line.
// The provided line might already have been highlighted by a user-provided
// highlighter: this function accounts for any embedded color sequences.
func ( *Engine) ( []rune,  core.Selection) string {
	// Sort regions and extract colors/positions.
	 := sortHighlights()
	 := .getHighlights(, )

	var  string

	// And apply highlighting before each rune.
	for ,  := range  {
		if ,  := [];  {
			 += string()
		}

		 += string()
	}

	// Finally, highlight comments using a regex.
	 := strings.Trim(.opts.GetString("comment-begin"), "\"")
	 := fmt.Sprintf(`(^|\s)%s.*`, )

	if ,  := regexp.Compile();  == nil {
		 := color.SGRStart + color.Fg + "244" + color.SGREnd
		 = .ReplaceAllString(, fmt.Sprintf("%s${0}%s", , color.Reset))
	}

	 += color.Reset

	return 
}

func sortHighlights( core.Selection) []core.Selection {
	 := make([]core.Selection, 0)
	 := make([]core.Selection, 0)
	 := make([]int, 0)

	for ,  := range .Surrounds() {
		 = append(, )
		,  := .Pos()
		 = append(, )
	}

	if .Active() && .IsVisual() {
		 = append(, )
		,  := .Pos()
		 = append(, )
	}

	sort.Ints()

	 := false
	 := 0

	for ,  := range  {
		for ,  := range  {
			,  := .Pos()
			 := .Type == "matcher"

			if  !=  || !.Active() || !.IsVisual() {
				continue
			}

			// If we have both a matcher and a visual selection
			// starting at the same position, then we might have
			// just added the matcher, and we must "overwrite" it
			// with the visual selection, so skip until we find it.
			if  &&  &&  ==  {
				continue
			}

			// Else the region is good to be used in that order.
			 = append(, )
			 = .Type == "matcher"
			 = 

			break
		}
	}

	return 
}

func ( *Engine) ( []rune,  []core.Selection) map[int][]rune {
	 := make(map[int][]rune)

	// Find any highlighting already applied on the line,
	// and keep the indexes so that we can skip those.
	var  [][]int

	 := regexp.MustCompile(`\x1b\[[0-9;]+m`)
	 = .FindAllStringIndex(string(), -1)

	// marks that started highlighting, but not done yet.
	 := make([]core.Selection, 0)
	 := -1
	 := 0

	// Build the string.
	for  := range  {
		var  []rune
		var  core.Selection

		// While in a color escape, keep reading runes.
		if  > 0 {
			--
			continue
		}

		// If starting a color escape code, add offset and read.
		if len() > 0 && [0][0] ==  {
			 += [0][1] - [0][0] - 1
			 = [1:]

			continue
		}

		// Or we are reading a printed rune.
		++

		// First check if we have a new highlighter to apply
		for ,  := range  {
			,  := .Pos()

			if  ==  {
				 = 
				 = append(, )
			}
		}

		// Add new colors if any, and reset if some are done.
		,  = .hlReset(, , )
		 = .hlAdd(, , )

		// Add to the line, with the raw index since
		// we must take into account embedded colors.
		if len() > 0 {
			[] = 
		}
	}

	return 
}

func ( *Engine) ( []core.Selection,  core.Selection,  []rune) []rune {
	var (
		,   string
		 bool
		      core.Selection
	)

	if .Active() {
		 = 
	} else if len() > 0 {
		 = [len()-1]
	}

	,  = .Highlights()
	 = .Type == "matcher"

	// Update the highlighting with inputrc settings if any.
	if  != "" && ! {
		 := color.UnquoteRC("active-region-start-color")
		if , _ = strconv.Unquote();  == "" {
			 = color.Reverse
		}
	}

	// Add highlightings
	 = append(, []rune()...)
	 = append(, []rune()...)

	return 
}

func ( *Engine) ( []core.Selection,  []rune,  int) ([]core.Selection, []rune) {
	for ,  := range  {
		,  := .Pos()
		,  := .Highlights()
		// matcher := reg.Type == "matcher"

		if  !=  {
			continue
		}

		if  < len()-1 {
			 = append([:], [+1:]...)
		} else {
			 = [:]
		}

		if  != "" {
			 = append(, []rune(color.FgDefault)...)
		}

		if  != "" {
			// background, _ := strconv.Unquote(e.opts.GetString("active-region-end-color"))
			// foreground := e.opts.GetString("active-region-start-color")
			 = append(, []rune(color.ReverseReset)...)
			 = append(, []rune(color.BgDefault)...)
			//	if background == "" && foreground == "" && !matcher {
			//		line = append(line, []rune(color.ReverseReset)...)
			//	} else {
			//
			//		line = append(line, []rune(color.BgDefault)...)
			//	}
			//
			// line = append(line, []rune(color.ReverseReset)...)
		}
	}

	return , 
}