<commit_before>import { createSelector } from 'reselect'
import { getMapOverlays, getMapZoom } from '../map/selectors'

export const FETCH_PANEL_ITEMS_REQUEST = 'FETCH_PANEL_ITEMS_REQUEST'
export const FETCH_PANEL_ITEMS_SUCCESS = 'FETCH_PANEL_ITEMS_SUCCESS'
export const FETCH_PANEL_ITEMS_FAILURE = 'FETCH_PANEL_ITEMS_FAILURE'

const initialState = {
  items: [],
  isLoading: false,
  error: null,
}

export const getActiveMapLayers = ({ map, mapLayers, user }) =>
  map.overlays
    .filter((overlay) => overlay.isVisible)
    .map((overlay) => {
      const overlayId = overlay.id.split('-')[1] || overlay.id // The active mapLayer must be matched to the original mapLayer independent of the collection it's in
      const mapLayer = mapLayers.layers.items.find((layer) => layer.id === overlayId) || {}

      return {
        ...mapLayer,
        id: overlay.id, // We want to preserve the ID of the overlay
      }
    })
    .filter((layer) => {
      return (
        layer.detailUrl &&
        !layer.noDetail &&
        (!layer.authScope || user.scopes.includes(layer.authScope))
      )
    })

export const getPanelLayers = (state) => state.mapLayers.panelLayers?.items || []

// Selector to get the mapLayers from the collections in the redux state
export const getMapPanelLayers = createSelector(getPanelLayers, (panelLayers) =>
  panelLayers.length > 0
    ? panelLayers.reduce((acc, cur) => {
        if (cur.mapLayers) {
          return [...acc, ...cur.mapLayers]
        }
        return cur
      }, [])
    : [],
)

export const selectActivePanelLayers = createSelector(
  [getMapPanelLayers, getMapOverlays],
  (panelLayers, overlays) => {
    const mapLayerIds = overlays.map((mapLayer) => mapLayer.id)

    return panelLayers
      .filter((mapLayer) =>
        [mapLayer.id, ...(mapLayer.legendItems?.map((legendItem) => legendItem.id) || [])]
          .filter((mapLayerId) => Boolean(mapLayerId))
          .some((mapLayerId) => mapLayerIds.includes(mapLayerId)),
      )
      .sort((a, b) => {
        const aId = a.id || a.legendItems[0].id
        const bId = b.id || b.legendItems[0].id
        return mapLayerIds.indexOf(bId) - mapLayerIds.indexOf(aId)
      })
  },
)

export const getActiveMapLayersWithinZoom = createSelector(
  [getMapZoom, selectActivePanelLayers],
  (zoomLevel, activePanelLayers) =>
    activePanelLayers.filter(
      (mapLayer) => zoomLevel >= mapLayer.minZoom && zoomLevel <= mapLayer.maxZoom,
    ),
)

export const selectNotClickableVisibleMapLayers = createSelector(
  [getActiveMapLayersWithinZoom, getMapOverlays],
  (activePanelLayers, overlays) =>
    activePanelLayers
      .map((mapLayer) => [mapLayer, ...mapLayer.legendItems])
      .reduce((accumulator, legendItems) => accumulator.concat(legendItems), [])
      .filter((legendItem) => legendItem.noDetail)
      .filter((legendItem) =>
        overlays.some((overlay) => overlay.id === legendItem.id && overlay.isVisible),
      ),
)

