import PropTypes from 'prop-types'
import React from 'react'
import {intlShape} from 'react-intl'

import {HeaderRow, ToManyRelationRow, FieldRow, RelationRow} from './table-components'
import {StyledMergeMatrixTable} from './StyledMergeMatrix'

const MergeMatrix = props => {
  const targetEntity = props.entities.find(e => e.pk === props.targetEntityPk)

  return (
    <StyledMergeMatrixTable>
      <thead>
        <HeaderRow
          entities={props.entities}
          changeTargetEntity={props.changeTargetEntity}
          selectSourceField={props.selectSourceField}
          targetEntityPk={props.targetEntityPk}
          intl={props.intl}
        />
      </thead>
      <tbody>
        {
          props.model.fields.map((field, idx) =>
            <FieldRow
              key={`fieldrow${idx}`}
              field={field}
              selections={props.selections}
              targetEntity={targetEntity}
              entities={props.entities}
              selectSourceField={props.selectSourceField}
            />
          )
        }{
          props.model.relations.filter(r => !r.toMany).map((relation, idx) =>
            <RelationRow
              key={`relationrow${idx}`}
              relation={relation}
              selections={props.selections}
              targetEntity={targetEntity}
              entities={props.entities}
              selectSourceRelation={props.selectSourceRelation}
              toggleRelationMany={props.toggleRelationMany}
            />
          )
        }{
          props.model.relations.filter(r => r.toMany).map((relation, idx) =>
            <ToManyRelationRow
              key={`relationrow${idx}`}
              relation={relation}
              selections={props.selections}
              targetEntity={targetEntity}
              entities={props.entities}
              toggleRelationMany={props.toggleRelationMany}
            />
          )
        }
      </tbody>
    </StyledMergeMatrixTable>
  )
}

MergeMatrix.propTypes = {
  entities: PropTypes.array.isRequired,
  model: PropTypes.object.isRequired,
  targetEntityPk: PropTypes.string,
  selections: PropTypes.object,
  changeTargetEntity: PropTypes.func.isRequired,
  selectSourceField: PropTypes.func.isRequired,
  selectSourceRelation: PropTypes.func.isRequired,
  toggleRelationMany: PropTypes.func.isRequired,
  intl: intlShape.isRequired
}

export default MergeMatrix

@@ -1979,99 +1979,449 @@
 %0A%7D%0A%0A
-MergeMatrix.propTypes = %7B%0A  entities: PropTypes.array.isRequired,%0A  model: PropTypes.object
+const fieldsPropType = PropTypes.shape(%7B%0A  label: PropTypes.string,%0A  name: PropTypes.string%0A%7D)%0A%0Aconst relationsPropType = PropTypes.shape(%7B%0A  label: PropTypes.string,%0A  name: PropTypes.string,%0A  toMany: PropTypes.bool%0A%7D)%0A%0AMergeMatrix.propTypes = %7B%0A  entities: PropTypes.array.isRequired,%0A  model: PropTypes.shape(%7B%0A    fields: PropTypes.arrayOf(fieldsPropType).isRequired,%0A    relations: PropTypes.arrayOf(relationsPropType).isRequired%0A  %7D)
 .isR

check type of model.relations and model.fields