// @flow
import {forwardLogs, enableActionLogging, immediateStateLogging} from '../local-debug'
import {noPayloadTransformer} from '../constants/types/flux'
import {stateLogTransformer} from '../constants/reducer'
import {setupLogger, immutableToJS} from '../util/periodic-logger'

function makeActionToLog (action, oldState) {
  if (action.logTransformer) {
    try {
      return action.logTransformer(action, oldState)
    } catch (e) {
      console.warn('Action logger error', e)
    }
  }
  return noPayloadTransformer(action, oldState)
}

const logger = enableActionLogging ? setupLogger('actionLogger', 100, immediateStateLogging, immutableToJS, 50) : {log: () => {}}

export const actionLogger = (store: any) => (next: any) => (action: any) => {
  const oldState = store.getState()
  const actionToLog = makeActionToLog(action, oldState)

  const log1 = [`Dispatching action: ${action.type}: `, forwardLogs ? JSON.stringify(actionToLog) : actionToLog]
  console.log(log1)

  const result = next(action)
  const logState = stateLogTransformer(store.getState())
  logger.log('State:', logState)
  return result
}

// @flow
import {forwardLogs, enableActionLogging, immediateStateLogging} from '../local-debug'
import {noPayloadTransformer} from '../constants/types/flux'
import {stateLogTransformer} from '../constants/reducer'
import {setupLogger, immutableToJS} from '../util/periodic-logger'

function makeActionToLog (action, oldState) {
  if (action.logTransformer) {
    try {
      return action.logTransformer(action, oldState)
    } catch (e) {
      console.warn('Action logger error', e)
    }
  }
  return noPayloadTransformer(action, oldState)
}

const logger = enableActionLogging ? setupLogger('actionLogger', 100, immediateStateLogging, immutableToJS, 50) : {log: () => {}}

export const actionLogger = (store: any) => (next: any) => (action: any) => {
  const oldState = store.getState()
  const actionToLog = makeActionToLog(action, oldState)

  const log1 = [`Dispatching action: ${action.type}: `, forwardLogs ? JSON.stringify(actionToLog) : actionToLog]
  console.log(log1)

  const result = next(action)
  const logState = stateLogTransformer(store.getState())
  logger.log('State:', [JSON.stringify(logState, null, 2)])
  return result
}

Fix unbounded long lines breaking log send for gui
Fix unbounded long lines breaking log send for gui
