'use strict';

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

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactRedux = require('react-redux');

var _reactIntl = require('react-intl');

var _cspaceLayout = require('cspace-layout');

var _withConfig = require('../../enhancers/withConfig');

var _withConfig2 = _interopRequireDefault(_withConfig);

var _withRecordType = require('../../enhancers/withRecordType');

var _withRecordType2 = _interopRequireDefault(_withRecordType);

var _prefs = require('../../actions/prefs');

var _reducers = require('../../reducers');

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

var getHeader = function getHeader(key, messages) {
  var message = messages.panel[key];

  if (!message) {
    return null;
  }

  return _react2.default.createElement(
    'h3',
    null,
    _react2.default.createElement(_reactIntl.FormattedMessage, message)
  );
};

var mapStateToProps = function mapStateToProps(state, ownProps) {
  var config = ownProps.config,
      msgkey = ownProps.msgkey,
      name = ownProps.name,
      recordType = ownProps.recordType;
  var header = ownProps.header;


  var collapsed = (0, _reducers.isPanelCollapsed)(state, recordType, name);

  if (typeof collapsed === 'undefined') {
    collapsed = ownProps.collapsed;
  }

  if (!header) {
    var messages = config.recordTypes[recordType].messages;


    header = getHeader(msgkey || name, messages);
  }

  return {
    collapsed: collapsed,
    header: header
  };
};

var mapDispatchToProps = function mapDispatchToProps(dispatch, ownProps) {
  var recordType = ownProps.recordType;


  return {
    onToggleCollapsed: function onToggleCollapsed(name, collapsed) {
      dispatch((0, _prefs.collapsePanel)(recordType, name, collapsed));
    }
  };
};

var ConnectedPanel = exports.ConnectedPanel = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_cspaceLayout.Panel);

var EnhancedConnectedPanel = (0, _withRecordType2.default)((0, _withConfig2.default)(ConnectedPanel));

EnhancedConnectedPanel.propTypes = _cspaceLayout.Panel.propTypes;

exports.default = EnhancedConnectedPanel;