import { connect } from 'react-redux';
import { isImmutable } from 'immutable';
import * as actionCreators from './actions';
import * as constants from './constants';
import reducer from './reducer';
import * as selectors from './selectors';

const mapState = store => {
  const storeObj = isImmutable(store) ? store.toJS() : store;
  return {
    global: storeObj.global,
    auth: storeObj.global.auth,
    userMode: storeObj.global.userMode,
    loginName: storeObj.global.loginName,
  };
};
const mapDispatch = dispatch => ({
  setAuth: auth => dispatch(actionCreators.setAuth(auth)),
  logout: () => {
    localStorage.setItem('token', '');
    localStorage.setItem('panda-publish', '');
    dispatch(actionCreators.setAuth([]));
  },
  setUserMode: userMode => dispatch(actionCreators.setUserMode(userMode)),
  setLoginName: loginName => dispatch(actionCreators.setLoginName(loginName)),
});

const appConnector = connect(
  mapState,
  mapDispatch,
);
export { actionCreators, constants, reducer, selectors, appConnector };
