'use strict';

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

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 }; }

const CheckboxInput = _cspaceInput.baseComponents.CheckboxInput;
var _inputHelpers$pathHel = _cspaceInput.helpers.pathHelpers;
const getPath = _inputHelpers$pathHel.getPath,
      pathPropType = _inputHelpers$pathHel.pathPropType;


const 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
};

class RolesInput extends _react.Component {
  constructor(props, context) {
    super(props, context);

    this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
  }

  componentDidMount() {
    var _props = this.props;
    const roles = _props.roles,
          readRoles = _props.readRoles;


    if (!roles && readRoles) {
      readRoles();
    }
  }

  getRolesMap() {
    let rolesList = this.props.value;

    if (!rolesList) {
      return undefined;
    }

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

    const roles = {};

    rolesList.forEach(role => {
      const roleId = role.get('roleId');

      roles[roleId] = true;
    });

    return roles;
  }

  handleCheckboxChange(event) {
    const onCommit = this.props.onCommit;


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


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

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

      const newValue = _immutable2.default.List(Object.keys(membership).map(roleId => _immutable2.default.Map({
        roleId
      })));

      onCommit(getPath(this.props), newValue);
    }
  }

  renderRoleRows() {
    var _props2 = this.props;
    const readOnly = _props2.readOnly,
          roles = _props2.roles;


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

    const rows = roles.map(role => {
      const displayName = role.get('displayName');
      const id = role.get('@csid');
      const isMember = !!membership[id];

      if (readOnly && !isMember) {
        return null;
      }

      return _react2.default.createElement(
        'li',
        { key: id },
        _react2.default.createElement(CheckboxInput, {
          checked: isMember,
          embedded: true,
          id: id,
          readOnly: readOnly,
          onChange: this.handleCheckboxChange
        }),
        _react2.default.createElement(
          'label',
          { htmlFor: id },
          displayName
        )
      );
    }).filter(row => !!row);

    return rows.toJS();
  }

  render() {
    var _props3 = this.props;
    const readOnly = _props3.readOnly,
          roles = _props3.roles;


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

    let value = this.props.value;


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

    const rows = this.renderRoleRows();

    if (rows.length === 0) {
      return null;
    }

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

    return _react2.default.createElement(
      'ul',
      { className: className },
      rows
    );
  }
}

exports.default = RolesInput;
RolesInput.propTypes = propTypes;