package main

import (
	"regexp"

	"github.com/rs/zerolog/log"
)

func regexpSelectN(n int, regexps []string, names []string) (matches []string) {
	if n <= 0 {
		matches = make([]string, 0)
	} else {
		matches = make([]string, 0, n)
	}

	res := make([]*regexp.Regexp, 0, len(regexps))
	for _, reStr := range regexps {
		re, err := regexp.Compile(reStr)
		if err != nil {
			log.Warn().Err(err).Str("regexp", reStr).Msg("invalid regexp, ignored")
			continue
		}
		res = append(res, re)
	}

namesLoop:
	for _, name := range names {
		if len(matches) == n {
			break
		}
		for _, re := range res {
			if re.MatchString(name) {
				matches = append(matches, name)
				continue namesLoop
			}
		}
	}

	return
}
