import { h } from '../webmapgl.core.js';

import { b as toArray } from './chunk-853b4351.js';

let _nextId = 0;
class Style {
    constructor() {
        this.handlers = {
            'click': (e) => this.handleClick(e),
            'mouseenter': () => this.handleMouseenter(),
            'mouseleave': () => this.handleMouseleave()
        };
        this.basemap = false;
        this.clickableLayers = [];
        this.enabled = true;
        this.json = {};
    }
    componentWillLoad() {
        if (this.id === undefined) {
            this.id = 'style-' + _nextId.toString();
            _nextId += 1;
        }
        if (this.url)
            this.fetchJSON();
    }
    componentDidLoad() {
        if (this.clickableLayers && this.clickableLayers.length)
            this.updateClickable(this.clickableLayers);
        this.glStyleElementAdded.emit(this);
    }
    componentDidUpdate() {
        this.glStyleElementModified.emit(this);
    }
    componentDidUnload() {
        this.glStyleElementRemoved.emit(this);
    }
    getMap() {
        return this.el.closest('gl-map').map;
    }
    async fetchJSON() {
        let res = await fetch(this.url);
        this.json = await res.json();
        if (this.token && this.json.sources) {
            for (let srcName in this.json.sources) {
                let src = this.json.sources[srcName];
                if (src.data)
                    src.data = src.data.replace(/\$\{TOKEN\}/g, this.token);
            }
        }
    }
    updateClickable(newLayers, oldLayers) {
        const map = this.getMap();
        newLayers = toArray(newLayers);
        oldLayers = toArray(oldLayers);
        for (let layer of newLayers) {
            const layerIdx = oldLayers.indexOf(layer);
            if (layerIdx === -1) {
                let layerName = `${this.id}:${layer}`;
                map.on('click', layerName, this.handlers.click);
                map.on('mouseenter', layerName, this.handlers.mouseenter);
                map.on('mouseleave', layerName, this.handlers.mouseleave);
            }
            else {
                oldLayers.splice(layerIdx, 1);
            }
        }
        for (let layer of oldLayers) {
            let layerName = `${this.id}:${layer}`;
            map.off('click', layerName, this.handlers.click);
            map.off('mouseenter', layerName, this.handlers.mouseenter);
            map.off('mouseleave', layerName, this.handlers.mouseleave);
        }
    }
    handleClick(e) {
        this.glFeatureClick.emit(e);
    }
    handleMouseenter() {
        this.getMap().getCanvas().style.cursor = 'pointer';
    }
    handleMouseleave() {
        this.getMap().getCanvas().style.cursor = '';
    }
    static get is() { return "gl-style"; }
    static get properties() { return {
        "basemap": {
            "type": Boolean,
            "attr": "basemap"
        },
        "clickableLayers": {
            "type": String,
            "attr": "clickable-layers",
            "watchCallbacks": ["updateClickable"]
        },
        "el": {
            "elementRef": true
        },
        "enabled": {
            "type": Boolean,
            "attr": "enabled"
        },
        "id": {
            "type": String,
            "attr": "id",
            "reflectToAttr": true,
            "mutable": true
        },
        "json": {
            "type": "Any",
            "attr": "json",
            "mutable": true
        },
        "name": {
            "type": String,
            "attr": "name"
        },
        "thumbnail": {
            "type": String,
            "attr": "thumbnail"
        },
        "token": {
            "type": String,
            "attr": "token"
        },
        "url": {
            "type": String,
            "attr": "url"
        }
    }; }
    static get events() { return [{
            "name": "glFeatureClick",
            "method": "glFeatureClick",
            "bubbles": true,
            "cancelable": true,
            "composed": true
        }, {
            "name": "glStyleElementAdded",
            "method": "glStyleElementAdded",
            "bubbles": true,
            "cancelable": true,
            "composed": true
        }, {
            "name": "glStyleElementModified",
            "method": "glStyleElementModified",
            "bubbles": true,
            "cancelable": true,
            "composed": true
        }, {
            "name": "glStyleElementRemoved",
            "method": "glStyleElementRemoved",
            "bubbles": true,
            "cancelable": true,
            "composed": true
        }]; }
}

export { Style as GlStyle };
