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