import { Map } from 'immutable';
import { createSelector } from 'reselect';

import { getActiveEventId } from './activeEvent.js';

const getState = (state) => state;

export const getEventById = (state, eventId) => state.getIn(['events', eventId], false);

export const getEvents = createSelector(
    [getState],
    (state) => state.get('events') || new Map(),
);

export const getActiveEvent = createSelector(
    [getActiveEventId, getEvents],
    (eventId, eventsAsMap) => (eventId ? eventsAsMap.get(eventId) : null),
);

export const getDefaultEvent = createSelector(
    [getEvents],
    (eventsAsMap) => eventsAsMap.first(),
);

export const getEventsAsList = createSelector(
    [getEvents],
    (eventsAsMap) => eventsAsMap.toList(),
);

export const hasMultipleEvents = createSelector(
    [getEvents],
    (eventsAsMap) => eventsAsMap.size > 1,
);
