"use strict";

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

var _react = require("react");

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

var _reactIntl = require("react-intl");

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

var _get = _interopRequireDefault(require("lodash/get"));

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

var _notificationStatusCodes = require("../../constants/notificationStatusCodes");

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

var _relationListHelpers = require("../../../src/helpers/relationListHelpers");

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

const notificationID = 'hierarchyReparentNotifier';
const messages = (0, _reactIntl.defineMessages)({
  reparentWarning: {
    "id": "hierarchyReparentNotifier.reparentWarning",
    "defaultMessage": "{childName} currently has the broader record {parentName}. Its broader record will be changed when this record is saved."
  }
});
const propTypes = {
  config: _propTypes.default.object.isRequired,
  csid: _propTypes.default.string.isRequired,
  childData: _propTypes.default.instanceOf(_immutable.default.Map).isRequired,
  readRecord: _propTypes.default.func.isRequired,
  removeNotification: _propTypes.default.func.isRequired,
  showNotification: _propTypes.default.func.isRequired
};

class HierarchyReparentNotifier extends _react.Component {
  componentDidMount() {
    this.readRecords(this.props.childData.keySeq().filter(refName => !!refName));
    this.showNotification();
  }

  componentDidUpdate(prevProps) {
    const {
      childData
    } = this.props;
    const {
      childData: prevChildData
    } = prevProps;
    const newRefNames = childData.keySeq().filter(refName => refName && !prevChildData.has(refName));
    this.readRecords(newRefNames);

    if (!childData.equals(prevChildData)) {
      this.showNotification();
    }
  }

  readRecords(refNames) {
    const {
      config,
      readRecord
    } = this.props;
    refNames.forEach(refName => {
      const childRecordType = (0, _refNameHelpers.getRecordType)(config, refName);

      if (!childRecordType) {
        return;
      }

      const childVocabulary = (0, _refNameHelpers.getVocabulary)(config, refName);

      if (!childVocabulary) {
        return;
      }

      const childRecordTypeConfig = (0, _get.default)(config, ['recordTypes', childRecordType]);
      const childVocabularyConfig = (0, _get.default)(childRecordTypeConfig, ['vocabularies', childVocabulary]);
      const childCsid = (0, _refNameHelpers.refNameToCsid)(refName);
      readRecord(config, childRecordTypeConfig, childVocabularyConfig, childCsid);
    });
  }

  showNotification() {
    const {
      csid,
      childData,
      removeNotification,
      showNotification
    } = this.props;
    const items = [];
    childData.forEach((data, childRefName) => {
      if (!childRefName || !data) {
        return;
      }

      const childCsid = (0, _refNameHelpers.refNameToCsid)(childRefName);
      const relations = (0, _relationListHelpers.normalizeRelationList)(data.getIn(['document', 'rel:relations-common-list', 'relation-list-item']));

      if (!relations) {
        return;
      }

      const relation = (0, _relationListHelpers.findBroaderRelation)(childCsid, relations);

      if (relation) {
        const parentCsid = relation.getIn(['object', 'csid']);

        if (parentCsid && parentCsid !== csid) {
          const childName = (0, _cspaceRefname.getDisplayName)(childRefName);
          const parentName = (0, _cspaceRefname.getDisplayName)(relation.getIn(['object', 'refName']));
          items.push({
            message: messages.reparentWarning,
            values: {
              childName,
              parentName
            }
          });
        }
      }
    });

    if (items.length > 0) {
      showNotification({
        items,
        date: new Date(),
        status: _notificationStatusCodes.STATUS_WARNING
      }, notificationID);
    } else {
      removeNotification(notificationID);
    }
  }

  render() {
    return null;
  }

}

exports.default = HierarchyReparentNotifier;
HierarchyReparentNotifier.propTypes = propTypes;
HierarchyReparentNotifier.notificationID = notificationID;