const layerMap = new Map();
var layer = 0;

export function register(id, dotNetReference) {
    layer = layer + 1;
    layerMap.set(id, {layer: layer, reference: dotNetReference});
}

export function closeLayer(tmpLayer) {
    var toClose = []

    layerMap.forEach((value, key, map) => {
        if (value.layer >= tmpLayer) {
            toClose.push({ id: key, ref: value.reference });
            //value.reference.invokeMethodAsync("layerClosed");
        }
    });

    console.log(layerMap);
    console.log(toClose);

    toClose.forEach((item) => {
        layerMap.delete(item.id);
        item.ref.invokeMethodAsync("layerClosed");
    });
    layer = tmpLayer - 1;
}

export function closeLatestLayer() {
    closeLayer(layer);
}

export function closeLayerid(id) {
    var tmpLayer = 1;
    layerMap.forEach((value, key, map) => {
        if (key == id) {
            tmpLayer = value.layer;
        }
    });

    closeLayer(tmpLayer);
}