import { useEffect } from 'react';
import { usePrevious } from '../hooks/usePrevious';
import { fitMapBoundsForMarkers, fitMapBoundsForOverlay } from './helpers';
export 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 = usePrevious(markers);
    var prevActiveOverlay = usePrevious(activeOverlay);
    var prevApiIsLoaded = usePrevious(apiIsLoaded);
    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) {
            fitMapBoundsForMarkers(markers, map, activeOverlay);
        }
        else if (shouldUseFitMapBoundsForOverlay) {
            fitMapBoundsForOverlay(map, activeOverlay);
        }
        // eslint-disable-next-line react-hooks/exhaustive-deps
    }, [apiIsLoaded, markers, activeOverlay]);
};
