package plugin

import "fmt"

type Registry struct {
	protocols map[string]ProtocolHandler
	importers map[string]Importer
}

func NewRegistry() *Registry {
	return &Registry{
		protocols: make(map[string]ProtocolHandler),
		importers: make(map[string]Importer),
	}
}

func (r *Registry) RegisterProtocol(handler ProtocolHandler) {
	r.protocols[handler.Name()] = handler
}

func (r *Registry) RegisterImporter(imp Importer) {
	r.importers[imp.Name()] = imp
}

func (r *Registry) GetProtocol(name string) (ProtocolHandler, error) {
	h, ok := r.protocols[name]
	if !ok {
		return nil, fmt.Errorf("protocol handler %q not registered", name)
	}
	return h, nil
}

func (r *Registry) GetImporter(name string) (Importer, error) {
	imp, ok := r.importers[name]
	if !ok {
		return nil, fmt.Errorf("importer %q not registered", name)
	}
	return imp, nil
}

func (r *Registry) ListProtocols() []string {
	names := make([]string, 0, len(r.protocols))
	for name := range r.protocols {
		names = append(names, name)
	}
	return names
}
