package vcs

import (
	
	
	
	
)

// BackendAuto selects automatic VCS backend detection.
const BackendAuto = "auto"

// Opener attempts to open a VCS repository containing dir.
type Opener struct {
	Name string
	Open func(ctx context.Context, dir string) (VCS, error)
}

// Registry holds available VCS backend openers without using global mutable state.
type Registry struct {
	openers []Opener
}

// NewRegistry creates a Registry with the given VCS backends in priority order.
func ( ...Opener) *Registry {
	return &Registry{
		openers: slices.Clone(),
	}
}

// Names returns the registered backend names.
func ( *Registry) () []string {
	 := make([]string, len(.openers))
	for ,  := range .openers {
		[] = .Name
	}
	return 
}

// Open opens the repository at dir using the named backend, or auto-detects
// when name is "" or "auto".
func ( *Registry) ( context.Context, ,  string) (VCS, error) {
	if  == "" ||  == BackendAuto {
		var  []string
		for ,  := range .openers {
			,  := .Open(, )
			if  == nil {
				return , nil
			}
			 = append(, fmt.Sprintf("%s: %v", .Name, ))
		}
		return nil, fmt.Errorf("no supported VCS repository found in %q (%s)", , strings.Join(, "; "))
	}

	for ,  := range .openers {
		if .Name ==  {
			return .Open(, )
		}
	}
	return nil, fmt.Errorf("unknown VCS backend %q (available: %s)", , strings.Join(.Names(), ", "))
}