'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _get = require('lodash/get');

var _get2 = _interopRequireDefault(_get);

var _upperFirst = require('lodash/upperFirst');

var _upperFirst2 = _interopRequireDefault(_upperFirst);

var _MediaSnapshotPanelContainer = require('../../containers/record/MediaSnapshotPanelContainer');

var _MediaSnapshotPanelContainer2 = _interopRequireDefault(_MediaSnapshotPanelContainer);

var _RelatedRecordPanelContainer = require('../../containers/record/RelatedRecordPanelContainer');

var _RelatedRecordPanelContainer2 = _interopRequireDefault(_RelatedRecordPanelContainer);

var _RecordBatchPanelContainer = require('../../containers/record/RecordBatchPanelContainer');

var _RecordBatchPanelContainer2 = _interopRequireDefault(_RecordBatchPanelContainer);

var _RecordReportPanelContainer = require('../../containers/record/RecordReportPanelContainer');

var _RecordReportPanelContainer2 = _interopRequireDefault(_RecordReportPanelContainer);

var _TermsUsedPanelContainer = require('../../containers/record/TermsUsedPanelContainer');

var _TermsUsedPanelContainer2 = _interopRequireDefault(_TermsUsedPanelContainer);

var _UsedByPanelContainer = require('../../containers/record/UsedByPanelContainer');

var _UsedByPanelContainer2 = _interopRequireDefault(_UsedByPanelContainer);

var _RecordSidebar = require('../../../styles/cspace-ui/RecordSidebar.css');

var _RecordSidebar2 = _interopRequireDefault(_RecordSidebar);

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

const propTypes = {
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  recordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  history: _propTypes2.default.object,
  isOpen: _propTypes2.default.bool,
  isRelatable: _propTypes2.default.bool
};

const defaultProps = {
  isOpen: true
};

function RecordSidebar(props) {
  const config = props.config,
        csid = props.csid,
        recordType = props.recordType,
        vocabulary = props.vocabulary,
        history = props.history,
        isOpen = props.isOpen,
        isRelatable = props.isRelatable;


  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.serviceType;
  const isAuthority = serviceType === 'authority';
  const isUtility = serviceType === 'utility';
  const panelColor = isAuthority ? 'purple' : 'blue';

  const relatedRecordDescriptors = (0, _get2.default)(recordTypeConfig, ['sidebar', 'relatedRecords']) || [{ recordType: 'collectionobject' }, { recordType: 'procedure' }];

  let mediaSnapshot = null;
  let relatedRecords = null;
  let usedBy = null;
  let reports = null;
  let batchJobs = null;

  if (!isAuthority && !isUtility) {
    mediaSnapshot = _react2.default.createElement(_MediaSnapshotPanelContainer2.default, {
      color: panelColor,
      csid: csid,
      config: config,
      recordType: recordType,
      sort: config.mediaSnapshotSort
    });

    relatedRecords = relatedRecordDescriptors.map(relatedRecordDescriptor => {
      const sort = relatedRecordDescriptor.sort,
            relatedRecordType = relatedRecordDescriptor.recordType;
      var _relatedRecordDescrip = relatedRecordDescriptor.columnSet;
      const columnSet = _relatedRecordDescrip === undefined ? 'narrow' : _relatedRecordDescrip;


      return _react2.default.createElement(_RelatedRecordPanelContainer2.default, {
        color: panelColor,
        csid: csid,
        columnSetName: columnSet,
        config: config,
        history: history,
        initialSort: sort,
        key: relatedRecordType,
        name: `related${(0, _upperFirst2.default)(relatedRecordType)}Panel`,
        recordType: recordType,
        relatedRecordType: relatedRecordType,
        showAddButton: isRelatable
      });
    });
  }

  if (!isUtility) {
    reports = _react2.default.createElement(_RecordReportPanelContainer2.default, {
      color: panelColor,
      csid: csid,
      config: config,
      recordType: recordType
    });

    batchJobs = _react2.default.createElement(_RecordBatchPanelContainer2.default, {
      color: panelColor,
      csid: csid,
      config: config,
      history: history,
      recordType: recordType
    });
  }

  if (isAuthority) {
    usedBy = _react2.default.createElement(_UsedByPanelContainer2.default, {
      color: panelColor,
      csid: csid,
      config: config,
      history: history,
      recordType: recordType,
      vocabulary: vocabulary
    });
  }

  return _react2.default.createElement(
    'div',
    { className: _RecordSidebar2.default[serviceType] },
    mediaSnapshot,
    _react2.default.createElement(_TermsUsedPanelContainer2.default, {
      color: panelColor,
      csid: csid,
      config: config,
      history: history,
      recordType: recordType,
      vocabulary: vocabulary
    }),
    relatedRecords,
    usedBy,
    reports,
    batchJobs
  );
}

RecordSidebar.propTypes = propTypes;
RecordSidebar.defaultProps = defaultProps;