'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

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 _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 }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var 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
};

var RecordBrowser = function (_Component) {
  _inherits(RecordBrowser, _Component);

  function RecordBrowser() {
    _classCallCheck(this, RecordBrowser);

    var _this = _possibleConstructorReturn(this, (RecordBrowser.__proto__ || Object.getPrototypeOf(RecordBrowser)).call(this));

    _this.cloneRecord = _this.cloneRecord.bind(_this);
    _this.handleRecordCreated = _this.handleRecordCreated.bind(_this);
    _this.handleRecordTransitioned = _this.handleRecordTransitioned.bind(_this);
    return _this;
  }

  _createClass(RecordBrowser, [{
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var csid = this.props.csid;
      var prevCsid = prevProps.csid;


      if (csid !== prevCsid) {
        this.clearPreferredRelatedCsid();
      }
    }
  }, {
    key: 'componentWillUnmount',
    value: function componentWillUnmount() {
      this.clearPreferredRelatedCsid();
    }
  }, {
    key: 'clearPreferredRelatedCsid',
    value: function clearPreferredRelatedCsid() {
      var clearPreferredRelatedCsid = this.props.clearPreferredRelatedCsid;


      if (clearPreferredRelatedCsid) {
        clearPreferredRelatedCsid();
      }
    }
  }, {
    key: 'cloneRecord',
    value: function cloneRecord() {
      var _props = this.props,
          csid = _props.csid,
          recordType = _props.recordType,
          vocabulary = _props.vocabulary,
          history = _props.history;


      var path = [recordType, vocabulary].filter(function (part) {
        return !!part;
      }).join('/');

      var query = {
        clone: csid
      };

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

      history.push({
        pathname: '/record/' + path,
        search: '?' + queryString
      });
    }
  }, {
    key: 'handleRecordCreated',
    value: function handleRecordCreated(newRecordCsid, isNavigating) {
      if (!isNavigating) {
        var _props2 = this.props,
            history = _props2.history,
            recordType = _props2.recordType,
            vocabulary = _props2.vocabulary;


        var path = [recordType, vocabulary, newRecordCsid].filter(function (part) {
          return !!part;
        }).join('/');

        history.replace('/record/' + path);
      }
    }
  }, {
    key: 'handleRecordTransitioned',
    value: function handleRecordTransitioned(transitionName) {
      var _props3 = this.props,
          history = _props3.history,
          location = _props3.location;


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

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

        var newLocation = void 0;

        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);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var _props4 = this.props,
          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;


      var content = void 0;

      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
        }),
        content
      );
    }
  }]);

  return RecordBrowser;
}(_react.Component);

exports.default = RecordBrowser;


RecordBrowser.propTypes = propTypes;