'use babel';

/* @flow*/

// import { combineReducers } from 'redux';
import type {
  TesterAction,
  TesterState,
} from '../types';

import {
  ADD_TESTER,
  ERROR,
  FILTER_MESSAGES,
  REMOVE_TESTER,
  SORT_MESSAGES,
  START_TEST,
  STOP_TEST,
  TEST,
  UPDATE_EDITOR,
  UPDATE_MESSAGES,
  UPDATE_OUTPUT,
} from './actions';

export default function (
  state: TesterState={
    rawMessages: [],
    currentMessage: null,
    messages: [],
    output: '',
    testRunning: false,
    editor: null,
    sorter: { key: '', desc: false },
    filter: { key: '', value: '' },
    testers: [],
  },
  action: TesterAction): ?TesterState {
  console.log('reducer', state, action);
  switch (action.type) {

    case ADD_TESTER:
      if (action.payload && action.payload.tester) {
        state.testers.push(action.payload.tester);
      }
      return state;
    case REMOVE_TESTER:
      if (action.payload && action.payload.tester) {
        const index = state.testers.indexOf(action.payload.tester);
        state.testers.splice(index, 1);
      }
      return state;

    case TEST:
      return state;
    case START_TEST:
      return Object.assign({}, state, {
        testRunning: true,
      });
    case STOP_TEST:
      return Object.assign({}, state, {
        testRunning: false,
      });

    case FILTER_MESSAGES:
      return Object.assign({}, state, {
        filter: action.payload && action.payload.filter ? action.payload.filter : { key: '', value: '' },
      });
    case SORT_MESSAGES:
      return Object.assign({}, state, {
        sorter: action.payload && action.payload.sorter ? action.payload.sorter : { key: '', desc: false },
      });

    case UPDATE_EDITOR:
      return Object.assign({}, state, {
        editor: (action.payload && action.payload.editor ? action.payload.editor : null),
      });
    case UPDATE_MESSAGES:
      return Object.assign({}, state, {
        messages: (action.payload && action.payload.messages ? action.payload.messages : []),
      });
    case UPDATE_OUTPUT:
      return Object.assign({}, state, {
        output: (action.payload && action.payload.output ? action.payload.output : ''),
      });

    case ERROR:
      return Object.assign({}, state, {
        output: action.errorMessage,
      });
    default: return state;
  }
}

'use babel';

/* @flow*/

// import { combineReducers } from 'redux';
import type {
  TesterAction,
  TesterState,
} from '../types';

import {
  ADD_TESTER,
  ERROR,
  FILTER_MESSAGES,
  REMOVE_TESTER,
  SORT_MESSAGES,
  START_TEST,
  STOP_TEST,
  TEST,
  UPDATE_EDITOR,
  UPDATE_MESSAGES,
  UPDATE_OUTPUT,
} from './actions';

export default function (
  state: TesterState={
    rawMessages: [],
    currentMessage: null,
    messages: [],
    output: '',
    testRunning: false,
    editor: null,
    sorter: { key: '', desc: false },
    filter: { key: '', value: '' },
    testers: [],
  },
  action: TesterAction): ?TesterState {
  console.log('reducer', state, action);
  switch (action.type) {

    case ADD_TESTER:
      if (action.payload && action.payload.tester) {
        if (!state.testers) {
          state.testers = [];
        }
        state.testers.push(action.payload.tester);
      }
      return state;
    case REMOVE_TESTER:
      if (action.payload && action.payload.tester) {
        const index = state.testers.indexOf(action.payload.tester);
        state.testers.splice(index, 1);
      }
      return state;

    case TEST:
      return state;
    case START_TEST:
      return Object.assign({}, state, {
        testRunning: true,
      });
    case STOP_TEST:
      return Object.assign({}, state, {
        testRunning: false,
      });

    case FILTER_MESSAGES:
      return Object.assign({}, state, {
        filter: action.payload && action.payload.filter ? action.payload.filter : { key: '', value: '' },
      });
    case SORT_MESSAGES:
      return Object.assign({}, state, {
        sorter: action.payload && action.payload.sorter ? action.payload.sorter : { key: '', desc: false },
      });

    case UPDATE_EDITOR:
      return Object.assign({}, state, {
        editor: (action.payload && action.payload.editor ? action.payload.editor : null),
      });
    case UPDATE_MESSAGES:
      return Object.assign({}, state, {
        messages: (action.payload && action.payload.messages ? action.payload.messages : []),
      });
    case UPDATE_OUTPUT:
      return Object.assign({}, state, {
        output: (action.payload && action.payload.output ? action.payload.output : ''),
      });

    case ERROR:
      return Object.assign({}, state, {
        output: action.errorMessage,
      });
    default: return state;
  }
}

fix push to testers