const https = require('https');

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

https.get(url, (res) => {
    let data = '';
    res.on('data', chunk => data += chunk);
    res.on('end', () => {
        // Match Layer Name and Title
        const regex = /<Layer[^>]*>([\s\S]*?)<\/Layer>/g;
        // We need to parse recursively or just look for Name/Title pairs close to each other
        // Simple scan
        const layers = [];
        const lines = data.split(/>\s*</);

        let currentName = "";
        let currentTitle = "";

        // This simple XML parsing is fragile but sufficient for discovery
        // Let's just dump all Name/Title occurrences
        const names = [];
        const titles = [];

        const nameRe = /<Name>(.*?)<\/Name>/g;
        const titleRe = /<Title>(.*?)<\/Title>/g;

        let m;
        while ((m = nameRe.exec(data)) !== null) {
            names.push(m[1]);
        }
        while ((m = titleRe.exec(data)) !== null) {
            titles.push(m[1]);
        }

        console.log("Names:", names);
        console.log("Titles:", titles);
    });
});
