import pluginMain, { DEFAULT_TILE_SERVER } from "./lib/plugin_index.js";

export default async function(IMap) {
    return class MapImpl extends IMap {
        constructor(response, { map, $page }) {
            super();
            const xmlDoc = new DOMParser().parseFromString(
                new TextDecoder().decode(response),
                "application/xml",
            );
            const svcURL = (function() {
                const svc = xmlDoc.querySelector("Capability OnlineResource");
                if (!svc) return "";
                return svc.getAttribute("xlink:href");
            }());
            let _layer = "";
            const baseLayers = {};
            xmlDoc.querySelectorAll("Layer").forEach((layer) => {
                _layer = layer.querySelector("Name").textContent;
                baseLayers[layer.querySelector("Title").textContent] = window.L.tileLayer.wms(svcURL, {
                    layers: _layer,
                });
            });
            map.setView([0, 0], 1);
            window.L.tileLayer.wms(svcURL, { layers: _layer }).addTo(map);
            window.L.control.layers(baseLayers, {}).addTo(map);
            pluginMain({ $page, map });
        }

        async toGeoJSON() {
            return null;
        }
    }
}
