"use strict";

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

var _react = _interopRequireDefault(require("react"));

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

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

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

var _withConfig = _interopRequireDefault(require("../../enhancers/withConfig"));

var _withRecordType = _interopRequireDefault(require("../../enhancers/withRecordType"));

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

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

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

const getHeader = (key, messages) => {
  const message = messages.panel[key];

  if (!message) {
    return null;
  }

  return _react.default.createElement("h3", null, _react.default.createElement(_reactIntl.FormattedMessage, message));
};

const mapStateToProps = (state, ownProps) => {
  const {
    config,
    msgkey,
    name,
    recordType
  } = ownProps;
  let {
    header
  } = ownProps;
  let collapsed = (0, _reducers.isPanelCollapsed)(state, recordType, name);

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

  if (!header) {
    const {
      messages
    } = config.recordTypes[recordType];
    header = getHeader(msgkey || name, messages);
  }

  return {
    collapsed,
    header
  };
};

const mapDispatchToProps = (dispatch, ownProps) => {
  const {
    recordType
  } = ownProps;
  return {
    onToggleCollapsed: (name, collapsed) => {
      dispatch((0, _prefs.collapsePanel)(recordType, name, collapsed));
    }
  };
};

const ConnectedPanel = (0, _reactRedux.connect)(mapStateToProps, mapDispatchToProps)(_cspaceLayout.Panel);
exports.ConnectedPanel = ConnectedPanel;
const EnhancedConnectedPanel = (0, _withRecordType.default)((0, _withConfig.default)(ConnectedPanel));
EnhancedConnectedPanel.propTypes = _cspaceLayout.Panel.propTypes;
var _default = EnhancedConnectedPanel;
exports.default = _default;