"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = RecordSidebar;
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _get = _interopRequireDefault(require("lodash/get"));
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst"));
var _MediaSnapshotPanelContainer = _interopRequireDefault(require("../../containers/record/MediaSnapshotPanelContainer"));
var _RelatedRecordPanelContainer = _interopRequireDefault(require("../../containers/record/RelatedRecordPanelContainer"));
var _RecordBatchPanelContainer = _interopRequireDefault(require("../../containers/record/RecordBatchPanelContainer"));
var _RecordReportPanelContainer = _interopRequireDefault(require("../../containers/record/RecordReportPanelContainer"));
var _TermsUsedPanelContainer = _interopRequireDefault(require("../../containers/record/TermsUsedPanelContainer"));
var _UsedByPanelContainer = _interopRequireDefault(require("../../containers/record/UsedByPanelContainer"));
var _RecordSidebar = _interopRequireDefault(require("../../../styles/cspace-ui/RecordSidebar.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const propTypes = {
  config: _propTypes.default.shape({
    altMediaSnapshot: _propTypes.default.shape({
      mediaRecordType: _propTypes.default.string.isRequired,
      mediaRecordBlobCsidField: _propTypes.default.string.isRequired,
      name: _propTypes.default.string.isRequired,
      sort: _propTypes.default.string.isRequired,
      titleMessage: _propTypes.default.shape({
        id: _propTypes.default.string.isRequired,
        defaultMessage: _propTypes.default.string.isRequired
      }).isRequired
    }),
    mediaSnapshotSort: _propTypes.default.string,
    recordTypes: _propTypes.default.object
  }),
  csid: _propTypes.default.string,
  recordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  history: _propTypes.default.shape({
    push: _propTypes.default.func
  }),
  isOpen: _propTypes.default.bool,
  isRelatable: _propTypes.default.bool
};
const defaultProps = {
  isOpen: true
};
function RecordSidebar(props) {
  const {
    config,
    csid,
    recordType,
    vocabulary,
    history,
    isOpen,
    isRelatable
  } = props;
  if (!isOpen) {
    return null;
  }

  // TODO: Make sidebar components configurable based on service type/record type.

  const recordTypeConfig = config.recordTypes[recordType];
  if (!recordTypeConfig) {
    return null;
  }
  const {
    serviceType
  } = recordTypeConfig.serviceConfig;
  const isAudit = serviceType === 'audit';
  const isAuthority = serviceType === 'authority';
  const isUtility = serviceType === 'utility';
  const panelColor = isAuthority ? 'purple' : 'blue';
  const relatedRecordDescriptors = (0, _get.default)(recordTypeConfig, ['sidebar', 'relatedRecords']) || [{
    recordType: 'collectionobject'
  }, {
    recordType: 'procedure'
  }];
  let mediaSnapshot = null;
  let altMediaSnapshot = null;
  let relatedRecords = null;
  let audit = null;
  let usedBy = null;
  let reports = null;
  let batchJobs = null;
  if (!isAuthority && !isUtility && !isAudit) {
    mediaSnapshot = /*#__PURE__*/_react.default.createElement(_MediaSnapshotPanelContainer.default, {
      color: panelColor,
      csid: csid,
      config: config,
      recordType: recordType,
      sort: config.mediaSnapshotSort
    });
    if (config.altMediaSnapshot) {
      const {
        mediaRecordType,
        mediaRecordBlobCsidField,
        name,
        sort,
        titleMessage
      } = config.altMediaSnapshot;
      altMediaSnapshot = /*#__PURE__*/_react.default.createElement(_MediaSnapshotPanelContainer.default, {
        color: panelColor,
        csid: csid,
        config: config,
        mediaRecordType: mediaRecordType,
        mediaRecordBlobCsidField: mediaRecordBlobCsidField,
        name: name,
        recordType: recordType,
        sort: sort,
        titleMessage: titleMessage
      });
    }
    relatedRecords = relatedRecordDescriptors.map(relatedRecordDescriptor => {
      const {
        sort,
        recordType: relatedRecordType,
        columnSet = 'narrow'
      } = relatedRecordDescriptor;
      return /*#__PURE__*/_react.default.createElement(_RelatedRecordPanelContainer.default, {
        color: panelColor,
        csid: csid,
        columnSetName: columnSet,
        config: config,
        initialSort: sort,
        key: relatedRecordType,
        name: `related${(0, _upperFirst.default)(relatedRecordType)}Panel`,
        recordType: recordType,
        relatedRecordType: relatedRecordType,
        showAddButton: isRelatable
      });
    });
  }
  if (!isAudit) {
    // Temporarily disable until services support exists.
    // audit = (
    //   <RelatedRecordPanelContainer
    //     color={panelColor}
    //     csid={csid}
    //     columnSetName="narrow"
    //     config={config}
    //     initialSort={undefined}
    //     key="audit"
    //     name="relatedAuditPanel"
    //     recordType={recordType}
    //     relatedRecordType="audit"
    //     showAddButton={false}
    //     showSearchButton={false}
    //     listType="audit"
    //   />
    // );
    audit = null;
  }
  if (!isUtility) {
    reports = /*#__PURE__*/_react.default.createElement(_RecordReportPanelContainer.default, {
      color: panelColor,
      csid: csid,
      config: config,
      recordType: recordType
    });
    batchJobs = /*#__PURE__*/_react.default.createElement(_RecordBatchPanelContainer.default, {
      color: panelColor,
      csid: csid,
      config: config,
      history: history,
      recordType: recordType
    });
  }
  if (isAuthority) {
    usedBy = /*#__PURE__*/_react.default.createElement(_UsedByPanelContainer.default, {
      color: panelColor,
      csid: csid,
      config: config,
      recordType: recordType,
      vocabulary: vocabulary
    });
  }
  return /*#__PURE__*/_react.default.createElement("div", {
    className: _RecordSidebar.default[serviceType]
  }, mediaSnapshot, altMediaSnapshot, /*#__PURE__*/_react.default.createElement(_TermsUsedPanelContainer.default, {
    color: panelColor,
    csid: csid,
    config: config,
    recordType: recordType,
    vocabulary: vocabulary
  }), relatedRecords, usedBy, reports, batchJobs, audit);
}
RecordSidebar.propTypes = propTypes;
RecordSidebar.defaultProps = defaultProps;