package decoders

import (
	"fmt"
	"strings"

	"git.max-richter.dev/max/marka/parser/matcher"
	"git.max-richter.dev/max/marka/template"
)

func List(input string, block template.Block) (value any, error error) {
	blocks, err := template.CompileTemplate(block.ListTemplate)
	if err != nil {
		return nil, fmt.Errorf("cannot extract blocks: %w", err)
	}

	var out []any

	for line := range strings.SplitSeq(strings.TrimSuffix(input, "\n"), "\n") {
		matches := matcher.MatchBlocksFuzzy(line, blocks, 0.3)

		res, err := Parse(matches)
		if err != nil {
			return nil, fmt.Errorf("could not match blocks: %w", err)
		}

		out = append(out, res)
	}

	return out, nil
}
