'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('react-router/lib/PropTypes');

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

var _get2 = _interopRequireDefault(_get);

var _isEqual = require('lodash/isEqual');

var _isEqual2 = _interopRequireDefault(_isEqual);

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

var _RecordTitleBarContainer2 = _interopRequireDefault(_RecordTitleBarContainer);

var _RecordBrowser = require('../record/RecordBrowser');

var _RecordBrowser2 = _interopRequireDefault(_RecordBrowser);

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

var _RecordSideBar2 = _interopRequireDefault(_RecordSideBar);

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

var _RecordPage2 = _interopRequireDefault(_RecordPage);

var _RecordBody = require('../../../styles/cspace-ui/RecordBody.css');

var _RecordBody2 = _interopRequireDefault(_RecordBody);

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 = {
  location: _PropTypes.locationShape,
  params: _react.PropTypes.shape({
    recordType: _react.PropTypes.string.isRequired,
    path1: _react.PropTypes.string,
    path2: _react.PropTypes.string
  }).isRequired,
  createNewRecord: _react.PropTypes.func,
  readRecord: _react.PropTypes.func
};

var contextTypes = {
  config: _react.PropTypes.object.isRequired
};

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

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

    return _possibleConstructorReturn(this, (RecordPage.__proto__ || Object.getPrototypeOf(RecordPage)).apply(this, arguments));
  }

  _createClass(RecordPage, [{
    key: 'componentDidMount',
    value: function componentDidMount() {
      this.initRecord();
    }
  }, {
    key: 'componentDidUpdate',
    value: function componentDidUpdate(prevProps) {
      var params = this.props.params;
      var prevParams = prevProps.params;


      if (!(0, _isEqual2.default)(params, prevParams)) {
        this.initRecord();
      }
    }
  }, {
    key: 'getParams',
    value: function getParams() {
      var _props$params = this.props.params,
          recordType = _props$params.recordType,
          path1 = _props$params.path1,
          path2 = _props$params.path2;
      var config = this.context.config;


      var vocabulary = void 0;
      var csid = void 0;

      var recordTypeConfig = config.recordTypes[recordType];

      if (recordTypeConfig) {
        var serviceType = recordTypeConfig.serviceConfig.serviceType;

        if (serviceType === 'authority') {
          vocabulary = path1;
          csid = path2;
        } else {
          csid = path1;
        }
      }

      return {
        recordType: recordType,
        vocabulary: vocabulary,
        csid: csid
      };
    }
  }, {
    key: 'initRecord',
    value: function initRecord() {
      var _props = this.props,
          location = _props.location,
          createNewRecord = _props.createNewRecord,
          readRecord = _props.readRecord;
      var config = this.context.config;

      var _getParams = this.getParams(),
          recordType = _getParams.recordType,
          vocabulary = _getParams.vocabulary,
          csid = _getParams.csid;

      var recordTypeConfig = config.recordTypes[recordType];

      if (!recordTypeConfig) {
        return;
      }

      var vocabularyConfig = void 0;

      if (vocabulary) {
        vocabularyConfig = (0, _get2.default)(recordTypeConfig, ['vocabularies', vocabulary]);

        if (!vocabularyConfig) {
          return;
        }
      }

      if (csid) {
        if (readRecord) {
          readRecord(recordTypeConfig, vocabularyConfig, csid);
        }
      } else if (createNewRecord) {
        createNewRecord(recordTypeConfig, vocabularyConfig, location.query.clone);
      }
    }
  }, {
    key: 'render',
    value: function render() {
      var config = this.context.config;

      var _getParams2 = this.getParams(),
          recordType = _getParams2.recordType,
          vocabulary = _getParams2.vocabulary,
          csid = _getParams2.csid;

      var normalizedCsid = csid || '';

      return _react2.default.createElement(
        'div',
        { className: _RecordPage2.default.common },
        _react2.default.createElement(_RecordTitleBarContainer2.default, { csid: normalizedCsid, recordType: recordType }),
        _react2.default.createElement(
          'div',
          { className: _RecordBody2.default.common },
          _react2.default.createElement(_RecordBrowser2.default, {
            csid: normalizedCsid,
            recordType: recordType,
            vocabulary: vocabulary,
            config: config
          }),
          _react2.default.createElement(_RecordSideBar2.default, {
            csid: normalizedCsid,
            recordType: recordType,
            vocabulary: vocabulary,
            config: config
          })
        )
      );
    }
  }]);

  return RecordPage;
}(_react.Component);

exports.default = RecordPage;


RecordPage.propTypes = propTypes;
RecordPage.contextTypes = contextTypes;