import { SelectionState, IntegratedSelection } from '@devexpress/dx-react-grid'
import { TableSelection } from '@devexpress/dx-react-grid-material-ui'

export default withSelection = (WrappedComponent) =>
  observer class WithSelection extends Component
    constructor: (props) ->
      super props

      @state = observable
        selectedIndexes: []

    @propTypes:
      onChangeSelection: PropTypes.func

    @defaultProps:
      onChangeSelection: =>

    onChangeSelection: (selectedIndexes) =>
      @state.selectedIndexes = selectedIndexes

      selectedRows = @props.rows.filter (row, index) =>
        if index in @state.selectedIndexes
          true

      @props.onChangeSelection selectedRows

    render: =>
      <WrappedComponent
        {...@props}
        selectionStateComponent={<SelectionState selection={@state.selectedIndexes} onSelectionChange={@onChangeSelection} />}
        integratedSelectionComponent={<IntegratedSelection />}
        tableSelectionComponent={<TableSelection showSelectAll />}
      />
