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

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

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

var _HierarchySiblingList = _interopRequireDefault(require("../../../styles/cspace-ui/HierarchySiblingList.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 propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  csid: _propTypes.default.string,
  parentCsid: _propTypes.default.string,
  recordType: _propTypes.default.string,
  title: _propTypes.default.node,
  findResult: _propTypes.default.instanceOf(_immutable.default.Map),
  findRelations: _propTypes.default.func
};

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

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

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

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

    if (parentCsid && findRelations) {
      const subject = {
        recordType
      };
      const object = {
        recordType,
        csid: parentCsid
      };
      findRelations(config, subject, object, 'hasBroader');
    }
  }

  render() {
    const {
      config,
      csid,
      findResult,
      title
    } = this.props;
    let siblings;

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

      if (items) {
        if (!_immutable.default.List.isList(items)) {
          items = _immutable.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 /*#__PURE__*/_react.default.createElement("li", {
            key: subject.get('csid')
          }, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, {
            to: url
          }, displayName));
        }).toJS();
      }
    }

    return /*#__PURE__*/_react.default.createElement("div", {
      className: _HierarchySiblingList.default.common
    }, /*#__PURE__*/_react.default.createElement("header", null, title), /*#__PURE__*/_react.default.createElement("ul", null, siblings));
  }

}

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