"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.useFitMapBounds = void 0;
var react_1 = require("react");
var usePrevious_1 = require("../hooks/usePrevious");
var helpers_1 = require("./helpers");
var useFitMapBounds = function (_a) {
    var markers = _a.markers, activeOverlay = _a.activeOverlay, isSearchOnMapMove = _a.isSearchOnMapMove, map = _a.map;
    var apiIsLoaded = Boolean(map && window.google);
    var prevMarkers = (0, usePrevious_1.usePrevious)(markers);
    var prevActiveOverlay = (0, usePrevious_1.usePrevious)(activeOverlay);
    var prevApiIsLoaded = (0, usePrevious_1.usePrevious)(apiIsLoaded);
    (0, react_1.useEffect)(function () {
        var activeOverlayChanged = prevActiveOverlay !== activeOverlay;
        var markersChanged = prevMarkers !== markers;
        var apiIsLoadedChanged = prevApiIsLoaded !== apiIsLoaded;
        var shouldUseFitMapBoundsForOverlay = apiIsLoaded && activeOverlay && (activeOverlayChanged || apiIsLoadedChanged);
        var shouldUseFitMapBoundsForMarkers = apiIsLoaded && !isSearchOnMapMove && markers.length && (markersChanged || apiIsLoadedChanged);
        if (shouldUseFitMapBoundsForMarkers) {
            (0, helpers_1.fitMapBoundsForMarkers)(markers, map, activeOverlay);
        }
        else if (shouldUseFitMapBoundsForOverlay) {
            (0, helpers_1.fitMapBoundsForOverlay)(map, activeOverlay);
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [apiIsLoaded, markers, activeOverlay]);
};
exports.useFitMapBounds = useFitMapBounds;
