import { Pusher as PusherInterface } from 'pusher-js'

export const PUSHER_CONNECT = '@@pusher/CONNECT'
export const PUSHER_DISCONNECT = '@@pusher/DISCONNECT'
export const PUSHER_CONNECTION_SUCCESS = '@@pusher/CONNECTION_SUCCESS'
export const PUSHER_CONNECTION_ERROR = '@@pusher/CONNECTION_ERROR'

/**
 * Dispatch this action to attempt to create a connection to pusher
 */
export interface PusherConnectAction {
    type: '@@pusher/CONNECT',
    key: string
    cluster: string
    options?: { [key: string]: any }
}

/**
 * Dispatch this action to end the connection
 */
export interface PusherDisconnectAction {
    type: '@@pusher/DISCONNECT'
}

/**
 * This action will be emitted when the connection is successful
 */
export interface PusherConnectionSuccessAction {
    type: '@@pusher/CONNECTION_SUCCESS',
    client: PusherInterface
    result: any
}

/**
 * This action will be emitted when the connection is not successful
 */
export interface PusherConnectionErrorAction {
    type: '@@pusher/CONNECTION_ERROR',
    error: any
}



export const pusherConnect =
    (key: string, cluster: string, options: any = {}): PusherConnectAction =>
        ({ type: PUSHER_CONNECT, key, cluster })

export const pusherDisconnect =
    (): PusherDisconnectAction =>
        ({ type: PUSHER_DISCONNECT })

export const pusherConnectionSuccess =
    (client: PusherInterface, result: any): PusherConnectionSuccessAction =>
        ({ type: PUSHER_CONNECTION_SUCCESS, client, result })

export const pusherConnectionError =
    (error: any): PusherConnectionErrorAction =>
        ({ type: PUSHER_CONNECTION_ERROR, error })
