import { useMapInternalContext } from '../context/map-context.utils'; import { useCallback } from 'react'; import type { DrivingInfo, RouteInfo } from '../../../bare'; import type { Point } from '../types'; import { MapApi } from '../../data/map.api'; import type { SetCameraPositionParams } from '../types'; import { MapEvent } from '../types'; export const useMap = () => { const { mapRef, isConfigured } = useMapInternalContext(); const getCameraPosition = useCallback(() => { return mapRef.current?.getCameraPosition(); }, [mapRef]); const setCameraPosition = useCallback( (params: SetCameraPositionParams) => { mapRef.current?.setCameraPosition(params); }, [mapRef] ); const addListener = useCallback( (event: MapEvent, cb: (data: T) => void) => { return mapRef.current?.addListener(event, cb); }, [mapRef] ); const geocode = useCallback((point: Point) => { return MapApi.shared.geocode(point); }, []); const reverseGeocode = useCallback((query: string) => { return MapApi.shared.reverseGeocode(query); }, []); const findPath = useCallback( async (points: Point[]): Promise[]> => { return new Promise((res) => { mapRef.current?.raw?.current?.findDrivingRoutes(points, (r) => { res((r as unknown as { routes: RouteInfo[] }).routes); }); }); }, [mapRef] ); const fitPoints = useCallback( (points: Array) => { mapRef.current?.raw.current?.fitMarkers(points); }, [mapRef] ); return { isReady: isConfigured, addListener, map: { getCameraPosition, setCameraPosition, fitPoints, }, api: { geocode, reverseGeocode, findPath, }, }; };