import { useEffect, useMemo } from 'react';
import { useMap } from '@vis.gl/react-google-maps';
export var usePolygon = function (props) {
    var onInit = props.onInit, onDragEnd = props.onDragEnd, onMouseUp = props.onMouseUp, options = props.options;
    var polygon = useMemo(function () { return new google.maps.Polygon(); }, []);
    useEffect(function () {
        polygon.setOptions(options);
    }, [polygon, options]);
    var map = useMap();
    useEffect(function () {
        if (!map)
            return;
        polygon.setMap(map);
        if (onInit) {
            onInit(polygon);
        }
        return function () {
            polygon.setMap(null);
        };
    }, [map]);
    useEffect(function () {
        if (!polygon)
            return;
        if (onDragEnd) {
            google.maps.event.addListener(polygon, 'dragend', onDragEnd);
        }
        if (onMouseUp) {
            google.maps.event.addListener(polygon, 'mouseup', onMouseUp);
        }
        return function () {
            google.maps.event.clearInstanceListeners(polygon);
        };
    }, [polygon, onDragEnd, onMouseUp]);
    return polygon;
};
