const https = require('https');

const url = "https://www.wfs.nrw.de/umwelt/erneuerbare_energien_wfs?REQUEST=GetCapabilities&SERVICE=WFS";

console.log("Fetching capabilities from:", url);

https.get(url, (res) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('end', () => {
        // Simple regex to find FeatureTypes
        // Look for <Name>...</Name> and <Title>...</Title> inside <FeatureType>
        // Note: XML namespace prefixes might vary.

        console.log(`Received ${data.length} bytes.`);

        // Split by FeatureType to isolate
        const parts = data.split(/<\/?(?:wfs:)?FeatureType>/);

        console.log("Found Layers:");
        let count = 0;
        for (const part of parts) {
            if (!part.includes('Name>')) continue;

            const nameMatch = part.match(/<Name>(.*?)<\/Name>/);
            const titleMatch = part.match(/<Title>(.*?)<\/Title>/);

            if (nameMatch) {
                // remove namespaces from name for cleaner printing if needed
                console.log(`- Layer: ${nameMatch[1]}`);
                if (titleMatch) console.log(`  Title: ${titleMatch[1]}`);
                count++;
            }
        }
        if (count === 0) {
            console.log("No layers found. Dumping first 500 chars:");
            console.log(data.substring(0, 500));
        }
    });
}).on('error', err => {
    console.error("Error:", err.message);
});
