package modgraph

import (
	
	
	
	
	
	
)

// ErrNoModules is returned when loading or filtering yields zero modules.
var ErrNoModules = errors.New("no modules matched")

// FilterOptions configures module filtering.
type FilterOptions struct {
	Include         []string
	Exclude         []string
	ExcludeVersions []string
	Targets         []string
}

// SkippedModule pairs a filtered-out module with the reason it was skipped.
type SkippedModule struct {
	Module *Module
	Reason string
}

// Filter partitions modules into kept and skipped according to opts.
func ( []*Module,  FilterOptions) ( []*Module,  []SkippedModule,  error) {
	 := make([]string, len(.Targets))
	for ,  := range .Targets {
		[] = normalizeTarget()
	}

	for ,  := range  {
		if  := matchExcludeVersion(, .ExcludeVersions);  != "" {
			 = append(, SkippedModule{Module: , Reason: })
			continue
		}
		if len(.Include) > 0 {
			,  := matchAny(.Include, )
			if  != nil {
				return nil, nil, 
			}
			if ! {
				 = append(, SkippedModule{Module: , Reason: "not matched by include"})
				continue
			}
		}
		if len(.Exclude) > 0 {
			,  := matchAny(.Exclude, )
			if  != nil {
				return nil, nil, 
			}
			if  {
				 = append(, SkippedModule{Module: , Reason: "matched by exclude"})
				continue
			}
		}
		if len() > 0 && !matchesTargets(, ) {
			 = append(, SkippedModule{Module: , Reason: "not in target list"})
			continue
		}
		 = append(, )
	}

	for ,  := range  {
		if !slices.ContainsFunc(, func( *Module) bool { return matchesTarget(, ) }) {
			return nil, nil, fmt.Errorf("target %q did not match any eligible module", )
		}
	}

	return , , nil
}

func ( *Module,  []string) string {
	if len() == 0 {
		return ""
	}
	for ,  := range .Requires {
		if slices.Contains(, .Version) {
			return fmt.Sprintf("requires excluded version %s (%s)", .Version, .Path)
		}
	}
	return ""
}

func ( string) string {
	 = strings.TrimSpace(filepath.ToSlash())
	if  == "." ||  == "./" {
		return "."
	}
	return strings.TrimPrefix(path.Clean(), "./")
}

func ( *Module,  []string) bool {
	return slices.ContainsFunc(, func( string) bool { return matchesTarget(, ) })
}

func ( *Module,  string) bool {
	return .Path ==  || .Dir == 
}

func ( []string,  *Module) (bool, error) {
	for ,  := range  {
		if  == "" {
			continue
		}
		,  := matchGlob(, .Path)
		if  != nil {
			return false, fmt.Errorf("invalid glob pattern %q: %w", , )
		}
		if  {
			return true, nil
		}
		,  := matchGlob(, .Dir)
		if  != nil {
			return false, fmt.Errorf("invalid glob pattern %q: %w", , )
		}
		if  {
			return true, nil
		}
	}
	return false, nil
}

// matchGlob matches pattern against name using slash-separated path segments,
// supporting "*", "?", "[...]", and "**" (zero or more segments).
func (,  string) (bool, error) {
	 = strings.TrimPrefix(filepath.ToSlash(), "./")
	 = strings.TrimPrefix(filepath.ToSlash(), "./")
	if !strings.Contains(, "**") {
		return path.Match(, )
	}
	return matchSegments(strings.Split(, "/"), strings.Split(, "/"))
}

func (,  []string) (bool, error) {
	if len() == 0 {
		return len() == 0, nil
	}
	if [0] == "**" {
		if ,  := ([1:], );  != nil ||  {
			return , 
		}
		if len() > 0 {
			return (, [1:])
		}
		return false, nil
	}
	if len() == 0 {
		return false, nil
	}
	,  := path.Match([0], [0])
	if  != nil || ! {
		return , 
	}
	return ([1:], [1:])
}