const https = require('https');

const url = "https://www.wms.nrw.de/wms/wms_nw_regionalplan?SERVICE=WMS&REQUEST=GetCapabilities";

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

https.get(url, (res) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('end', () => {
        console.log(`Received ${data.length} bytes.`);

        // Simple regex to find Layers
        // WMS: <Layer> <Name>...</Name> <Title>...</Title> ... <Layer>

        const matches = data.matchAll(/<Layer[^>]*>([\s\S]*?)<\/Layer>/g);
        console.log("Found Layers (Top level):");
        // This regex is too greedy for nested layers.
        // Let's just grep for Name and Title lines to see what's available

        const lines = data.split('\n');
        lines.forEach(line => {
            if (line.includes('<Name>') || line.includes('<Title>')) {
                // console.log(line.trim());
            }
        });

        // Better: print the structure loosely
        // Or just print Names
        const nameMatches = data.matchAll(/<Name>(.*?)<\/Name>/g);
        const titleMatches = data.matchAll(/<Title>(.*?)<\/Title>/g);

        console.log("--- Names found ---");
        for (const m of nameMatches) console.log(m[1]);

        console.log("--- Titles found ---");
        // Limit titles output
        let i = 0;
        for (const m of titleMatches) {
            if (i++ < 20) console.log(m[1]);
        }
    });
}).on('error', err => {
    console.error("Error:", err.message);
});
