package dev.dinauer.maven.maven.core;

import jakarta.ws.rs.BadRequestException;

import java.util.List;
import java.util.stream.Stream;

public class MavenMetadataUrlParser
{
    private final String path;

    private MavenMetadataUrlParser(String path)
    {
        this.path = path;
    }

    public static MavenMetadataUrlParser parse(String path)
    {
        return new MavenMetadataUrlParser(path);
    }

    public String groupId()
    {
        List<String> sections = getSections(path);
        return String.join(".", sections.subList(0, sections.size() - 2));
    }

    public String artifactId()
    {
        List<String> sections = getSections(path);
        return sections.get(sections.size() - 2);
    }

    public List<String> getSections(String path)
    {
        String[] sections = path.split("/");
        if (sections.length > 3)
        {
            return Stream.of(sections).filter(section -> !section.isBlank()).toList();
        }
        else
        {
            throw new BadRequestException();
        }
    }
}
