import { PusherMetadataModel } from '../interfaces'
import * as Actions from '../actions'

export const PUSHER_METADATA_REDUCER_KEY = 'metadata'

export interface PusherMetadataState {
    [channel: string]: PusherMetadataModel
}

export const initialPusherMetadataState: PusherMetadataState = {}

export const pusherMetadataReducer =
    (state = initialPusherMetadataState, action: Actions.PusherAction) => {
        switch (action.type) {
            case Actions.SET_PUSHER_METADATA: {
                return setMetadata(state, action)
            }
            default:
                return state
        }
    }

const setMetadata =
    (state: PusherMetadataState, action: Actions.SetPusherMetadataAction): PusherMetadataState => ({
        ...state,
        [action.channel]: action.metadata
    })
