"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = exports.getParams = 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 _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

const getParams = props => {
  const {
    config
  } = props;
  const {
    recordType,
    path1,
    path2,
    path3
  } = props.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.object.isRequired,
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  error: _propTypes.default.instanceOf(_immutable.default.Map),
  history: _propTypes.default.object,
  isSidebarOpen: _propTypes.default.bool,
  location: _propTypes.default.object,
  // Use of the match prop isn't being detected by eslint.
  match: _propTypes.default.object,
  // eslint-disable-line react/no-unused-prop-types
  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,
      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);
      }

      history.replace((0, _refNameHelpers.refNameToUrl)(config, (0, _recordDataHelpers.getRefName)(data), dataCsid));
    }
  }

  componentWillUnmount() {
    const {
      setRecordPagePrimaryCsid
    } = this.props;

    if (setRecordPagePrimaryCsid) {
      setRecordPagePrimaryCsid(undefined);
    }
  }

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

  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/".concat(path),
      state: location.state
    });
  }

  handleTitleBarDocked(height) {
    this.setState({
      headerDockPosition: height
    });
  }

  render() {
    const {
      headerDockPosition
    } = this.state;
    const {
      config,
      data,
      error,
      history,
      isSidebarOpen,
      location,
      perms
    } = this.props;

    if (error) {
      return _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 _react.default.createElement(_ErrorPage.default, {
        error: validation.error
      });
    }

    const locationState = location.state;
    let searchName;
    let searchDescriptor;

    if (locationState) {
      searchName = locationState.searchName;
      searchDescriptor = _immutable.default.fromJS(locationState.searchDescriptor);
    }

    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 _react.default.createElement("div", {
      className: _RecordPage.default[serviceType]
    }, _react.default.createElement(_RecordTitleBarContainer.default, {
      csid: normalizedCsid,
      recordType: recordType,
      vocabulary: vocabulary,
      searchName: searchName,
      searchDescriptor: searchDescriptor,
      updateDocumentTitle: true,
      onDocked: this.handleTitleBarDocked
    }), _react.default.createElement("div", {
      className: _PageBody.default.common
    }, _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
    }), _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;