"use strict";

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

var _react = _interopRequireWildcard(require("react"));

var _propTypes = _interopRequireDefault(require("prop-types"));

var _qs = _interopRequireDefault(require("qs"));

var _get = _interopRequireDefault(require("lodash/get"));

var _immutable = _interopRequireDefault(require("immutable"));

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

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

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

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

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

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

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

function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; }

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

const propTypes = {
  cloneCsid: _propTypes.default.string,
  config: _propTypes.default.object,
  csid: _propTypes.default.string,
  dockTop: _propTypes.default.number,
  history: _propTypes.default.object,
  location: _propTypes.default.object,
  recordType: _propTypes.default.string,
  relatedCsid: _propTypes.default.string,
  relatedRecordType: _propTypes.default.string,
  vocabulary: _propTypes.default.string,
  workflowState: _propTypes.default.string,
  clearPreferredRelatedCsid: _propTypes.default.func,
  onShowRelated: _propTypes.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;
    const {
      csid: prevCsid
    } = prevProps;

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

  componentWillUnmount() {
    this.clearPreferredRelatedCsid();
  }

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

    if (clearPreferredRelatedCsid) {
      clearPreferredRelatedCsid();
    }
  }

  cloneRecord() {
    const {
      csid,
      recordType,
      vocabulary,
      history
    } = this.props;
    const path = [recordType, vocabulary].filter(part => !!part).join('/');
    const query = {
      clone: csid
    };

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

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

  handleRecordCreated(newRecordCsid, isNavigating) {
    if (!isNavigating) {
      const {
        history,
        recordType,
        vocabulary
      } = this.props;
      const path = [recordType, vocabulary, newRecordCsid].filter(part => !!part).join('/');
      history.replace("/record/".concat(path));
    }
  }

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

    if (transitionName === 'delete') {
      // The record was soft-deleted.
      const searchDescriptor = (0, _get.default)(location, ['state', 'searchDescriptor']);
      let newLocation;

      if (searchDescriptor) {
        // We came from a search. Return to the search result page.
        newLocation = (0, _searchHelpers.searchDescriptorToLocation)(_immutable.default.fromJS(searchDescriptor));
      } else {
        // Go to the index page.
        newLocation = '/';
      }

      history.replace(newLocation);
    }
  }

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

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

    return _react.default.createElement("div", {
      className: _RecordBrowser.default.common
    }, _react.default.createElement(_RecordBrowserNavBarContainer.default, {
      config: config,
      csid: csid,
      recordType: recordType,
      relatedRecordType: relatedRecordType,
      onSelect: onShowRelated
    }), _react.default.createElement(_RecordSidebarToggleButtonContainer.default, {
      recordType: recordType,
      config: config
    }), content);
  }

}

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