"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
var AbstractMap_1 = require("../AbstractMap");
var L = require("leaflet");
var Properties_1 = require("../../Properties");
var OpenStreetMap = /** @class */ (function (_super) {
    __extends(OpenStreetMap, _super);
    function OpenStreetMap(spec) {
        var _this = _super.call(this, spec) || this;
        _this.markers = [];
        _this.maxZoom = 18;
        _this.minZoom = 0;
        _this.defaultZoom = 8;
        _this.resetAllMarkers = function () { return _this.markers.forEach(function (marker) { return _this.deactivateMarker(marker); }); };
        return _this;
    }
    OpenStreetMap.prototype.init = function (wrapper) {
        var map = this.initMap(wrapper);
        this.initMarkers(wrapper, map);
        this.setMarkersToValue(wrapper);
        return wrapper.unwrap();
    };
    OpenStreetMap.prototype.initMap = function (wrapper) {
        var mapContainer = wrapper.find(".lto-map-container");
        return L.map(mapContainer.unwrap(), { minZoom: this.minZoom, maxZoom: this.maxZoom })
            .addLayer(L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors' }))
            .setView(this.getCenter(), this.getZoom());
    };
    OpenStreetMap.prototype.initMarkers = function (wrapper, map) {
        var _this = this;
        if (!this.spec.src)
            return;
        this.initMarkerIcons();
        var countSelections = 0;
        var maxSelections = this.spec.maxSelections || 1;
        var activeMarker;
        OpenStreetMap.getMarkersFromSrc(this.spec.src).then(function (markers) {
            if (!markers)
                return;
            markers.forEach(function (m) {
                var current = L.marker(m.position).addTo(map);
                current.getElement().setAttribute("data-label", m.label || "");
                current.getElement().setAttribute("data-meta", JSON.stringify(m.meta) || "");
                current.getElement().setAttribute("data-active", JSON.stringify(m.active || true));
                _this.markers.push(current);
                m.active ? _this.activateMarker(current) : _this.deactivateMarker(current);
                current.on("click", function () {
                    if (maxSelections === 1) {
                        if (activeMarker) {
                            _this.deactivateMarker(activeMarker);
                        }
                        _this.activateMarker(current);
                        _this.setLabel(current.getElement().getAttribute("data-label") || "", wrapper);
                        activeMarker = current;
                    }
                    else if (JSON.parse(current.getElement().getAttribute("data-active"))) {
                        countSelections -= 1;
                        _this.deactivateMarker(current);
                    }
                    else if (countSelections < maxSelections) {
                        countSelections += 1;
                        _this.activateMarker(current);
                    }
                    _this.setMarkersToValue(wrapper);
                });
            });
        });
    };
    OpenStreetMap.prototype.deactivateMarker = function (marker) {
        marker.getElement().setAttribute("data-active", JSON.stringify(false));
        marker.setIcon(this.markerIcon);
    };
    OpenStreetMap.prototype.activateMarker = function (marker) {
        marker.getElement().setAttribute("data-active", JSON.stringify(true));
        marker.setIcon(this.selectedMarkerIcon);
    };
    OpenStreetMap.prototype.initMarkerIcons = function () {
        var markerSize = [Properties_1.default.resolve("OSM_MARKER_WIDTH") || 32,
            Properties_1.default.resolve("OSM_MARKER_HEIGHT") || 32];
        var selectedMarkerSize = [Properties_1.default.resolve("OSM_SELECTED_MARKER_WIDTH") || 32,
            Properties_1.default.resolve("OSM_SELECTED_MARKER_HEIGHT") || 32];
        this.markerIcon = L.icon({ iconUrl: this.getMarkerIconSrc(), iconSize: markerSize });
        this.selectedMarkerIcon = L.icon({ iconUrl: this.getSelectedMarkerIconSrc(), iconSize: selectedMarkerSize });
    };
    OpenStreetMap.prototype.setMarkersToValue = function (wrapper) {
        var selectedMarkers = [];
        this.markers.forEach(function (marker) {
            if (marker.getElement()) {
                var active = JSON.parse(marker.getElement().getAttribute("data-active"));
                var meta = JSON.parse(marker.getElement().getAttribute("data-meta"));
                if (active) {
                    selectedMarkers.push({ position: marker.getLatLng(), meta: meta });
                }
            }
        });
        this.addMarkersToForm(wrapper, selectedMarkers);
    };
    return OpenStreetMap;
}(AbstractMap_1.AbstractMap));
exports.OpenStreetMap = OpenStreetMap;
//# sourceMappingURL=index.js.map