package com.printhub.printhub.config;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.LinkedHashSet;
import java.util.Set;

@Component
public class I18nBaseNameProvider {

    public String[] getBaseNames() {
        Set<String> baseNames = new LinkedHashSet<>();

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        try {
            // Busca todos los .properties bajo i18n/*/*.properties
            var resources = resolver.getResources("classpath*:/i18n/*/*.properties");

            for (var res : resources) {
                String uri = res.getURI().toString();
                // Extraer ruta base sin extensión .properties ni sufijos como _en
                String base = uri
                        .replaceAll(".*?/i18n/", "i18n/")
                        .replaceAll("(_[a-zA-Z]+)?\\.properties$", "")
                        .replace(".jar!", ""); // por si viene de un jar

                // Eliminar duplicados con un Set
                baseNames.add("classpath:" + base);
            }

        } catch (IOException e) {
            e.printStackTrace(); // o usa logger
        }

        return baseNames.toArray(new String[0]);
    }
}
