"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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; 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); }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

const propTypes = {
  config: _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
};

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 _react.default.createElement("h3", null, _react.default.createElement("br", null));
    }

    const csid = (0, _recordDataHelpers.getCsid)(data);
    const path = ['/record', recordType, vocabulary, csid].filter(part => !!part).join('/');
    return _react.default.createElement("h3", null, _react.default.createElement(_reactRouterDom.Link, {
      to: path
    }, title));
  }

  render() {
    const _this$props = this.props,
          {
      vocabulary
    } = _this$props,
          remainingProps = _objectWithoutProperties(_this$props, ["vocabulary"]);

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

}

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