package p.studio.compiler;

import p.studio.compiler.models.FrontendSpec;
import p.studio.compiler.services.FrontendPhaseService;
import p.studio.compiler.services.PBSFrontendPhaseService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class FrontendRegistryService {
    private static final Map<String, FrontendSpec> FRONTEND_SPECS = new HashMap<>();
    private static final Map<String, FrontendPhaseService> FRONTEND_PHASE_SERVICES = new HashMap<>();

    static {
        FRONTEND_SPECS.put(PBSDefinitions.PBS.getLanguageId(), PBSDefinitions.PBS);
        FRONTEND_PHASE_SERVICES.put(PBSDefinitions.PBS.getLanguageId(), new PBSFrontendPhaseService());
    }

    public static FrontendSpec getDefaultFrontendSpec() {
        return FRONTEND_SPECS.get(PBSDefinitions.PBS.getLanguageId());
    }

    public static Optional<FrontendSpec> getFrontendSpec(final String languageId) {
        return Optional.ofNullable(FRONTEND_SPECS.get(languageId));
    }

    public static Optional<FrontendPhaseService> getFrontendPhaseService(final String languageId) {
        return Optional.ofNullable(FRONTEND_PHASE_SERVICES.get(languageId));
    }

    public static List<FrontendSpec> listFrontendSpecs() {
        return FRONTEND_SPECS.values().stream()
                .sorted((left, right) -> left.getLanguageId().compareToIgnoreCase(right.getLanguageId()))
                .toList();
    }
}
