"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.getParams = exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _immutable = _interopRequireDefault(require("immutable"));
var _qs = _interopRequireDefault(require("qs"));
var _get = _interopRequireDefault(require("lodash/get"));
var _ErrorPage = _interopRequireDefault(require("./ErrorPage"));
var _RecordTitleBarContainer = _interopRequireDefault(require("../../containers/record/RecordTitleBarContainer"));
var _RecordBrowserContainer = _interopRequireDefault(require("../../containers/record/RecordBrowserContainer"));
var _RecordSidebar = _interopRequireDefault(require("../record/RecordSidebar"));
var _configHelpers = require("../../helpers/configHelpers");
var _permissionHelpers = require("../../helpers/permissionHelpers");
var _refNameHelpers = require("../../helpers/refNameHelpers");
var _recordDataHelpers = require("../../helpers/recordDataHelpers");
var _RecordPage = _interopRequireDefault(require("../../../styles/cspace-ui/RecordPage.css"));
var _PageBody = _interopRequireDefault(require("../../../styles/cspace-ui/PageBody.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && 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 getParams = props => {
  const {
    config,
    match
  } = props;
  const {
    recordType,
    path1,
    path2,
    path3
  } = match.params;
  let vocabulary;
  let csid;
  let relatedRecordType;
  let relatedCsid;
  const recordTypeConfig = (0, _get.default)(config, ['recordTypes', recordType]);
  if (recordTypeConfig) {
    const serviceType = (0, _get.default)(recordTypeConfig, ['serviceConfig', 'serviceType']);
    if (serviceType === 'authority') {
      vocabulary = path1;
      csid = path2;
      relatedRecordType = path3;
    } else {
      csid = path1;
      relatedRecordType = path2;
      relatedCsid = path3;
    }
  }
  return {
    recordType,
    vocabulary,
    csid,
    relatedRecordType,
    relatedCsid
  };
};
exports.getParams = getParams;
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }).isRequired,
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  error: _propTypes.default.instanceOf(_immutable.default.Map),
  history: _propTypes.default.shape({
    replace: _propTypes.default.func
  }),
  isSidebarOpen: _propTypes.default.bool,
  location: _propTypes.default.shape({
    search: _propTypes.default.string,
    state: _propTypes.default.object
  }),
  // Use of the match prop isn't being detected by eslint.
  // eslint-disable-next-line react/no-unused-prop-types
  match: _propTypes.default.shape({
    params: _propTypes.default.object
  }),
  perms: _propTypes.default.instanceOf(_immutable.default.Map),
  clearRecord: _propTypes.default.func,
  readRecord: _propTypes.default.func,
  setRecordPagePrimaryCsid: _propTypes.default.func
};
class RecordPage extends _react.Component {
  constructor() {
    super();
    this.handleShowRelated = this.handleShowRelated.bind(this);
    this.handleTitleBarDocked = this.handleTitleBarDocked.bind(this);
    this.state = {
      headerDockPosition: null
    };
  }
  componentDidMount() {
    this.initRecord();
  }
  componentDidUpdate(prevProps) {
    const params = getParams(this.props);
    const prevParams = getParams(prevProps);
    const {
      recordType,
      vocabulary,
      csid
    } = params;
    const {
      csid: prevCsid
    } = prevParams;
    const {
      config,
      data,
      history,
      location,
      perms,
      clearRecord
    } = this.props;
    const {
      perms: prevPerms
    } = prevProps;
    if (csid !== prevCsid || perms !== prevPerms) {
      this.initRecord();
    } else if ((recordType === 'all' || vocabulary === 'all') && data) {
      // If we got here via an 'all' record type or vocabulary, redirect to the correct record type
      // and vocabulary page once we have data.

      const dataCsid = (0, _recordDataHelpers.getCsid)(data);

      // TODO: This workaround for DRYD-487 can be removed when that issue is fixed. For now we
      // need to clear the record data retrieved through servicegroups/common/items, since it
      // won't have returned the hierarchy relations. Clearing the existing record data will cause
      // it to be retrieved again through the specific record service, so that the hierarchy
      // relations will be included.

      if (clearRecord) {
        clearRecord(dataCsid, true);
      }
      history.replace({
        pathname: (0, _refNameHelpers.refNameToUrl)(config, (0, _recordDataHelpers.getRefName)(data), dataCsid),
        state: location.state
      });
    }
  }
  componentWillUnmount() {
    const {
      setRecordPagePrimaryCsid
    } = this.props;
    if (setRecordPagePrimaryCsid) {
      setRecordPagePrimaryCsid(undefined);
    }
  }
  handleShowRelated(relatedRecordType, relatedCsid) {
    const {
      recordType,
      vocabulary,
      csid
    } = getParams(this.props);
    const {
      history,
      location
    } = this.props;
    const path = [recordType, vocabulary, csid, relatedRecordType, relatedCsid].filter(part => !!part).join('/');
    history.replace({
      pathname: `/record/${path}`,
      state: location.state
    });
  }
  handleTitleBarDocked(height) {
    this.setState({
      headerDockPosition: height
    });
  }
  initRecord() {
    const {
      config,
      readRecord,
      setRecordPagePrimaryCsid
    } = this.props;
    const {
      recordType,
      vocabulary,
      csid
    } = getParams(this.props);
    const normalizedCsid = csid === 'new' ? '' : csid;
    if (setRecordPagePrimaryCsid) {
      setRecordPagePrimaryCsid(csid);
    }
    if (normalizedCsid && readRecord) {
      // If vocabulary is 'all', use the 'all' record type, since we can't get a record by csid in
      // an authority without specifying a vocabulary, but we can get a record by csid without
      // specifying any record type.

      const recordTypeConfig = (0, _get.default)(config, ['recordTypes', vocabulary === 'all' ? 'all' : recordType]);
      const vocabularyConfig = vocabulary && vocabulary !== 'all' ? (0, _get.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;
      readRecord(config, recordTypeConfig, vocabularyConfig, normalizedCsid);
    }
  }
  render() {
    const {
      headerDockPosition
    } = this.state;
    const {
      config,
      data,
      error,
      history,
      isSidebarOpen,
      location,
      perms
    } = this.props;
    if (error) {
      return /*#__PURE__*/_react.default.createElement(_ErrorPage.default, {
        error: error.toJS()
      });
    }
    const {
      recordType,
      vocabulary,
      csid,
      relatedRecordType,
      relatedCsid
    } = getParams(this.props);
    if (recordType === 'all' || vocabulary === 'all') {
      // Render nothing for the 'all' record type/vocabulary. We'll redirect to the appropriate
      // page once we've determined what the actual record type/vocabulary are.

      return null;
    }
    const query = _qs.default.parse(location.search.substring(1));
    const cloneCsid = query.clone;
    const normalizedCsid = csid === 'new' ? '' : csid || '';
    const normalizedRelatedCsid = relatedCsid === 'new' ? '' : relatedCsid;
    const validation = (0, _configHelpers.validateLocation)(config, {
      recordType,
      vocabulary,
      relatedRecordType,
      csid: normalizedCsid,
      relatedCsid: normalizedRelatedCsid
    });
    if (validation.error) {
      return /*#__PURE__*/_react.default.createElement(_ErrorPage.default, {
        error: validation.error
      });
    }
    const searchName = (0, _get.default)(location, ['state', 'searchName']);
    const searchDescriptor = _immutable.default.fromJS((0, _get.default)(location, ['state', 'searchDescriptor']));
    const originSearchPageState = (0, _get.default)(location, ['state', 'originSearchPage']);
    const serviceType = (0, _get.default)(config, ['recordTypes', recordType, 'serviceConfig', 'serviceType']);
    const workflowState = (0, _recordDataHelpers.getWorkflowState)(data);
    const isRelatable = workflowState !== 'locked' && (0, _permissionHelpers.canRelate)(recordType, perms, config);
    return /*#__PURE__*/_react.default.createElement("div", {
      className: _RecordPage.default[serviceType]
    }, /*#__PURE__*/_react.default.createElement(_RecordTitleBarContainer.default, {
      csid: normalizedCsid,
      recordType: recordType,
      vocabulary: vocabulary,
      searchName: searchName,
      searchDescriptor: searchDescriptor,
      originSearchPageState: originSearchPageState,
      updateDocumentTitle: true,
      onDocked: this.handleTitleBarDocked
    }), /*#__PURE__*/_react.default.createElement("div", {
      className: _PageBody.default.common
    }, /*#__PURE__*/_react.default.createElement(_RecordBrowserContainer.default, {
      cloneCsid: cloneCsid,
      csid: normalizedCsid,
      dockTop: headerDockPosition,
      history: history,
      location: location,
      workflowState: workflowState,
      recordType: recordType,
      relatedCsid: normalizedRelatedCsid,
      relatedRecordType: relatedRecordType,
      vocabulary: vocabulary,
      config: config,
      onShowRelated: this.handleShowRelated
    }), /*#__PURE__*/_react.default.createElement(_RecordSidebar.default, {
      csid: normalizedCsid,
      recordType: recordType,
      vocabulary: vocabulary,
      config: config,
      history: history,
      isOpen: isSidebarOpen,
      isRelatable: isRelatable
    })));
  }
}
exports.default = RecordPage;
RecordPage.propTypes = propTypes;