'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

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

var _propTypes2 = _interopRequireDefault(_propTypes);

var _immutable = require('immutable');

var _immutable2 = _interopRequireDefault(_immutable);

var _qs = require('qs');

var _qs2 = _interopRequireDefault(_qs);

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

var _get2 = _interopRequireDefault(_get);

var _ErrorPage = require('./ErrorPage');

var _ErrorPage2 = _interopRequireDefault(_ErrorPage);

var _RecordTitleBarContainer = require('../../containers/record/RecordTitleBarContainer');

var _RecordTitleBarContainer2 = _interopRequireDefault(_RecordTitleBarContainer);

var _RecordBrowserContainer = require('../../containers/record/RecordBrowserContainer');

var _RecordBrowserContainer2 = _interopRequireDefault(_RecordBrowserContainer);

var _RecordSideBar = require('../record/RecordSideBar');

var _RecordSideBar2 = _interopRequireDefault(_RecordSideBar);

var _configHelpers = require('../../helpers/configHelpers');

var _permissionHelpers = require('../../helpers/permissionHelpers');

var _recordDataHelpers = require('../../helpers/recordDataHelpers');

var _RecordPage = require('../../../styles/cspace-ui/RecordPage.css');

var _RecordPage2 = _interopRequireDefault(_RecordPage);

var _PageBody = require('../../../styles/cspace-ui/PageBody.css');

var _PageBody2 = _interopRequireDefault(_PageBody);

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

const getParams = exports.getParams = props => {
  const config = props.config;
  var _props$match$params = props.match.params;
  const recordType = _props$match$params.recordType,
        path1 = _props$match$params.path1,
        path2 = _props$match$params.path2,
        path3 = _props$match$params.path3;


  let vocabulary;
  let csid;
  let relatedRecordType;
  let relatedCsid;

  const recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);

  if (recordTypeConfig) {
    const serviceType = (0, _get2.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
  };
};

const propTypes = {
  config: _propTypes2.default.object.isRequired,
  data: _propTypes2.default.instanceOf(_immutable2.default.Map),
  error: _propTypes2.default.instanceOf(_immutable2.default.Map),
  history: _propTypes2.default.object,
  location: _propTypes2.default.object,
  // Use of the match prop isn't being detected by eslint.
  match: _propTypes2.default.object, // eslint-disable-line react/no-unused-prop-types
  perms: _propTypes2.default.instanceOf(_immutable2.default.Map),
  readRecord: _propTypes2.default.func,
  setRecordPagePrimaryCsid: _propTypes2.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 csid = params.csid;
    const prevCsid = prevParams.csid;
    const perms = this.props.perms;
    const prevPerms = prevProps.perms;


    if (csid !== prevCsid || perms !== prevPerms) {
      this.initRecord();
    }
  }

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


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

  initRecord() {
    var _props = this.props;
    const config = _props.config,
          readRecord = _props.readRecord,
          setRecordPagePrimaryCsid = _props.setRecordPagePrimaryCsid;

    var _getParams = getParams(this.props);

    const recordType = _getParams.recordType,
          vocabulary = _getParams.vocabulary,
          csid = _getParams.csid;


    const normalizedCsid = csid === 'new' ? '' : csid;

    if (setRecordPagePrimaryCsid) {
      setRecordPagePrimaryCsid(csid);
    }

    if (normalizedCsid && readRecord) {
      const recordTypeConfig = (0, _get2.default)(config, ['recordTypes', recordType]);

      const vocabularyConfig = vocabulary ? (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]) : undefined;

      readRecord(config, recordTypeConfig, vocabularyConfig, normalizedCsid);
    }
  }

  handleShowRelated(relatedRecordType, relatedCsid) {
    var _getParams2 = getParams(this.props);

    const recordType = _getParams2.recordType,
          vocabulary = _getParams2.vocabulary,
          csid = _getParams2.csid;
    var _props2 = this.props;
    const history = _props2.history,
          location = _props2.location;


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

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


    if (error) {
      return _react2.default.createElement(_ErrorPage2.default, { error: error.toJS() });
    }

    var _getParams3 = getParams(this.props);

    const recordType = _getParams3.recordType,
          vocabulary = _getParams3.vocabulary,
          csid = _getParams3.csid,
          relatedRecordType = _getParams3.relatedRecordType,
          relatedCsid = _getParams3.relatedCsid;


    const query = _qs2.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 _react2.default.createElement(_ErrorPage2.default, { error: validation.error });
    }

    const locationState = location.state;

    let searchName;
    let searchDescriptor;

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

    const serviceType = (0, _get2.default)(config, ['recordTypes', recordType, 'serviceConfig', 'serviceType']);
    const workflowState = (0, _recordDataHelpers.getWorkflowState)(data);

    const isRelatable = workflowState !== 'locked' && (0, _permissionHelpers.canRelate)(recordType, perms);

    return _react2.default.createElement(
      'div',
      { className: _RecordPage2.default[serviceType] },
      _react2.default.createElement(_RecordTitleBarContainer2.default, {
        csid: normalizedCsid,
        recordType: recordType,
        vocabulary: vocabulary,
        searchName: searchName,
        searchDescriptor: searchDescriptor,
        updateDocumentTitle: true,
        onDocked: this.handleTitleBarDocked
      }),
      _react2.default.createElement(
        'div',
        { className: _PageBody2.default.common },
        _react2.default.createElement(_RecordBrowserContainer2.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
        }),
        _react2.default.createElement(_RecordSideBar2.default, {
          csid: normalizedCsid,
          recordType: recordType,
          vocabulary: vocabulary,
          config: config,
          history: history,
          isRelatable: isRelatable
        })
      )
    );
  }
}

exports.default = RecordPage;
RecordPage.propTypes = propTypes;