package fieldgroups import ( "strings" "pdf-wizard/internal/models" ) // MultiLineHandler handles groups where multiple text fields form a single // multi-line response. When the form is filled, the textarea value is split // evenly across the individual fields. type MultiLineHandler struct{} func init() { Register(MultiLineHandler{}) } func (MultiLineHandler) Type() string { return "multiline" } func (MultiLineHandler) Validate(fields []models.FormField, fieldValues map[int]string) error { return nil } func (h MultiLineHandler) ConvertToFillValues(fields []models.FormField, fieldValues map[int]string) map[int]string { result := make(map[int]string) // Collect all submitted values - they should all have the same combined text var combined string for _, f := range fields { if v, ok := fieldValues[f.ID]; ok { combined = v break } } if combined == "" { // No value submitted, return empty for all for _, f := range fields { result[f.ID] = "" } return result } // Split the combined value into lines lines := strings.Split(combined, "\n") // Distribute lines evenly across fields n := len(fields) if n == 0 { return result } linesPerField := len(lines) / n extra := len(lines) % n start := 0 for _, f := range fields { count := linesPerField if extra > 0 { count++ extra-- } end := start + count if end > len(lines) { end = len(lines) } result[f.ID] = strings.Join(lines[start:end], "\n") start = end } return result }