package ai.decompile.analysis.service.parser;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

@Component
public class LibrarySymbolParserRegistry {
  private final Map<String, LibrarySymbolParser> parsers;

  public LibrarySymbolParserRegistry(List<LibrarySymbolParser> parserList) {
    this.parsers =
        parserList.stream()
            .collect(Collectors.toMap(LibrarySymbolParser::getFormatName, Function.identity()));
  }

  public LibrarySymbolParser get(String formatName) {
    var parser = parsers.get(formatName);
    if (Objects.isNull(parser)) {
      throw new IllegalArgumentException(
          "No parser found for format '" + formatName + "'. Available: " + parsers.keySet());
    }

    return parser;
  }

  public List<String> getAvailableFormats() {
    return List.copyOf(parsers.keySet());
  }
}
