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 __());
    };
})();
import { MarkerIcon } from "./MarkerIcon";
import Properties from "../../Properties";
import { AbstractMap } from "../AbstractMap";
var GoogleMap = /** @class */ (function (_super) {
    __extends(GoogleMap, _super);
    function GoogleMap(spec) {
        var _this = _super.call(this, spec) || this;
        _this.API = "https://maps.googleapis.com/maps/api/js?sensor=false&key=" + Properties.resolve("GOOGLE_MAPS_API_KEY");
        _this.markers = [];
        _this.markerIcon = null;
        _this.selectedMarkerIcon = null;
        _this.minZoom = 0;
        _this.maxZoom = 19;
        _this.defaultZoom = 8;
        _this.resetAllMarkers = function () { return _this.markers.forEach(function (marker) { return _this.deactivateMarker(marker); }); };
        return _this;
    }
    GoogleMap.prototype.init = function (wrapper) {
        var _this = this;
        this.includeScripts([this.API]).then(function () {
            var map = _this.initMap(wrapper);
            _this.initMarkers(map, wrapper);
            _this.setMarkersToValue(wrapper);
        });
        return wrapper.unwrap();
    };
    GoogleMap.prototype.initMarkers = function (map, wrapper) {
        var _this = this;
        if (!this.spec.src)
            return;
        this.initMarkerIcons();
        var countSelections = 0;
        var maxSelections = this.spec.maxSelections || 1;
        var activeMarker;
        GoogleMap.getMarkersFromSrc(this.spec.src).then(function (markers) {
            if (!markers)
                return;
            markers.forEach(function (marker) {
                var current = new google.maps.Marker({ map: map, position: marker.position });
                current.setValues({ context: { label: marker.label, meta: marker.meta, active: marker.active } });
                _this.markers.push(current);
                current.get("context").active ? _this.activateMarker(current) : _this.deactivateMarker(current);
                current.addListener("click", function () {
                    if (maxSelections === 1) {
                        if (activeMarker)
                            _this.deactivateMarker(activeMarker);
                        _this.activateMarker(current);
                        _this.setLabel(current.get("context").label || "", wrapper);
                        activeMarker = current;
                    }
                    else if (current.get("active")) {
                        countSelections -= 1;
                        _this.deactivateMarker(current);
                    }
                    else if (countSelections < maxSelections) {
                        countSelections += 1;
                        _this.activateMarker(current);
                    }
                    _this.setMarkersToValue(wrapper);
                });
            });
        });
    };
    GoogleMap.prototype.initMap = function (wrapper) {
        var map = new google.maps.Map(wrapper.find(".lto-map-container").unwrap(), { center: { lat: 0, lng: 0 }, zoom: 8 });
        map.setCenter(this.getCenter());
        map.setZoom(this.getZoom());
        return map;
    };
    GoogleMap.prototype.deactivateMarker = function (marker) {
        marker.get("context").active = false;
        marker.setIcon(this.markerIcon);
    };
    GoogleMap.prototype.activateMarker = function (marker) {
        marker.get("context").active = true;
        marker.setIcon(this.selectedMarkerIcon);
    };
    GoogleMap.prototype.initMarkerIcons = function () {
        var markerSize = new google.maps.Size(Properties.resolve("GOOGLE_MAPS_MARKER_WIDTH") || 32, Properties.resolve("GOOGLE_MAPS_MARKER_HEIGHT") || 32);
        var selectedMarkerSize = new google.maps.Size(Properties.resolve("GOOGLE_MAPS_SELECTED_MARKER_WIDTH") || 32, Properties.resolve("GOOGLE_MAPS_SELECTED_MARKER_HEIGHT") || 32);
        this.markerIcon = new MarkerIcon(this.getMarkerIconSrc(), markerSize, markerSize);
        this.selectedMarkerIcon = new MarkerIcon(this.getSelectedMarkerIconSrc(), selectedMarkerSize, selectedMarkerSize);
    };
    GoogleMap.prototype.setMarkersToValue = function (wrapper) {
        var selectedMarkers = [];
        this.markers.forEach(function (marker) {
            if (marker.get("context").active) {
                selectedMarkers.push({ position: marker.getPosition(), meta: marker.get("context").meta });
            }
        });
        this.addMarkersToForm(wrapper, selectedMarkers);
    };
    return GoogleMap;
}(AbstractMap));
export { GoogleMap };
//# sourceMappingURL=index.js.map