"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.fitMapBoundsForOverlay = exports.fitMapBoundsForMarkers = exports.isActivePolygonDrawing = exports.getMapBoundsPolygon = exports.isDomElement = void 0;
var constants_1 = require("../constants");
var isDomElement = function (el) { return typeof Element !== 'undefined' && el instanceof Element; };
exports.isDomElement = isDomElement;
var getMapBoundsPolygon = function (map) {
    var bounds = map.getBounds();
    var ne = bounds.getNorthEast();
    var sw = bounds.getSouthWest();
    var neLat = ne.lat(), neLng = ne.lng(), swLat = sw.lat(), swLng = sw.lng();
    var additionalPoint1 = [];
    var additionalPoint2 = [];
    if (!((neLng === 180 && swLng === -180) || (neLng === -180 && swLng === 180))) {
        // excluding corner case when we see more than one "map unfolding" (in this case google maps automatically set 180 and -180),
        // we can't describe map bounds as just rectangle, because lng distance can be more than 180",
        // so we need to add additional points to have correct polygon
        var center = bounds.getCenter();
        additionalPoint1.push([neLat, center.lng()]);
        additionalPoint2.push([swLat, center.lng()]);
    }
    return __spreadArray(__spreadArray(__spreadArray(__spreadArray([[neLat, neLng]], additionalPoint1, true), [[neLat, swLng], [swLat, swLng]], false), additionalPoint2, true), [[swLat, neLng]], false);
};
exports.getMapBoundsPolygon = getMapBoundsPolygon;
var isActivePolygonDrawing = function (drawingMode) { return drawingMode === constants_1.POLYGON_DRAWING_MODE; };
exports.isActivePolygonDrawing = isActivePolygonDrawing;
var fitMapBoundsForMarkers = function (markers, map, overlay) {
    var bounds = new window.google.maps.LatLngBounds();
    markers.forEach(function (marker) { return bounds.extend(marker.point); });
    var needAlignZoomLevel = markers.length === 1;
    if (needAlignZoomLevel) {
        var markerPoint = markers[0].point;
        bounds.extend(new window.google.maps.LatLng(markerPoint.lat + constants_1.MAP_MINIMUM_SCALE_RADIUS, markerPoint.lng + constants_1.MAP_MINIMUM_SCALE_RADIUS));
        bounds.extend(new window.google.maps.LatLng(markerPoint.lat - constants_1.MAP_MINIMUM_SCALE_RADIUS, markerPoint.lng - constants_1.MAP_MINIMUM_SCALE_RADIUS));
    }
    map.fitBounds(bounds);
    if (overlay) {
        (0, exports.fitMapBoundsForOverlay)(map, overlay, bounds);
    }
};
exports.fitMapBoundsForMarkers = fitMapBoundsForMarkers;
var fitMapBoundsForOverlay = function (map, overlay, existingBounds) {
    if (overlay.path) {
        var bounds_1 = existingBounds || new window.google.maps.LatLngBounds();
        overlay.path.forEach(function (_a) {
            var lat = _a.lat, lng = _a.lng;
            return bounds_1.extend(new window.google.maps.LatLng(lat, lng));
        });
        map.fitBounds(bounds_1);
    }
};
exports.fitMapBoundsForOverlay = fitMapBoundsForOverlay;
