'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 _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _dateInputField = require('./tableComponents/dateInputField');

var _dateInputField2 = _interopRequireDefault(_dateInputField);

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var AsyncTableWidget = function (_React$Component) {
  _inherits(AsyncTableWidget, _React$Component);

  function AsyncTableWidget(props) {
    _classCallCheck(this, AsyncTableWidget);

    var _this = _possibleConstructorReturn(this, (AsyncTableWidget.__proto__ || Object.getPrototypeOf(AsyncTableWidget)).call(this, props));

    _this._handleTableUpdate = _this._handleTableUpdate.bind(_this);
    _this._handleRowDelete = _this._handleRowDelete.bind(_this);
    return _this;
  }

  _createClass(AsyncTableWidget, [{
    key: '_handleTableUpdate',
    value: function _handleTableUpdate() {
      this.props.onChange(JSON.stringify(this.props.widgetData));
    }
  }, {
    key: '_handleRowDelete',
    value: function _handleRowDelete(rowKeys) {
      var filteredRows = [];
      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = this.props.widgetData.list[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          var row = _step.value;

          var flag = true;
          var _iteratorNormalCompletion2 = true;
          var _didIteratorError2 = false;
          var _iteratorError2 = undefined;

          try {
            for (var _iterator2 = rowKeys[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
              var key = _step2.value;

              if (row[this.props.widgetData.keyField] === key) flag = false;
            }
          } catch (err) {
            _didIteratorError2 = true;
            _iteratorError2 = err;
          } finally {
            try {
              if (!_iteratorNormalCompletion2 && _iterator2.return) {
                _iterator2.return();
              }
            } finally {
              if (_didIteratorError2) {
                throw _iteratorError2;
              }
            }
          }

          if (flag) filteredRows = filteredRows.concat(row);
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      var newWidgetData = this.props.widgetData;
      newWidgetData.list = filteredRows;
      this.props.onChange(JSON.stringify(newWidgetData));
    }
  }, {
    key: 'render',
    value: function render() {
      var cellEditProp = {
        mode: 'click',
        blurToSave: true
      };

      var options = {
        handleConfirmDeleteRow: this._deleteConfirmation,
        afterTableComplete: this._handleTableUpdate,
        afterDeleteRow: this._handleRowDelete
      };

      var selectRowProp = {
        mode: 'checkbox'
      };

      var dateEditor = function dateEditor(onUpdate, props) {
        return _react2.default.createElement(_dateInputField2.default, _extends({ onUpdate: onUpdate }, props));
      };

      return _react2.default.createElement(
        _reactBootstrapTable.BootstrapTable,
        { data: this.props.widgetData.list, keyField: this.props.widgetData.keyField, cellEdit: cellEditProp, deleteRow: true, selectRow: selectRowProp, options: options },
        this.props.widgetData.tableCols.map(function (row, i) {
          if (row.customFieldType) return _react2.default.createElement(
            _reactBootstrapTable.TableHeaderColumn,
            { dataField: row.field, customEditor: { getElement: dateEditor }, key: i, editable: true },
            ' ',
            row.displayName,
            ' '
          );else return _react2.default.createElement(
            _reactBootstrapTable.TableHeaderColumn,
            { dataField: row.field, key: i, editable: row.editable },
            ' ',
            row.displayName,
            ' '
          );
        })
      );
    }
  }, {
    key: '_deleteConfirmation',
    value: function _deleteConfirmation(next, dropRowKeys) {
      var dropRowKeysStr = dropRowKeys.join(',');
      if (confirm('Are you sure you want to delete ' + dropRowKeysStr + '?')) {
        next();
      }
    }
  }]);

  return AsyncTableWidget;
}(_react2.default.Component);

exports.default = AsyncTableWidget;