package ui

import (
	

	
)

// StylingRegion represents a region to apply styling.
type StylingRegion struct {
	diag.Ranging
	Styling  Styling
	Priority int
}

// StyleRegions applies styling to the specified regions in s.
//
// The regions are sorted by start position. If multiple Regions share the same
// starting position, the one with the highest priority is kept; the other
// regions are removed. If a Region starts before the end of the previous
// Region, it is also removed.
func ( string,  []StylingRegion) Text {
	 = fixRegions()

	var  Text
	 := 0
	for ,  := range  {
		if .From >  {
			// Add text between regions or before the first region.
			 = append(, &Segment{Text: [:.From]})
		}
		 = append(,
			StyleSegment(&Segment{Text: [.From:.To]}, .Styling))
		 = .To
	}
	if len() >  {
		// Add text after the last region.
		 = append(, &Segment{Text: [:]})
	}
	return 
}

func ( []StylingRegion) []StylingRegion {
	 = append([]StylingRegion(nil), ...)
	// Sort regions by their start positions. Regions with the same start
	// position are sorted by decreasing priority.
	sort.Slice(, func(,  int) bool {
		,  := [], []
		return .From < .From || (.From == .From && .Priority > .Priority)
	})
	// Remove overlapping regions, preferring the ones that appear earlier.
	var  []StylingRegion
	 := 0
	for ,  := range  {
		if .From <  {
			// Overlaps with the last one
			continue
		}
		 = append(, )
		 = .To
	}
	return 
}