'use strict';

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

var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');

var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _List = require('@material-ui/core/List');

var _List2 = _interopRequireDefault(_List);

var _ListItem = require('@material-ui/core/ListItem');

var _ListItem2 = _interopRequireDefault(_ListItem);

var _ListItemSecondaryAction = require('@material-ui/core/ListItemSecondaryAction');

var _ListItemSecondaryAction2 = _interopRequireDefault(_ListItemSecondaryAction);

var _ListItemText = require('@material-ui/core/ListItemText');

var _ListItemText2 = _interopRequireDefault(_ListItemText);

var _Switch = require('@material-ui/core/Switch');

var _Switch2 = _interopRequireDefault(_Switch);

var _FormattedFieldname = require('../Shared/FormattedFieldname');

var _FormattedFieldname2 = _interopRequireDefault(_FormattedFieldname);

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

var FieldList = function (_React$Component) {
  (0, _inherits3.default)(FieldList, _React$Component);

  function FieldList() {
    (0, _classCallCheck3.default)(this, FieldList);
    return (0, _possibleConstructorReturn3.default)(this, (FieldList.__proto__ || (0, _getPrototypeOf2.default)(FieldList)).apply(this, arguments));
  }

  (0, _createClass3.default)(FieldList, [{
    key: 'handleToggle',
    value: function handleToggle(value, e) {
      var _props = this.props,
          fields = _props.fields,
          visibleFields = _props.visibleFields;
      // Keep order of fields in visibleFields

      var newVisibleFields = fields.filter(function (field) {
        return e.target.checked && (field === value || visibleFields.indexOf(field) > -1) || !e.target.checked && field !== value && visibleFields.indexOf(field) > -1;
      });
      this.props.onUpdate(newVisibleFields);
    }
  }, {
    key: 'render',
    value: function render() {
      var _this2 = this;

      var _props2 = this.props,
          fields = _props2.fields,
          visibleFields = _props2.visibleFields;


      return _react2.default.createElement(
        _List2.default,
        { dense: true },
        fields.map(function (field) {
          return _react2.default.createElement(
            _ListItem2.default,
            {
              key: field,
              style: !visibleFields.includes(field) ? { opacity: 0.4 } : {} },
            _react2.default.createElement(_ListItemText2.default, { primary: _react2.default.createElement(_FormattedFieldname2.default, { fieldname: field }) }),
            _react2.default.createElement(
              _ListItemSecondaryAction2.default,
              null,
              _react2.default.createElement(_Switch2.default, {
                checked: visibleFields.includes(field),
                onChange: function onChange(e) {
                  return _this2.handleToggle(field, e);
                }
              })
            )
          );
        })
      );
    }
  }]);
  return FieldList;
}(_react2.default.Component);

FieldList.propTypes = {
  fields: _propTypes2.default.array.isRequired,
  onUpdate: _propTypes2.default.func.isRequired,
  visibleFields: _propTypes2.default.array.isRequired
};
exports.default = FieldList;
//# sourceMappingURL=FieldList.js.map