export const getLayers = createSelector(
  [getMapPanelLayers, getActiveMapLayers, getMapZoom],
  (panelLayers, activeMapLayers, zoom) =>
    activeMapLayers.filter((layer) => {
      const matchingPanelLayer = panelLayers.find(
        (panelLayer) =>
          panelLayer.id === layer.id ||<commit_msg>Fixed issue where legendItems need to be spread but can be undefined<commit_after>import { createSelector } from 'reselect'
import { getMapOverlays, getMapZoom } from '../map/selectors'

export const FETCH_PANEL_ITEMS_REQUEST = 'FETCH_PANEL_ITEMS_REQUEST'
export const FETCH_PANEL_ITEMS_SUCCESS = 'FETCH_PANEL_ITEMS_SUCCESS'
export const FETCH_PANEL_ITEMS_FAILURE = 'FETCH_PANEL_ITEMS_FAILURE'

const initialState = {
  items: [],
  isLoading: false,
  error: null,
}

export const getActiveMapLayers = ({ map, mapLayers, user }) =>
  map.overlays
    .filter((overlay) => overlay.isVisible)
    .map((overlay) => {
      const overlayId = overlay.id.split('-')[1] || overlay.id // The active mapLayer must be matched to the original mapLayer independent of the collection it's in
      const mapLayer = mapLayers.layers.items.find((layer) => layer.id === overlayId) || {}

      return {
        ...mapLayer,
        id: overlay.id, // We want to preserve the ID of the overlay
      }
    })
    .filter((layer) => {
      return (
        layer.detailUrl &&
        !layer.noDetail &&
        (!layer.authScope || user.scopes.includes(layer.authScope))
      )
    })

export const getPanelLayers = (state) => state.mapLayers.panelLayers?.items || []

// Selector to get the mapLayers from the collections in the redux state
export const getMapPanelLayers = createSelector(getPanelLayers, (panelLayers) =>
  panelLayers.length > 0
    ? panelLayers.reduce((acc, cur) => {
        if (cur.mapLayers) {
          return [...acc, ...cur.mapLayers]
        }
        return cur
      }, [])
    : [],
)

export const selectActivePanelLayers = createSelector(
  [getMapPanelLayers, getMapOverlays],
  (panelLayers, overlays) => {
    const mapLayerIds = overlays.map((mapLayer) => mapLayer.id)

    return panelLayers
      .filter((mapLayer) =>
        [mapLayer.id, ...(mapLayer.legendItems?.map((legendItem) => legendItem.id) || [])]
          .filter((mapLayerId) => Boolean(mapLayerId))
          .some((mapLayerId) => mapLayerIds.includes(mapLayerId)),
      )
      .sort((a, b) => {
        const aId = a.id || a.legendItems[0].id
        const bId = b.id || b.legendItems[0].id
        return mapLayerIds.indexOf(bId) - mapLayerIds.indexOf(aId)
      })
  },
)

export const getActiveMapLayersWithinZoom = createSelector(
  [getMapZoom, selectActivePanelLayers],
  (zoomLevel, activePanelLayers) =>
    activePanelLayers.filter(
      (mapLayer) => zoomLevel >= mapLayer.minZoom && zoomLevel <= mapLayer.maxZoom,
    ),
)

export const selectNotClickableVisibleMapLayers = createSelector(
  [getActiveMapLayersWithinZoom, getMapOverlays],
  (activePanelLayers, overlays) =>
    activePanelLayers
      .map((mapLayer) => [mapLayer, ...(mapLayer.legendItems || [])])
      .reduce((accumulator, legendItems) => accumulator.concat(legendItems), [])
      .filter((legendItem) => legendItem.noDetail)
      .filter((legendItem) =>
        overlays.some((overlay) => overlay.id === legendItem.id && overlay.isVisible),
      ),
)

export const getLayers = createSelector(
  [getMapPanelLayers, getActiveMapLayers, getMapZoom],
  (panelLayers, activeMapLayers, zoom) =>
    activeMapLayers.filter((layer) => {
      const matchingPanelLayer = panelLayers.find(
        (panelLayer) =>
          panelLayer.id === layer.id ||
{'fraction_non_alphanumeric': 0.1335559265442404, 'fraction_numerical': 0.0012141447867658219, 'mean_word_length': 3.3584656084656084, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2758889', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2084, 'n_words': 417}