// Use Vite's Glob Import to load all SVGs from subdirectories
// eager: true loads the URL strings immediately
// as: 'url' gives us the path string instead of a module
const emojiFiles = import.meta.glob('./*/*.svg', { eager: true, as: 'url' });

// Transform into a categorized object
// Structure: { [category]: [ { name: string, src: string } ] }
export const CategorizedEmojis = {};

Object.entries(emojiFiles).forEach(([path, src]) => {
    // path examples: "./activity/baseball.svg", "./people/smile.svg"
    
    // Extract parts
    const match = path.match(/\.\/([^/]+)\/(.+)\.svg$/);
    if (match) {
        const category = match[1];
        const filename = match[2];
        
        if (!CategorizedEmojis[category]) {
            CategorizedEmojis[category] = [];
        }

        CategorizedEmojis[category].push({
            name: filename,
            src: src
        });
    }
});

// Helper to get flatten list
export const AllEmojis = Object.values(CategorizedEmojis).flat();

// Helper to get a specific emoji URL (safely)
export const getEmojiUrl = (category, name) => {
    return CategorizedEmojis[category]?.find(e => e.name === name)?.src || null;
};

export default CategorizedEmojis;
