"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
var _react = _interopRequireWildcard(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _reactIntl = require("react-intl");
var _immutable = _interopRequireDefault(require("immutable"));
var _cspaceLayout = require("cspace-layout");
var _CancelButton = _interopRequireDefault(require("../navigation/CancelButton"));
var _UnrelateButton = _interopRequireDefault(require("./UnrelateButton"));
var _ConfirmRecordUnrelateModal = _interopRequireDefault(require("../../../styles/cspace-ui/ConfirmRecordUnrelateModal.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
const messages = (0, _reactIntl.defineMessages)({
  title: {
    "id": "confirmRecordUnrelateModal.title",
    "defaultMessage": "Unrelate {recordName}"
  },
  prompt: {
    "id": "confirmRecordUnrelateModal.prompt",
    "defaultMessage": "Unrelate {title} from the primary record?"
  },
  promptMultiple: {
    "id": "confirmRecordUnrelateModal.promptMultiple",
    "defaultMessage": "Unrelate {recordCount, plural, one {the selected record} other {# selected records}} from the primary record?"
  },
  unrelating: {
    "id": "confirmRecordUnrelateModal.unrelating",
    "defaultMessage": "Unrelating..."
  }
});
const propTypes = {
  config: _propTypes.default.shape({
    recordTypes: _propTypes.default.object
  }),
  data: _propTypes.default.instanceOf(_immutable.default.Map),
  isOpen: _propTypes.default.bool,
  isMultiSelect: _propTypes.default.bool,
  isUnrelating: _propTypes.default.bool,
  recordCount: _propTypes.default.number,
  recordType: _propTypes.default.string,
  onCancelButtonClick: _propTypes.default.func,
  onCloseButtonClick: _propTypes.default.func,
  onUnrelateButtonClick: _propTypes.default.func
};
const contextTypes = {
  intl: _reactIntl.intlShape
};
class ConfirmRecordUnrelateModal extends _react.Component {
  constructor(props) {
    super(props);
    this.renderButtonBar = this.renderButtonBar.bind(this);
  }
  renderButtonBar() {
    const {
      isUnrelating,
      onCancelButtonClick,
      onUnrelateButtonClick
    } = this.props;
    return /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement(_CancelButton.default, {
      disabled: isUnrelating,
      onClick: onCancelButtonClick
    }), /*#__PURE__*/_react.default.createElement(_UnrelateButton.default, {
      isUnrelating: isUnrelating,
      onClick: onUnrelateButtonClick
    }));
  }
  render() {
    const {
      config,
      data,
      isMultiSelect,
      isOpen,
      isUnrelating,
      recordCount,
      recordType,
      onCloseButtonClick
    } = this.props;
    if (!isOpen || !isMultiSelect && !data || isMultiSelect && !recordCount) {
      return null;
    }
    const {
      intl
    } = this.context;
    const recordTypeConfig = config.recordTypes[recordType];
    const recordNameMessage = isMultiSelect ? 'collectionName' : 'name';
    const recordName = intl.formatMessage(recordTypeConfig.messages.record[recordNameMessage]);
    let content;
    if (isUnrelating) {
      content = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, messages.unrelating);
    } else if (isMultiSelect) {
      content = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.promptMultiple, {
        values: {
          recordCount
        }
      }));
    } else {
      const title = recordTypeConfig.title(data, {
        config,
        intl
      });
      content = /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.prompt, {
        values: {
          title
        }
      }));
    }
    return /*#__PURE__*/_react.default.createElement(_cspaceLayout.Modal, {
      className: _ConfirmRecordUnrelateModal.default.common,
      isOpen: isOpen,
      title: /*#__PURE__*/_react.default.createElement("h1", null, /*#__PURE__*/_react.default.createElement(_reactIntl.FormattedMessage, _extends({}, messages.title, {
        values: {
          recordName
        }
      }))),
      closeButtonClassName: "material-icons",
      closeButtonLabel: "close",
      renderButtonBar: this.renderButtonBar,
      onCloseButtonClick: onCloseButtonClick
    }, content);
  }
}
exports.default = ConfirmRecordUnrelateModal;
ConfirmRecordUnrelateModal.propTypes = propTypes;
ConfirmRecordUnrelateModal.contextTypes = contextTypes;