package com.yourname.premiumah.util;

import org.bukkit.Material;

import java.util.Locale;
import java.util.Optional;

public final class MaterialUtil {
    private MaterialUtil() {
    }

    public static Optional<Material> parse(String value) {
        if (value == null || value.isBlank()) {
            return Optional.empty();
        }
        String normalized = value.trim().toUpperCase(Locale.ROOT).replace(' ', '_').replace('-', '_');
        try {
            Material material = Material.valueOf(normalized);
            return Optional.of(material);
        } catch (IllegalArgumentException ignored) {
            return Optional.empty();
        }
    }

    public static String pretty(Material material) {
        String lower = material.name().toLowerCase(Locale.ROOT).replace('_', ' ');
        StringBuilder builder = new StringBuilder(lower.length());
        boolean cap = true;
        for (char c : lower.toCharArray()) {
            if (cap && Character.isLetter(c)) {
                builder.append(Character.toUpperCase(c));
                cap = false;
            } else {
                builder.append(c);
            }
            if (c == ' ') {
                cap = true;
            }
        }
        return builder.toString();
    }
}
