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

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

class HierarchySiblingList extends _react.Component {
  componentDidMount() {
    this.findRelations();
  }

  componentDidUpdate(prevProps) {
    const parentCsid = this.props.parentCsid;
    const prevParentCsid = prevProps.parentCsid;


    if (parentCsid !== prevParentCsid) {
      this.findRelations();
    }
  }

  findRelations() {
    var _props = this.props;
    const config = _props.config,
          parentCsid = _props.parentCsid,
          recordType = _props.recordType,
          findRelations = _props.findRelations;


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

      const object = {
        recordType,
        csid: parentCsid
      };

      findRelations(config, subject, object, 'hasBroader');
    }
  }

  render() {
    var _props2 = this.props;
    const config = _props2.config,
          csid = _props2.csid,
          findResult = _props2.findResult,
          title = _props2.title;


    let siblings;

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

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

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

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

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

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

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

exports.default = HierarchySiblingList;
HierarchySiblingList.propTypes = propTypes;