import customIcons from './iconsMap.js';

const overlayMaps = {};

async function getOverlayMaps() {
    await Promise.all(Object.keys(overlayMapsTemplate).map(async (key) => {
        const url = `/poi/${key.toLowerCase()}/`;
        try {
            const response = await fetch(url);
            const data = await response.json();
            let icon = overlayMapsTemplate[key].icon;
            overlayMaps[key] = L.layerGroup(data.map((item) => L.marker([item.y, item.x], { icon: icon }).bindPopup(item.description)));
        } catch (error) {
            console.error(`Error fetching ${key} data:`, error);
        }
    }));
    return overlayMaps;
}

// Fonction pour récupérer les positions des personnages
async function getPlayersLocations() {
    const url = `/characterloc`;
    try {
        const response = await fetch(url);
        let data = await response.json();
        return data;
    } catch (error) {
        console.error(`Error fetching ${key} data:`, error);
    }
}

// overlayMapsTemplate est l'objet avec les clés que vous avez définies
const overlayMapsTemplate = {
    Ringways: { icon: customIcons.blueCircleVoidIcon },
    Gateways: { icon: customIcons.blueCircleFullIcon },
    PoI: { icon: customIcons.blueCrossFullIcon },
    Town: { icon: customIcons.yellowSquareFullIcon },
    Outpost: { icon: customIcons.yellowSquareVoidIcon },
    Vault: { icon: customIcons.redCrossFullIcon },
    Dungeon: { icon: customIcons.redCrossVoidIcon },
    City: { icon: customIcons.yellowCrownIcon },
    Faction: { icon: customIcons.greySquareIcon },
};

export { getOverlayMaps, getPlayersLocations };