import React from 'react' ; import { MapkitContext } from './MapkitProvider' ; import { MapOptions , propsToMapConstructionOptions } from './utils' ; import { NumberTuple , Rect , RegionType , createCoordinate , createMapRect , createCoordinateRegionFromValues , } from './utils' ; export const useMap = ( defaultOptions = { } ) => { const [ defaultMapOptions ] = React . useState ( defaultOptions ) ; let { mapkit } = React . useContext ( MapkitContext ) ; let mapRef = React . useRef < HTMLDivElement > ( null ) ; let [ map , setMap ] = React . useState < mapkit . Map > ( ) ; React . useEffect ( ( ) => { if ( mapkit && mapRef . current ) { const newMap = new mapkit . Map ( mapRef . current , propsToMapConstructionOptions ( defaultMapOptions ) ) ; setMap ( newMap ) ; } } , [ mapRef , mapkit ] ) ; React . useEffect ( ( ) => { return ( ) => { if ( map ) { map . destroy ( ) ; } } ; } , [ ] ) ; return { mapkit , map , mapProps : { mapkit , map , mapRef , } , setRotation : React . useCallback ( ( rotationValue , isAnimated = false ) => { if ( map ) { map . setRotationAnimated ( rotationValue , isAnimated ) ; } } , [ map ] ) , setCenter : React . useCallback ( ( centerValue , isAnimated = false ) => { if ( map ) { map . setCenterAnimated ( createCoordinate ( ... centerValue ) , isAnimated ) ; } } , [ map ] ) , setRegion : React . useCallback ( ( region , isAnimated = false ) => { if ( map ) { map . setRegionAnimated ( createCoordinateRegionFromValues ( region ) , isAnimated ) ; } } , [ map ] ) , setVisibleMapRect : React . useCallback ( ( visibleMapRect , isAnimated = false ) => { if ( map ) { map . setVisibleMapRectAnimated ( createMapRect ( ... visibleMapRect ) , isAnimated ) ; } } , [ map ] ) , } ; } ;