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 { AbstractMap } from "../AbstractMap";
import Properties from "../../Properties";
var HereMap = /** @class */ (function (_super) {
    __extends(HereMap, _super);
    function HereMap(spec) {
        var _this = _super.call(this, spec) || this;
        _this.API_CORE = "https://js.api.here.com/v3/3.1/mapsjs-core.js";
        _this.API_SERVICE = "https://js.api.here.com/v3/3.1/mapsjs-service.js";
        _this.API_UI = "https://js.api.here.com/v3/3.1/mapsjs-ui.js";
        _this.API_EVENTS = "https://js.api.here.com/v3/3.1/mapsjs-mapevents.js";
        _this.markers = [];
        _this.defaultZoom = 8;
        _this.maxZoom = 20;
        _this.minZoom = 0;
        // @ts-ignore
        _this.createPlatform = function () { return new H.service.Platform({ apikey: Properties.resolve("HERE_MAPS_API_KEY") }); };
        _this.resetAllMarkers = function () { return _this.markers.forEach(function (marker) { return _this.deactivateMarker(marker); }); };
        _this.onRoutingError = function (error) { return console.error(error.name, ": ", error.message); };
        _this.initAPI = function () { return new Promise(function (resolve) { return _this.includeScripts([_this.API_CORE]).then(function () { return _this.includeScripts([_this.API_SERVICE, _this.API_UI, _this.API_EVENTS]).then(function () { return resolve(); }); }); }); };
        return _this;
    }
    HereMap.prototype.init = function (wrapper) {
        var _this = this;
        this.initAPI().then(function () {
            var platform = _this.createPlatform();
            _this.initMap(wrapper, platform);
            _this.initMarkers(wrapper);
            _this.calculateRoute(platform, _this.spec.routePoints);
        });
        return wrapper.unwrap();
    };
    HereMap.prototype.initMap = function (wrapper, platform) {
        var layers = platform.createDefaultLayers();
        var map = new H.Map(wrapper.find('.lto-map-container').unwrap(), 
        // @ts-ignore
        layers.vector.normal.map, {
            zoom: this.getZoom(), center: this.getCenter()
        });
        H.ui.UI.createDefault(map, layers, 'de-DE');
        new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
        this.map = map;
    };
    HereMap.prototype.initMarkerIcons = function () {
        var markerIcon = document.createElement("img");
        markerIcon.src = this.getMarkerIconSrc();
        markerIcon.width = Properties.resolve("HERE_MAPS_MARKER_WIDTH") || 32;
        markerIcon.height = Properties.resolve("HERE_MAPS_MARKER_HEIGHT") || 32;
        markerIcon.style.margin = Properties.resolve("HERE_MAPS_MARKER_MARGIN") || "-32px 0 0 -16px";
        var selectedMarkerIcon = document.createElement("img");
        selectedMarkerIcon.src = this.getSelectedMarkerIconSrc();
        selectedMarkerIcon.width = Properties.resolve("HERE_MAPS_SELECTED_MARKER_WIDTH") || 32;
        selectedMarkerIcon.height = Properties.resolve("HERE_MAPS_SELECTED_MARKER_HEIGHT") || 32;
        selectedMarkerIcon.style.margin = Properties.resolve("HERE_MAPS_SELECTED_MARKER_MARGIN") || "-32px 0 0 -16px";
        this.markerIcon = new H.map.DomIcon(markerIcon);
        this.selectedMarkerIcon = new H.map.DomIcon(selectedMarkerIcon);
    };
    HereMap.prototype.initMarkers = function (wrapper) {
        var _this = this;
        if (!this.spec.src)
            return;
        this.initMarkerIcons();
        var countSelections = 0;
        var maxSelections = this.spec.maxSelections || 1;
        var activeMarker;
        AbstractMap.getMarkersFromSrc(this.spec.src).then(function (markers) {
            if (!markers)
                return;
            markers.forEach(function (m) {
                var current = new H.map.DomMarker(m.position);
                current.setData({ label: m.label || "", meta: m.meta || "", active: m.active, position: m.position });
                _this.markers.push(current);
                m.active ? _this.activateMarker(current) : _this.deactivateMarker(current);
                current.addEventListener("pointerdown", function () {
                    if (maxSelections === 1) {
                        if (activeMarker)
                            _this.deactivateMarker(activeMarker);
                        _this.activateMarker(current);
                        _this.setLabel(current.getData().label, wrapper);
                        activeMarker = current;
                    }
                    else if (current.getData().active) {
                        countSelections -= 1;
                        _this.deactivateMarker(current);
                    }
                    else if (countSelections < maxSelections) {
                        countSelections += 1;
                        _this.activateMarker(current);
                    }
                    _this.setMarkersToValue(wrapper);
                });
                _this.map.addObject(current);
            });
            _this.setMarkersToValue(wrapper);
        });
    };
    HereMap.prototype.deactivateMarker = function (marker) {
        var data = marker.getData();
        data.active = false;
        marker.setData(data);
        marker.setIcon(this.markerIcon);
    };
    HereMap.prototype.activateMarker = function (marker) {
        var data = marker.getData();
        data.active = true;
        marker.setData(data);
        marker.setIcon(this.selectedMarkerIcon);
    };
    HereMap.prototype.calculateRoute = function (platform, routePoints) {
        var _this = this;
        if (!routePoints)
            return;
        var parts = routePoints.split(";");
        var waypoint0 = parts[0];
        var waypoint1 = parts[1];
        var routeRequestParams = {
            mode: 'fastest;car',
            representation: 'display',
            routeattributes: 'waypoints,summary,shape,legs',
            maneuverattributes: 'direction,action',
            waypoint0: waypoint0, waypoint1: waypoint1
        };
        platform.getRoutingService().calculateRoute(routeRequestParams, function (result) { return _this.onRoutingSuccess(result); }, function (error) { return _this.onRoutingError(error); });
    };
    HereMap.prototype.onRoutingSuccess = function (result) {
        if (result.response && result.response.route && result.response.route[0]) {
            if (result.response.route[0].shape) {
                this.addRouteShapeToMap(result.response.route[0].shape);
            }
            if (result.response.route[0].waypoint) {
                this.addRouteMarkersToMap(result.response.route[0].waypoint);
            }
        }
        else
            console.error("Route can't be displayed");
    };
    HereMap.prototype.addRouteMarkersToMap = function (waypoints) {
        var startPoint = waypoints[0];
        var endPoint = waypoints[waypoints.length - 1];
        var startMarker = new H.map.DomMarker({ lat: startPoint.originalPosition.latitude, lng: startPoint.originalPosition.longitude }, { icon: this.getRouteStartIcon() });
        var endMarker = new H.map.DomMarker({ lat: endPoint.originalPosition.latitude, lng: endPoint.originalPosition.longitude }, { icon: this.getRouteEndIcon() });
        this.map.addObjects([startMarker, endMarker]);
    };
    HereMap.prototype.getRouteStartIcon = function () {
        var routeStartIcon = document.createElement("img");
        routeStartIcon.src = this.spec.routeStartIcon || this.getMarkerIconSrc();
        routeStartIcon.width = Properties.resolve("HERE_MAPS_ROUTE_START_ICON_WIDTH") || 32;
        routeStartIcon.height = Properties.resolve("HERE_MAPS_ROUTE_START_ICON_HEIGHT") || 32;
        routeStartIcon.style.margin = Properties.resolve("HERE_MAPS_ROUTE_START_ICON_MARGIN") || "-32px 0 0 -16px";
        return new H.map.DomIcon(routeStartIcon);
    };
    HereMap.prototype.getRouteEndIcon = function () {
        var routeEndIcon = document.createElement("img");
        routeEndIcon.src = this.spec.routeEndIcon || this.getMarkerIconSrc();
        routeEndIcon.width = Properties.resolve("HERE_MAPS_ROUTE_END_ICON_WIDTH") || 32;
        routeEndIcon.height = Properties.resolve("HERE_MAPS_ROUTE_END_ICON_HEIGHT") || 32;
        routeEndIcon.style.margin = Properties.resolve("HERE_MAPS_ROUTE_END_ICON_MARGIN") || "-32px 0 0 -16px";
        return new H.map.DomIcon(routeEndIcon);
    };
    HereMap.prototype.addRouteShapeToMap = function (routeShape) {
        var lineString = new H.geo.LineString();
        routeShape.forEach(function (point) {
            var parts = point.split(',');
            lineString.pushLatLngAlt(Number(parts[0]), Number(parts[1]), 10);
        });
        var polylineOptions = Properties.resolve("HERE_MAPS_ROUTING_LINE_OPTIONS") ||
            { style: { lineWidth: 4, strokeColor: 'rgba(0, 128, 255, 0.7)' } };
        this.map.addObject(new H.map.Polyline(lineString, polylineOptions));
    };
    HereMap.prototype.setMarkersToValue = function (wrapper) {
        var selectedMarkers = [];
        this.markers.forEach(function (marker) {
            if (marker.getData().active) {
                selectedMarkers.push({ position: marker.getData().position, meta: marker.getData().meta });
            }
        });
        this.addMarkersToForm(wrapper, selectedMarkers);
    };
    return HereMap;
}(AbstractMap));
export { HereMap };
//# sourceMappingURL=index.js.map