import { useMap } from '@react-hookz/web'

import { useRefWithInit } from './useRefWithInit.ts'

/**
 * Hook that combines useMap with lazy initialization. The init function runs once on the first
 * render.
 */
export function useMapWithInit<T extends (readonly [unknown, unknown])[]>(
  initFn: () => T,
): ReturnType<typeof useMap<T[number][0], T[number][1]>> {
  const initialValue = useRefWithInit(initFn).current
  return useMap(initialValue)
}
