"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };

var _react = require("react");

var _react2 = _interopRequireDefault(_react);

var _TableField = require("../TableField");

var _reactBootstrapTable = require("react-bootstrap-table");

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

function selectFormatter(selectable) {
  if (selectable) {
    return "<i class='glyphicon glyphicon-ok'></i>";
  } else {
    return "<i class='glyphicon glyphicon-plus'></i>";
  }
}

var SELECTABLE_COLUMN = {
  dataField: "selectable",
  className: "col-md-1",
  columnClassName: "col-md-1",
  dataFormat: selectFormatter
};

var toColumns = function toColumns(schema) {
  var columns = (0, _TableField.toTableColumns)(schema);
  columns.unshift(SELECTABLE_COLUMN);
  columns.forEach(function (column) {
    return column.editable = false;
  });
  return columns;
};

var SelectTable = function SelectTable(_ref) {
  var data = _ref.data,
      schema = _ref.schema,
      onRowClick = _ref.onRowClick;

  var columns = toColumns(schema);
  var options = { onRowClick: onRowClick };
  var selectRow = {
    mode: "radio",
    clickToSelect: true,
    hideSelectColumn: true
  };
  return _react2.default.createElement(
    _reactBootstrapTable.BootstrapTable,
    { data: data, options: options, selectRow: selectRow, hover: true },
    columns.map(function (column, i) {
      return _react2.default.createElement(
        _reactBootstrapTable.TableHeaderColumn,
        _extends({ key: i }, column, { isKey: i === 0 }),
        column.displayName
      );
    })
  );
};

exports.default = SelectTable;