import { useMemo } from 'react'
import { useLayerStore } from './providers/layers/store'
import { useMapStore } from './providers/map/provider'
import type { MapComponentProps } from './types'

/**
 * Returns the resolved deck.gl layer instances assigned to a specific map, in their configured order.
 *
 * @param mapId - Identifier of the map whose layers to retrieve.
 * @returns An array of layer objects registered for the given map.
 */
export function useLayers({ mapId }: { mapId: MapComponentProps['id'] }) {
  const { layers: layersMap } = useMapStore((state) => state.maps[mapId]!)
  const layers = useLayerStore((state) => state.layers)

  const computedLayers = useMemo(() => {
    return layersMap
      .map((layerId) => layers[layerId])
      .filter((layer) => !!layer)
  }, [layers, layersMap])

  return computedLayers
}
