'use strict';

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

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 _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _cspaceInput = require('cspace-input');

var _RolesInput = require('../../../styles/cspace-ui/RolesInput.css');

var _RolesInput2 = _interopRequireDefault(_RolesInput);

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 CheckboxInput = _cspaceInput.baseComponents.CheckboxInput;
var _inputHelpers$pathHel = _cspaceInput.helpers.pathHelpers,
    getPath = _inputHelpers$pathHel.getPath,
    pathPropType = _inputHelpers$pathHel.pathPropType;


var propTypes = {
  /* eslint-disable react/no-unused-prop-types */
  name: _propTypes2.default.string,
  parentPath: pathPropType,
  subpath: pathPropType,
  /* eslint-enable react/no-unused-prop-types */
  readOnly: _propTypes2.default.bool,
  value: _propTypes2.default.oneOfType([_propTypes2.default.instanceOf(_immutable2.default.List), _propTypes2.default.instanceOf(_immutable2.default.Map)]),
  roles: _propTypes2.default.instanceOf(_immutable2.default.List),
  readRoles: _propTypes2.default.func,
  onCommit: _propTypes2.default.func
};

var RolesInput = function (_Component) {
  _inherits(RolesInput, _Component);

  function RolesInput(props, context) {
    _classCallCheck(this, RolesInput);

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

    _this.handleCheckboxChange = _this.handleCheckboxChange.bind(_this);
    return _this;
  }

  _createClass(RolesInput, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      var _props = this.props,
          roles = _props.roles,
          readRoles = _props.readRoles;


      if (!roles && readRoles) {
        readRoles();
      }
    }
  }, {
    key: 'getRolesMap',
    value: function getRolesMap() {
      var rolesList = this.props.value;

      if (!rolesList) {
        return undefined;
      }

      if (!_immutable2.default.List.isList(rolesList)) {
        rolesList = _immutable2.default.List.of(rolesList);
      }

      var roles = {};

      rolesList.forEach(function (role) {
        var roleName = role.get('roleName');

        roles[roleName] = true;
      });

      return roles;
    }
  }, {
    key: 'handleCheckboxChange',
    value: function handleCheckboxChange(event) {
      var onCommit = this.props.onCommit;


      if (onCommit) {
        var _event$target = event.target,
            checked = _event$target.checked,
            id = _event$target.id;


        var membership = this.getRolesMap() || {};

        if (checked) {
          membership[id] = true;
        } else {
          delete membership[id];
        }

        var newValue = _immutable2.default.List(Object.keys(membership).map(function (roleName) {
          return _immutable2.default.Map({
            roleName: roleName
          });
        }));

        onCommit(getPath(this.props), newValue);
      }
    }
  }, {
    key: 'renderRoleRows',
    value: function renderRoleRows() {
      var _this2 = this;

      var _props2 = this.props,
          readOnly = _props2.readOnly,
          roles = _props2.roles;


      var membership = this.getRolesMap() || {};

      var rows = roles.map(function (role) {
        var displayName = role.get('displayName');
        var name = role.get('roleName');

        return _react2.default.createElement(
          'li',
          { key: name },
          _react2.default.createElement(CheckboxInput, {
            checked: !!membership[name],
            embedded: true,
            id: name,
            readOnly: readOnly,
            onChange: _this2.handleCheckboxChange
          }),
          _react2.default.createElement(
            'label',
            { htmlFor: name },
            displayName
          )
        );
      });

      return rows.toJS();
    }
  }, {
    key: 'render',
    value: function render() {
      var _props3 = this.props,
          readOnly = _props3.readOnly,
          roles = _props3.roles;


      if (!roles || roles.size === 0) {
        return null;
      }

      var value = this.props.value;


      if (value && !_immutable2.default.List.isList(value)) {
        value = _immutable2.default.List.of(value);
      }

      var className = readOnly ? _RolesInput2.default.readOnly : _RolesInput2.default.common;

      return _react2.default.createElement(
        'ul',
        { className: className },
        this.renderRoleRows()
      );
    }
  }]);

  return RolesInput;
}(_react.Component);

exports.default = RolesInput;


RolesInput.propTypes = propTypes;