'use strict';

exports.__esModule = true;

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _classnames = require('classnames');

var _classnames2 = _interopRequireDefault(_classnames);

require('./style.css');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var Table = function Table(table) {
  var tableWideClass = '';
  var tableHeaderRow = [];
  if (table.head && table.head.rows) {
    tableHeaderRow = table.head.rows[table.head.rows.length - 1];
    if (tableHeaderRow.cells.length > 3) {
      tableWideClass = ' ma__table--wide';
    }
  }
  return _react2.default.createElement(
    'table',
    { id: table.id, className: 'ma__table' + tableWideClass },
    table.head && table.head.rows && _react2.default.createElement(
      'thead',
      null,
      table.head.rows.map(function (row, rowIndex) {
        return _react2.default.createElement(
          'tr',
          { key: row.key || 'thead-row' + rowIndex },
          row.cells.map(function (cell, cellIndex) {
            return _react2.default.createElement(
              'th',
              {
                key: cell.key || 'thead-row' + rowIndex + '-cell' + cellIndex,
                scope: 'col',
                colSpan: cell.colspan || null,
                rowSpan: cell.rowspan || null
              },
              cell.text
            );
          })
        );
      })
    ),
    table.bodies.map(function (body, bodyIndex) {
      return _react2.default.createElement(
        'tbody',
        { key: body.key || 'tbody' + bodyIndex },
        body.rows.map(function (row, rowIndex) {
          var tableIndexOffset = row.rowSpanOffset ? 0 : 1;
          var defaultRowKey = 'tbody' + bodyIndex + '-row' + rowIndex;
          return _react2.default.createElement(
            'tr',
            { key: row.key || defaultRowKey, className: row.rowSpanOffset ? 'is-offset' : null },
            row.cells.map(function (cell, cellIndex) {
              var dataLabel = null;
              var headerCellIndex = cellIndex + 1 - tableIndexOffset;
              if (tableHeaderRow.cells && tableHeaderRow.cells.length > 0 && headerCellIndex > 0) {
                // Mayflower twig loops started at 1 - add one to index.
                dataLabel = tableHeaderRow.cells[headerCellIndex] ? tableHeaderRow.cells[headerCellIndex].text : '';
              }
              var defaultCellKey = defaultRowKey + '-cell' + cellIndex;
              var cellClasses = (0, _classnames2.default)({
                'ma__table-cell': true,
                'no-left-pad': !row.rowSpanOffset && !cell.heading && cellIndex === 1
              });
              return cell.heading ? _react2.default.createElement(
                'th',
                {
                  className: cellClasses,
                  key: cell.key || defaultCellKey,
                  'data-label': dataLabel,
                  scope: 'row',
                  colSpan: cell.colspan || null,
                  rowSpan: cell.rowspan || null
                },
                cell.text
              ) : _react2.default.createElement(
                'td',
                {
                  className: cellClasses,
                  key: cell.key || defaultCellKey,
                  'data-label': dataLabel,
                  colSpan: cell.colspan || null,
                  rowSpan: cell.rowspan || null
                },
                cell.text
              );
            })
          );
        })
      );
    })
  );
};

Table.propTypes = process.env.NODE_ENV !== "production" ? {
  id: _propTypes2.default.string,
  head: _propTypes2.default.shape({
    rows: _propTypes2.default.arrayOf(_propTypes2.default.shape({
      rowSpanOffset: _propTypes2.default.bool,
      cells: _propTypes2.default.arrayOf(_propTypes2.default.shape({
        heading: _propTypes2.default.bool.isRequired,
        colspan: _propTypes2.default.string,
        rowspan: _propTypes2.default.string,
        text: _propTypes2.default.string.isRequired,
        // Optional key for cell in case the table is dynamic.
        key: _propTypes2.default.string
      })),
      // Optional key for row in case the table is dynamic.
      key: _propTypes2.default.string
    }))
  }),
  bodies: _propTypes2.default.arrayOf(_propTypes2.default.shape({
    rows: _propTypes2.default.arrayOf(_propTypes2.default.shape({
      // If true, padding-left is set to zero on the first th/td.
      rowSpanOffset: _propTypes2.default.bool,
      cells: _propTypes2.default.arrayOf(_propTypes2.default.shape({
        heading: _propTypes2.default.bool.isRequired,
        colspan: _propTypes2.default.string,
        rowspan: _propTypes2.default.string,
        text: _propTypes2.default.string.isRequired,
        // Optional key for cell in case the table is dynamic.
        key: _propTypes2.default.string
      })),
      // Optional key for row in case the table is dynamic.
      key: _propTypes2.default.string
    })),
    key: _propTypes2.default.string
  }))
} : {};

Table.defaultProps = {};

exports.default = Table;
module.exports = exports['default'];