package httpsfv

import (
	
	
)

// ErrInvalidInnerListFormat is returned when an inner list format is invalid.
var ErrInvalidInnerListFormat = errors.New("invalid inner list format")

// InnerList represents an inner list as defined in
// https://httpwg.org/specs/rfc9651.html#inner-list.
type InnerList struct {
	Items  []Item
	Params *Params
}

func ( InnerList) () {
}

// marshalSFV serializes as defined in
// https://httpwg.org/specs/rfc9651.html#ser-innerlist.
func ( InnerList) ( *strings.Builder) error {
	if  := .WriteByte('(');  != nil {
		return 
	}

	 := len(.Items)
	for  := 0;  < ; ++ {
		if  := .Items[].marshalSFV();  != nil {
			return 
		}

		if  != -1 {
			if  := .WriteByte(' ');  != nil {
				return 
			}
		}
	}

	if  := .WriteByte(')');  != nil {
		return 
	}

	return .Params.marshalSFV()
}

// parseInnerList parses as defined in
// https://httpwg.org/specs/rfc9651.html#parse-item-or-list.
func parseInnerList( *scanner) (InnerList, error) {
	if .eof() || .data[.off] != '(' {
		return InnerList{}, &UnmarshalError{.off, ErrInvalidInnerListFormat}
	}
	.off++

	 := InnerList{nil, nil}

	for !.eof() {
		.scanWhileSp()

		if .eof() {
			return InnerList{}, &UnmarshalError{.off, ErrInvalidInnerListFormat}
		}

		if .data[.off] == ')' {
			.off++

			,  := parseParams()
			if  != nil {
				return InnerList{}, 
			}

			.Params = 

			return , nil
		}

		,  := parseItem()
		if  != nil {
			return InnerList{}, 
		}

		if .eof() || (.data[.off] != ')' && .data[.off] != ' ') {
			return InnerList{}, &UnmarshalError{.off, ErrInvalidInnerListFormat}
		}

		.Items = append(.Items, )
	}

	return InnerList{}, &UnmarshalError{.off, ErrInvalidInnerListFormat}
}