'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

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

var _get2 = _interopRequireDefault(_get);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _RecordBrowserNavBarContainer = require('../../containers/record/RecordBrowserNavBarContainer');

var _RecordBrowserNavBarContainer2 = _interopRequireDefault(_RecordBrowserNavBarContainer);

var _RecordSidebarToggleButtonContainer = require('../../containers/record/RecordSidebarToggleButtonContainer');

var _RecordSidebarToggleButtonContainer2 = _interopRequireDefault(_RecordSidebarToggleButtonContainer);

var _RecordEditorContainer = require('../../containers/record/RecordEditorContainer');

var _RecordEditorContainer2 = _interopRequireDefault(_RecordEditorContainer);

var _RelatedRecordBrowserContainer = require('../../containers/record/RelatedRecordBrowserContainer');

var _RelatedRecordBrowserContainer2 = _interopRequireDefault(_RelatedRecordBrowserContainer);

var _searchHelpers = require('../../helpers/searchHelpers');

var _RecordBrowser = require('../../../styles/cspace-ui/RecordBrowser.css');

var _RecordBrowser2 = _interopRequireDefault(_RecordBrowser);

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

const propTypes = {
  cloneCsid: _propTypes2.default.string,
  config: _propTypes2.default.object,
  csid: _propTypes2.default.string,
  dockTop: _propTypes2.default.number,
  history: _propTypes2.default.object,
  location: _propTypes2.default.object,
  recordType: _propTypes2.default.string,
  relatedCsid: _propTypes2.default.string,
  relatedRecordType: _propTypes2.default.string,
  vocabulary: _propTypes2.default.string,
  workflowState: _propTypes2.default.string,
  clearPreferredRelatedCsid: _propTypes2.default.func,
  onShowRelated: _propTypes2.default.func
};

class RecordBrowser extends _react.Component {
  constructor() {
    super();

    this.cloneRecord = this.cloneRecord.bind(this);
    this.handleRecordCreated = this.handleRecordCreated.bind(this);
    this.handleRecordTransitioned = this.handleRecordTransitioned.bind(this);
  }

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


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

  componentWillUnmount() {
    this.clearPreferredRelatedCsid();
  }

  clearPreferredRelatedCsid() {
    const clearPreferredRelatedCsid = this.props.clearPreferredRelatedCsid;


    if (clearPreferredRelatedCsid) {
      clearPreferredRelatedCsid();
    }
  }

  cloneRecord() {
    var _props = this.props;
    const csid = _props.csid,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary,
          history = _props.history;


    const path = [recordType, vocabulary].filter(part => !!part).join('/');

    const query = {
      clone: csid
    };

    const queryString = _qs2.default.stringify(query);

    history.push({
      pathname: `/record/${path}`,
      search: `?${queryString}`
    });
  }

  handleRecordCreated(newRecordCsid, isNavigating) {
    if (!isNavigating) {
      var _props2 = this.props;
      const history = _props2.history,
            recordType = _props2.recordType,
            vocabulary = _props2.vocabulary;


      const path = [recordType, vocabulary, newRecordCsid].filter(part => !!part).join('/');

      history.replace(`/record/${path}`);
    }
  }

  handleRecordTransitioned(transitionName) {
    var _props3 = this.props;
    const history = _props3.history,
          location = _props3.location;


    if (transitionName === 'delete') {
      // The record was soft-deleted.

      const searchDescriptor = (0, _get2.default)(location, ['state', 'searchDescriptor']);

      let newLocation;

      if (searchDescriptor) {
        // We came from a search. Return to the search result page.

        newLocation = (0, _searchHelpers.searchDescriptorToLocation)(_immutable2.default.fromJS(searchDescriptor));
      } else {
        // Go to the index page.

        newLocation = '/';
      }

      history.replace(newLocation);
    }
  }

  render() {
    var _props4 = this.props;
    const cloneCsid = _props4.cloneCsid,
          config = _props4.config,
          csid = _props4.csid,
          dockTop = _props4.dockTop,
          history = _props4.history,
          location = _props4.location,
          recordType = _props4.recordType,
          relatedCsid = _props4.relatedCsid,
          relatedRecordType = _props4.relatedRecordType,
          vocabulary = _props4.vocabulary,
          workflowState = _props4.workflowState,
          onShowRelated = _props4.onShowRelated;


    let content;

    if (relatedRecordType) {
      content = _react2.default.createElement(_RelatedRecordBrowserContainer2.default, {
        cloneCsid: cloneCsid,
        config: config,
        history: history,
        location: location,
        recordType: recordType,
        csid: csid,
        relatedRecordType: relatedRecordType,
        relatedCsid: relatedCsid,
        workflowState: workflowState,
        onShowRelated: onShowRelated
      });
    } else {
      content = _react2.default.createElement(_RecordEditorContainer2.default, {
        cloneCsid: cloneCsid,
        config: config,
        csid: csid,
        dockTop: dockTop,
        recordType: recordType,
        vocabulary: vocabulary,
        clone: this.cloneRecord,
        onRecordCreated: this.handleRecordCreated,
        onRecordTransitioned: this.handleRecordTransitioned
      });
    }

    return _react2.default.createElement(
      'div',
      { className: _RecordBrowser2.default.common },
      _react2.default.createElement(_RecordBrowserNavBarContainer2.default, {
        config: config,
        csid: csid,
        recordType: recordType,
        relatedRecordType: relatedRecordType,
        onSelect: onShowRelated
      }),
      _react2.default.createElement(_RecordSidebarToggleButtonContainer2.default, {
        recordType: recordType,
        config: config
      }),
      content
    );
  }
}

exports.default = RecordBrowser;
RecordBrowser.propTypes = propTypes;