'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 _reactRouterDom = require('react-router-dom');

var _cspaceRefname = require('cspace-refname');

var _refNameHelpers = require('../../helpers/refNameHelpers');

var _HierarchySiblingList = require('../../../styles/cspace-ui/HierarchySiblingList.css');

var _HierarchySiblingList2 = _interopRequireDefault(_HierarchySiblingList);

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 propTypes = {
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  parentCsid: _propTypes2.default.string,
  recordType: _propTypes2.default.string,
  title: _propTypes2.default.node,
  findResult: _propTypes2.default.instanceOf(_immutable2.default.Map),
  findRelations: _propTypes2.default.func
};

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

  function HierarchySiblingList() {
    _classCallCheck(this, HierarchySiblingList);

    return _possibleConstructorReturn(this, (HierarchySiblingList.__proto__ || Object.getPrototypeOf(HierarchySiblingList)).apply(this, arguments));
  }

  _createClass(HierarchySiblingList, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.findRelations();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var parentCsid = this.props.parentCsid;
      var prevParentCsid = prevProps.parentCsid;


      if (parentCsid !== prevParentCsid) {
        this.findRelations();
      }
    }
  }, {
    key: 'findRelations',
    value: function findRelations() {
      var _props = this.props,
          config = _props.config,
          parentCsid = _props.parentCsid,
          recordType = _props.recordType,
          findRelations = _props.findRelations;


      if (parentCsid && findRelations) {
        var subject = {
          recordType: recordType
        };

        var object = {
          recordType: recordType,
          csid: parentCsid
        };

        findRelations(config, subject, object, 'hasBroader');
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props2 = this.props,
          config = _props2.config,
          csid = _props2.csid,
          findResult = _props2.findResult,
          title = _props2.title;


      var siblings = void 0;

      if (findResult) {
        var list = findResult.get('rel:relations-common-list');

        var items = list.get('relation-list-item');

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

          items = items.filter(function (item) {
            return item.getIn(['subject', 'csid']) !== csid;
          }).sort(function (itemA, itemB) {
            return itemA.getIn(['subject', 'number']).localeCompare(itemB.getIn(['subject', 'number']));
          });

          var lastIndex = items.size - 1;

          siblings = items.map(function (item, index) {
            var subject = item.get('subject');
            var refName = subject.get('refName');
            var displayName = (0, _cspaceRefname.getDisplayName)(refName);
            var url = (0, _refNameHelpers.refNameToUrl)(config, refName);

            return _react2.default.createElement(
              'li',
              { key: subject.get('csid') },
              _react2.default.createElement(
                _reactRouterDom.Link,
                { to: url },
                displayName
              ),
              index < lastIndex ? ', ' : ''
            );
          }).toJS();
        }
      }

      return _react2.default.createElement(
        'div',
        { className: _HierarchySiblingList2.default.common },
        _react2.default.createElement(
          'header',
          null,
          title
        ),
        _react2.default.createElement(
          'ul',
          null,
          siblings
        )
      );
    }
  }]);

  return HierarchySiblingList;
}(_react.Component);

exports.default = HierarchySiblingList;


HierarchySiblingList.propTypes = propTypes;