import { Controller } from '@hotwired/stimulus';
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
export default class default_1 extends Controller {
    constructor() {
        super(...arguments);
        this.map = null;
    }
    connect() {
        if (localStorage.getItem('mapbox-consent') === 'true') {
            this.initMap();
        }
        else {
            window.addEventListener('mapbox-consent-given', () => {
                this.initMap();
            }, { once: true });
        }
    }
    initMap() {
        const token = this.tokenValue;
        if (!token || !this.latValue || !this.lngValue || this.map)
            return;
        mapboxgl.accessToken = token;
        try {
            this.map = new mapboxgl.Map({
                container: this.element,
                style: 'mapbox://styles/mapbox/streets-v12',
                center: [this.lngValue, this.latValue],
                zoom: this.zoomValue,
                minZoom: 10,
                maxZoom: 18
            });
            new mapboxgl.Marker()
                .setLngLat([this.lngValue, this.latValue])
                .addTo(this.map);
            this.map.on('load', () => {
                var _a;
                (_a = this.map) === null || _a === void 0 ? void 0 : _a.resize();
            });
            setTimeout(() => {
                var _a;
                (_a = this.map) === null || _a === void 0 ? void 0 : _a.resize();
            }, 200);
        }
        catch (e) {
            console.error('Mapbox error:', e);
        }
    }
}
default_1.values = {
    token: String,
    lat: Number,
    lng: Number,
    zoom: { type: Number, default: 15 }
};
