"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _immutable = _interopRequireDefault(require("immutable"));

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

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const {
  CheckboxInput
} = _cspaceInput.baseComponents;
const {
  getPath,
  pathPropType
} = _cspaceInput.helpers.pathHelpers;
const propTypes = {
  /* eslint-disable react/no-unused-prop-types */
  name: _propTypes.default.string,
  parentPath: pathPropType,
  subpath: pathPropType,

  /* eslint-enable react/no-unused-prop-types */
  readOnly: _propTypes.default.bool,
  value: _propTypes.default.oneOfType([_propTypes.default.instanceOf(_immutable.default.List), _propTypes.default.instanceOf(_immutable.default.Map)]),
  roles: _propTypes.default.instanceOf(_immutable.default.List),
  readRoles: _propTypes.default.func,
  onCommit: _propTypes.default.func
};

class RolesInput extends _react.Component {
  constructor(props) {
    super(props);
    this.handleCheckboxChange = this.handleCheckboxChange.bind(this);
  }

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

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

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

    if (!rolesList) {
      return undefined;
    }

    if (!_immutable.default.List.isList(rolesList)) {
      rolesList = _immutable.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;

    if (onCommit) {
      const {
        checked,
        id
      } = event.target;
      const membership = this.getRolesMap() || {};

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

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

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

  renderRoleRows() {
    const {
      readOnly,
      roles
    } = this.props;
    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 /*#__PURE__*/_react.default.createElement("li", {
        key: id
      }, /*#__PURE__*/_react.default.createElement(CheckboxInput, {
        checked: isMember,
        embedded: true,
        id: id,
        readOnly: readOnly,
        onChange: this.handleCheckboxChange
      }), /*#__PURE__*/_react.default.createElement("label", {
        htmlFor: id
      }, displayName));
    }).filter(row => !!row);
    return rows.toJS();
  }

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

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

    let {
      value
    } = this.props;

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

    const rows = this.renderRoleRows();

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

    const className = readOnly ? _RolesInput.default.readOnly : _RolesInput.default.common;
    return /*#__PURE__*/_react.default.createElement("ul", {
      className: className
    }, rows);
  }

}

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