package org.openautonomousconnection.webserver;

import java.util.Map;

public final class ContentTypeResolver {

    private static final Map<String, String> MAP = Map.ofEntries(
            Map.entry("html", "text/html"),
            Map.entry("css", "text/css"),
            Map.entry("json", "application/json"),
            Map.entry("xml", "application/xml"),
            Map.entry("yml", "application/x-yaml"),
            Map.entry("yaml", "application/x-yaml"),
            Map.entry("txt", "text/plain"),
            Map.entry("png", "image/png"),
            Map.entry("jpg", "image/jpeg"),
            Map.entry("jpeg", "image/jpeg"),
            Map.entry("gif", "image/gif"),
            Map.entry("svg", "image/svg+xml"),
            Map.entry("mp4", "video/mp4"),
            Map.entry("webm", "video/webm"),
            Map.entry("mp3", "audio/mpeg"),
            Map.entry("wav", "audio/wav"),

            // explicitly NOT executed
            Map.entry("java", "text/plain"),
            Map.entry("js", "text/plain"),
            Map.entry("ts", "text/plain"),
            Map.entry("php", "text/plain"),
            Map.entry("py", "text/plain")
    );

    public static String resolve(String name) {
        int i = name.lastIndexOf('.');
        if (i == -1) return "application/octet-stream";
        return MAP.getOrDefault(
                name.substring(i + 1).toLowerCase(),
                "application/octet-stream"
        );
    }
}
