package eval

import (
	
	

	
	
	
)

var errStyledSegmentArgType = errors.New("argument to styled-segment must be a string or a styled segment")

func () {
	addBuiltinFns(map[string]interface{}{
		"styled-segment": styledSegment,
		"styled":         styled,
	})
}

//elvdoc:fn styled-segment
//
// ```elvish
// styled-segment $object &fg-color=default &bg-color=default &bold=$false &dim=$false &italic=$false &underlined=$false &blink=$false &inverse=$false
// ```
//
// Constructs a styled segment and is a helper function for styled transformers.
// `$object` can be a plain string, a styled segment or a concatenation thereof.
// Probably the only reason to use it is to build custom style transformers:
//
// ```elvish
// fn my-awesome-style-transformer [seg]{ styled-segment $seg &bold=(not $seg[dim]) &dim=(not $seg[italic]) &italic=$seg[bold] }
// styled abc $my-awesome-style-transformer~
// ```
//
// As just seen the properties of styled segments can be inspected by indexing into
// it. Valid indices are the same as the options to `styled-segment` plus `text`.
//
// ```elvish
// s = (styled-segment abc &bold)
// put $s[text]
// put $s[fg-color]
// put $s[bold]
// ```

// Turns a string or ui.Segment into a new ui.Segment with the attributes
// from the supplied options applied to it. If the input is already a Segment its
// attributes are copied and modified.
func ( RawOptions,  interface{}) (*ui.Segment, error) {
	var  string
	var  ui.Style

	switch input := .(type) {
	case string:
		 = 
	case *ui.Segment:
		 = .Text
		 = .Style
	default:
		return nil, errStyledSegmentArgType
	}

	if  := .MergeFromOptions();  != nil {
		return nil, 
	}

	return &ui.Segment{
		Text:  ,
		Style: ,
	}, nil
}

//elvdoc:fn styled
//
// ```elvish
// styled $object $style-transformer...
// ```
//
// Construct a styled text by applying the supplied transformers to the supplied
// object. `$object` can be either a string, a styled segment (see below), a styled
// text or an arbitrary concatenation of them. A `$style-transformer` is either:
//
// -   The name of a builtin style transformer, which may be one of the following:
//
//     -   One of the attribute names `bold`, `dim`, `italic`, `underlined`,
//     `blink` or `inverse` for setting the corresponding attribute.
//
//     -   An attribute name prefixed by `no-` for unsetting the attribute.
//
//     -   An attribute name prefixed by `toggle-` for toggling the attribute
//     between set and unset.
//
// -   A color name for setting the text color, which may be one of the
// following:
//
//     -   One of the 8 basic ANSI colors: `black`, `red`, `green`, `yellow`,
//    `blue`, `magenta`, `cyan` and `white`.
//
//     -   The bright variant of the 8 basic ANSI colors, with a `bright-`
//    prefix.
//
//     -   Any color from the xterm 256-color palette, as `colorX` (such as
//    `color12`).
//
//     -   A 24-bit RGB color written as `#RRGGBB` such as `'#778899'`.
//
//		   **Note**: You need to quote such values since an unquoted `#` char
//		   introduces a comment. So use `'bg-#778899'` not `bg-#778899`. If
//		   you omit the quotes the text after the `#` char is ignored which
//		   will result in an error or unexpected behavior.
//
// -   A color name prefixed by `bg-` to set the background color.
//
// -   A color name prefixed by `fg-` to set the foreground color. This has
// the same effect as specifying the color name without the `fg-` prefix.
//
// -   A lambda that receives a styled segment as the only argument and returns a
// single styled segment.
//
// -   A function with the same properties as the lambda (provided via the
// `$transformer~` syntax).
//
// When a styled text is converted to a string the corresponding
// [ANSI SGR code](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_.28Select_Graphic_Rendition.29_parameters)
// is built to render the style.
//
// A styled text is nothing more than a wrapper around a list of styled segments.
// They can be accessed by indexing into it.
//
// ```elvish
// s = (styled abc red)(styled def green)
// put $s[0] $s[1]
// ```

func ( *Frame,  interface{},  ...interface{}) (ui.Text, error) {
	var  ui.Text

	switch input := .(type) {
	case string:
		 = ui.Text{&ui.Segment{
			Text:  ,
			Style: ui.Style{},
		}}
	case *ui.Segment:
		 = ui.Text{.Clone()}
	case ui.Text:
		 = .Clone()
	default:
		return nil, fmt.Errorf("expected string, styled segment or styled text; got %s", vals.Kind())
	}

	for ,  := range  {
		switch styling := .(type) {
		case string:
			 := ui.ParseStyling()
			if  == nil {
				return nil, fmt.Errorf("%s is not a valid style transformer", parse.Quote())
			}
			 = ui.StyleText(, )
		case Callable:
			for ,  := range  {
				,  := .CaptureOutput(func( *Frame) error {
					return .Call(, []interface{}{}, NoOpts)
				})
				if  != nil {
					return nil, 
				}

				if  := len();  != 1 {
					return nil, fmt.Errorf("styling function must return a single segment; got %d values", )
				} else if ,  := [0].(*ui.Segment); ! {
					return nil, fmt.Errorf("styling function must return a segment; got %s", vals.Kind([0]))
				} else {
					[] = 
				}
			}

		default:
			return nil, fmt.Errorf("need string or callable; got %s", vals.Kind())
		}
	}

	return , nil
}