"use strict";

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

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

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

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

var _reactRouterDom = require("react-router-dom");

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

var _RecordForm = _interopRequireDefault(require("./RecordForm"));

var _recordDataHelpers = require("../../helpers/recordDataHelpers");

var _MiniView = _interopRequireDefault(require("../../../styles/cspace-ui/MiniView.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; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  intl: _reactIntl.intlShape,
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  csid: _propTypes.default.string,
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  readRecord: _propTypes.default.func
};
const messages = (0, _reactIntl.defineMessages)({
  authority: {
    "id": "miniView.authority",
    "defaultMessage": "{recordType} - {vocabulary}"
  }
});

class MiniView extends _react.Component {
  componentDidMount() {
    this.initRecord();
  }

  componentDidUpdate(prevProps) {
    const {
      csid
    } = this.props;
    const {
      csid: prevCsid
    } = prevProps;

    if (csid !== prevCsid) {
      this.initRecord();
    }
  }

  initRecord() {
    const {
      csid,
      readRecord
    } = this.props;

    if (csid && readRecord) {
      readRecord();
    }
  }

  renderTitle() {
    const {
      config,
      intl,
      recordType,
      vocabulary,
      data
    } = this.props;
    const recordTypeConfig = config.recordTypes[recordType];
    const title = recordTypeConfig.title(data, {
      config,
      intl
    });

    if (!title) {
      return /*#__PURE__*/_react.default.createElement("h3", null, /*#__PURE__*/_react.default.createElement("br", null));
    }

    const csid = (0, _recordDataHelpers.getCsid)(data);
    const path = ['/record', recordType, vocabulary, csid].filter(part => !!part).join('/');
    let secondary;

    if (vocabulary) {
      const values = {
        recordType: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.messages.record.name),
        vocabulary: /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, recordTypeConfig.vocabularies[vocabulary].messages.name)
      };
      secondary = /*#__PURE__*/_react.default.createElement("span", {
        className: _MiniView.default.secondaryTitle
      }, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.authority, {
        values: values
      })));
    }

    return /*#__PURE__*/_react.default.createElement("h3", null, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Link, {
      to: path
    }, title), secondary);
  }

  render() {
    const {
      vocabulary,
      ...remainingProps
    } = this.props;
    const {
      config,
      recordType
    } = this.props;
    const className = vocabulary ? _MiniView.default.authority : _MiniView.default.procedure;
    const recordTypeConfig = config.recordTypes[recordType];
    return /*#__PURE__*/_react.default.createElement("div", {
      className: className
    }, this.renderTitle(), /*#__PURE__*/_react.default.createElement(_RecordForm.default, _extends({
      readOnly: true,
      formName: "mini",
      recordTypeConfig: recordTypeConfig,
      vocabulary: vocabulary
    }, remainingProps)));
  }

}

exports.default = MiniView;
MiniView.propTypes = propTypes;