// @flow
import {connect} from 'react-redux'
import * as I from 'immutable'
import {compose, withState} from 'recompose'
import RolePicker from '.'
import * as Creators from '../../actions/teams/creators'
import * as Constants from '../../constants/teams'

import type {TypedState} from '../../constants/reducer'

type StateProps = {
  _memberInfo: I.Set<Constants.MemberInfo>,
  you: ?string,
  username: string,
  teamname: string,
}

const mapStateToProps = (state: TypedState, {routeProps}): StateProps => ({
  _memberInfo: state.entities.getIn(['teams', 'teamNameToMembers', routeProps.get('teamname')], I.Set()),
  teamname: routeProps.get('teamname'),
  username: routeProps.get('username'),
  you: state.config.username,
})

// TODO add stuff for edit membership options
type DispatchProps = {
  _onAddMember: (
    teamname: Constants.Teamname,
    username: string,
    role: Constants.TeamRoleType,
    sendNotification: boolean
  ) => void,
  _onEditMember: (teamname: Constants.Teamname, username: string, role: Constants.TeamRoleType) => void,
  onBack: () => void,
}

const mapDispatchToProps = (dispatch: Dispatch, {navigateUp}): DispatchProps => ({
  _onAddMember: (teamname, username, role, sendNotification) =>
    dispatch(Creators.addToTeam(teamname, '', username, role, sendNotification)),
  _onEditMember: (teamname, username, role) => dispatch(Creators.editMembership(teamname, username, role)),
  onBack: () => dispatch(navigateUp()),
})

const mergeProps = (stateProps: StateProps, dispatchProps: DispatchProps, ownProps) => {
  const yourInfo = stateProps._memberInfo.find(member => member.username === stateProps.you)
  const user = stateProps._memberInfo.find(member => member.username === stateProps.username)
  const onComplete = (role: Constants.TeamRoleType, sendNotification?: boolean) => {
    if (user) {
      dispatchProps._onEditMember(stateProps.teamname, stateProps.username, role)
    } else {
      dispatchProps._onAddMember(stateProps.teamname, stateProps.username, role, sendNotification || false)
    }
    dispatchProps.onBack()
  }
  const showSendNotification = !user
  return {
    ...stateProps,
    ...dispatchProps,
    ...ownProps,
    allowOwner: yourInfo && yourInfo.type === 'owners',
    onComplete,
    showSendNotification,
    currentType: user && user.type,
  }
}

export default compose(
  connect(mapStateToProps, mapDispatchToProps, mergeProps),
  withState('selectedRole', 'setSelectedRole', props => props.currentType),
  withState('sendNotification', 'setSendNotification', false),
  withState('confirm', 'setConfirm', false)
)(RolePicker)

@@ -2232,17 +2232,16 @@
 = 'owner
-s
 ',%0A    o

Fix owner check