/**
 * DevExtreme (esm/ui/map/provider.google_static.js)
 * Version: 21.1.4 (build 21182-0312)
 * Build date: Thu Jul 01 2021
 *
 * Copyright (c) 2012 - 2021 Developer Express Inc. ALL RIGHTS RESERVED
 * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
 */
import {
    each
} from "../../core/utils/iterator";
import eventsEngine from "../../events/core/events_engine";
import Promise from "../../core/polyfills/promise";
import Provider from "./provider";
import Color from "../../color";
import {
    name as clickEventName
} from "../../events/click";
var GOOGLE_STATIC_URL = "https://maps.google.com/maps/api/staticmap?";
var GoogleStaticProvider = Provider.inherit({
    _locationToString: function(location) {
        var latLng = this._getLatLng(location);
        return latLng ? latLng.lat + "," + latLng.lng : location.toString().replace(/ /g, "+")
    },
    _renderImpl: function() {
        return this._updateMap()
    },
    updateDimensions: function() {
        return this._updateMap()
    },
    updateMapType: function() {
        return this._updateMap()
    },
    updateBounds: function() {
        return Promise.resolve()
    },
    updateCenter: function() {
        return this._updateMap()
    },
    updateZoom: function() {
        return this._updateMap()
    },
    updateControls: function() {
        return Promise.resolve()
    },
    addMarkers: function(options) {
        var that = this;
        return this._updateMap().then((function(result) {
            each(options, (function(_, options) {
                that._fireMarkerAddedAction({
                    options: options
                })
            }));
            return result
        }))
    },
    removeMarkers: function(options) {
        var that = this;
        return this._updateMap().then((function(result) {
            each(options, (function(_, options) {
                that._fireMarkerRemovedAction({
                    options: options
                })
            }));
            return result
        }))
    },
    adjustViewport: function() {
        return Promise.resolve()
    },
    addRoutes: function(options) {
        var that = this;
        return this._updateMap().then((function(result) {
            each(options, (function(_, options) {
                that._fireRouteAddedAction({
                    options: options
                })
            }));
            return result
        }))
    },
    removeRoutes: function(options) {
        var that = this;
        return this._updateMap().then((function(result) {
            each(options, (function(_, options) {
                that._fireRouteRemovedAction({
                    options: options
                })
            }));
            return result
        }))
    },
    clean: function() {
        this._$container.css("backgroundImage", "none");
        eventsEngine.off(this._$container, this._addEventNamespace(clickEventName));
        return Promise.resolve()
    },
    mapRendered: function() {
        return true
    },
    _updateMap: function() {
        var key = this._keyOption("googleStatic");
        var $container = this._$container;
        var requestOptions = ["sensor=false", "size=" + Math.round($container.width()) + "x" + Math.round($container.height()), "maptype=" + this._option("type"), "center=" + this._locationToString(this._option("center")), "zoom=" + this._option("zoom"), this._markersSubstring()];
        requestOptions.push.apply(requestOptions, this._routeSubstrings());
        if (key) {
            requestOptions.push("key=" + key)
        }
        var request = GOOGLE_STATIC_URL + requestOptions.join("&");
        this._$container.css("background", 'url("' + request + '") no-repeat 0 0');
        this._attachClickEvent();
        return Promise.resolve(true)
    },
    _markersSubstring: function() {
        var that = this;
        var markers = [];
        var markerIcon = this._option("markerIconSrc");
        if (markerIcon) {
            markers.push("icon:" + markerIcon)
        }
        each(this._option("markers"), (function(_, marker) {
            markers.push(that._locationToString(marker.location))
        }));
        return "markers=" + markers.join("|")
    },
    _routeSubstrings: function() {
        var that = this;
        var routes = [];
        each(this._option("routes"), (function(_, route) {
            var color = new Color(route.color || that._defaultRouteColor()).toHex().replace("#", "0x");
            var opacity = Math.round(255 * (route.opacity || that._defaultRouteOpacity())).toString(16);
            var width = route.weight || that._defaultRouteWeight();
            var locations = [];
            each(route.locations, (function(_, routePoint) {
                locations.push(that._locationToString(routePoint))
            }));
            routes.push("path=color:" + color + opacity + "|weight:" + width + "|" + locations.join("|"))
        }));
        return routes
    },
    _attachClickEvent: function() {
        var that = this;
        var eventName = this._addEventNamespace(clickEventName);
        eventsEngine.off(this._$container, eventName);
        eventsEngine.on(this._$container, eventName, (function(e) {
            that._fireClickAction({
                event: e
            })
        }))
    }
});
export default GoogleStaticProvider;
